#!/bin/sh
# *********************************************************************
# $Id: install-sh 62 2009-08-11 09:19:00Z carlo $
#
# install-sh: NoSQL installation script.
#
# Copyright (c) 1998,2009 Carlo Strozzi
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 dated June, 1991.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# *********************************************************************

# *********************************************************************
# Install NoSQL scripts, programs and datafiles
#
# Author: Carlo Strozzi <carlos@linux.it>
#
# To install to a target directory other than the default /usr/local
# use the following command-line string:
#
#         PREFIX=/some/target/dir ./install-sh
#
# Note: since some mkdir(1) don't support the -p flag, we don't use it.
# So, the ancestors of the target directory must already exist.
#
# To look for a default Bourne shell other than ash(1), say "bash", use
# the following command-line string:
#
#	  BOURNE=bash ./install-sh
#
# Of course the above can be combined, i.e.:
#
#	  PREFIX=/some/target/dir BOURNE=bash ./install-sh
#
# Whatever the value of PREFIX, the suffix "/nosql" will always be
# appended to it.
#
# *********************************************************************

VERSION=4.1.8
SNAPSHOT=final		# this can be rc1, rc2, etc., or 'final'.

DOCSTUFF='ACKNOWLEDGEMENTS AUTHORS NEWS COPYING WARRANTY README TODO'
DOCSTUFF="$DOCSTUFF INSTALL ChangeLog"
#LIBSTUFF='nosql.lsm'

# Try and work-out this system name.
UNAME=`uname -s | tr A-Z a-z`

# Set the default installation path prefix if not specified in env.

if [ X$PREFIX = X ]
then
   PREFIX=/usr/local
fi

SAVED_PREFIX=$PREFIX
export SWU_PREFIX=$PREFIX/swu
export PREFIX=$PREFIX/nosql

# This is specific for Debian.
PREFIX=$DESTDIR$PREFIX

# Set the preferred Bourne shell (default = "ash").
[ X$BOURNE = X ] && BOURNE=ash

# Set default location for the Shell-Ware Utilities package.

if [ X$SWUDIR = X ]
then
   SWUDIR=/usr/local/swu
   export SWUDIR
fi

# Get a sane test(1). Not all shells provide a standard builtin one.
TEST=`which test`
case "$TEST" in
   /*)	;;
   *)
   	echo "$0: unable to find test(1)" >&2
   	exit 1
   ;;
esac

# Look for required printf(1).
PRINTF=`which printf`
case "$PRINTF" in
   /*)	;;
   *)
   	echo "$0: unable to find printf(1)" >&2
   	exit 1
   ;;
esac

# Get a pager program.
for i in less more pg
do
    PAGER=`which $i`
    case "$PAGER" in
       /*) break ;;
       *)  PAGER= ;;
    esac
done

if [ X$PAGER = X ]
then
   echo "$0: unable to find a known pager program" >&2
   exit 1
fi

# Inform the user about copying deatails.

cat <<EOF

	NoSQL DBMS, Copyright (c) 1998,2009 by Carlo Strozzi

NoSQL is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Parts of this program are distributed under other, compatible, copying terms,
and are copyright by the respective authors. Please refer to the accompanying
documentation for details.

There is absolutely no warranty for NoSQL. Please refer to file WARRANTY
for more information.

To display detailed copying and warranty information, most NoSQL operators
can be called with the '--show-copying' and '--show-warranty' command-line
options respectively. For those NoSQL programs that do not yet support such
options, please refer to the relevant source code for copying and warranty
information.

EOF
$PRINTF 'press ENTER to continue, or type CTRL-C to quit: '

read ANSWER

# Look for required lockfile(1).
LOCKFILE=`which lockfile`
case "$LOCKFILE" in
   /*)	;;
   *)
   	echo "$0: unable to find lockfile(1)" >&2
	echo "$0: please make sure the procmail package is installed" >&2
   	exit 1
   ;;
esac

# Look for required mktemp(1).
MKTEMP=`which mktemp`
case "$MKTEMP" in
   /*)	;;
   *)
	echo "$0: unable to find mktemp(1) in PATH" >&2
	exit 1
   ;;
esac

# Look for required iconv(1).
ICONV=`which iconv`
case "$ICONV" in
   /*)	;;
   *)
	echo "$0: unable to find iconv(1) in PATH" >&2
	exit 1
   ;;
esac

# Look for a shell as close as possible to ash(1)
for i in $BOURNE ash dash ksh bash
do
    SH=`which $i`
    case "$SH" in
       /*) break ;;
       *)  SH= ;;
    esac
done

# Let's settle to sh(1) if we're out of luck with all others.

[ X$SH = X ] && SH=/bin/sh

SH="#!$SH"

# Look for required Shell-Ware Utilities, and install a minimal
# version if not found.
SAVED_PATH=$PATH
export PATH=$PATH:$SWUDIR/bin
SWULOCK=`which swu-lock`
case "$SWULOCK" in
   /*)
	# Set actual value now that it is known.
	SWUDIR=$(dirname $SWULOCK | sed 's,/bin$,,')
   ;;
   *)
	if cd swu
	then
	   PREFIX=$SAVED_PREFIX ./install-sh
	   [ $? -eq 0 ] || exit 1
	else
	  echo "$0: unable to 'cd' into '$PREFIX'" >&2
	  exit 1
	fi
	cd -
	SWUDIR=$SWU_PREFIX
   ;;
esac

# Set also this one the its final value.
export PATH=$SAVED_PATH:$SWUDIR/bin

# Look for AWK
AWK=
for i in mawk gawk
do
    AWK=`which $i`
    case "$AWK" in
       /*/mawk)
	  AWKEXE="$AWK -We"
	  break
       ;;
       /*/gawk)
	  AWKEXE=$SWUDIR/bin/_gawk	# will use SWU's gawk wrapper.
	  break
       ;;
    esac
done

if [ X$AWK = X ]
then
   echo "$0: unable to find neither mawk(1) nor gawk(1) in PATH" >&2
   exit 1
fi

# Look for required perl(1)
PERL=`which perl`
case "$PERL" in
   /*)	;;
   *)
	echo "$0: unable to find perl(1) in PATH" >&2
	exit 1
   ;;
esac

PERL="#!$PERL"

# Test for older, incompatible versions of NoSQL.

OLD_VERSION="`cat $PREFIX/nosql.version 2>/dev/null`"
case $OLD_VERSION in
     [0-3].*)
	cat <<EOF

>> Warning: you seem to already have an older, incompatible version     <<
>> of NoSQL. If you overwrite with the new version you will most likely <<
>> break your current NoSQL applications.                               <<

To choose an installation PATH other than $PREFIX
you can quit this program by pressing CTRL-C and rerun it with:

	    PREFIX=/some/other/path ./install-sh

To continue anyway just hit ENTER now, otherwise press CTRL-C to quit.

EOF
	read answr
     ;;
esac

umask 022				# Just in case.

# Create target directories. Not all mkdir(1) support the '-p' flag.

echo "$0: creating $PREFIX/ ..." >&2
mkdir $PREFIX 2>/dev/null

if $TEST -d $PREFIX
then :
else
   echo "$0: unable to create target directory '$PREFIX'" >&2
   exit 1
fi


#cd $PREFIX
#if [ $? -ne 0 ]
#   echo "$0: unable to 'cd' into '$PREFIX'" >&2
#   exit 1
#fi

echo "$0: creating subdirs in $PREFIX/ ..." >&2
mkdir $PREFIX/bin $PREFIX/lib \
      $PREFIX/doc $PREFIX/help $PREFIX/etc 2>/dev/null

if $TEST -d $PREFIX/bin
then :
else
   echo "$0: unable to create target directories under '$PREFIX/'" >&2
   exit 1
fi

# Assess available local command paths.
LOCAL_PATH=$SWUDIR/bin:$PREFIX/bin
for i in /usr/gnu/bin /usr/local/bin /usr/ucb /usr/bin /bin
do
    $TEST -d $i && LOCAL_PATH=$LOCAL_PATH:$i
done

SWU_INSTALL=$SWUDIR
NOSQL_INSTALL=$PREFIX

# Copy sh(1) files.
echo "$0: copying sh(1) files ..." >&2
for i in sh/*
do
   case $i in
	*RCS*|*CVS*|*,v) continue ;;
   esac
   j=`basename $i`
   $AWK -- 'NR==1 {print "'"$SH"'"; next} {print}' $i |
       sed -e "s,@NOSQL_INSTALL@,$NOSQL_INSTALL,g" \
	   -e "s,@SWU_INSTALL@,$SWU_INSTALL,g" \
	   -e "s,@LOCAL_PATH@,$LOCAL_PATH,g" \
       > $PREFIX/bin/$j.$$
   mv $PREFIX/bin/$j.$$ $PREFIX/bin/$j
   chmod 755 $PREFIX/bin/$j
done

# Copy AWK files.
echo "$0: copying AWK executable files ..." >&2
for i in mawk/*
do
   case $i in
	*RCS*|*CVS*|*,v) continue ;;
   esac
   j=`basename $i`
   $AWK -- 'NR==1 {print "'"#!$AWKEXE"'"; next} {print}' $i \
       > $PREFIX/bin/$j.$$
   mv $PREFIX/bin/$j.$$ $PREFIX/bin/$j
   chmod 755 $PREFIX/bin/$j
done

# Perform compatibility stuff in ./bin/
(
  cd $PREFIX/bin

  rm -f project select justify template rmcolumn notcolumn \
	row column filter number depend index _index search \
	random rename union total update repair spottable unspot \
	addheader headon makedata _join inosql getrow compute

  ln -s getcolumn column
  ln -s getcolumn project
  ln -s getcolumn rmcolumn
  ln -s getcolumn notcolumn
  ln -s awktable select
  ln -s awktable getrow
  ln -s awktable row
  ln -s awktable compute
  ln -s filtertable filter
  ln -s prtable justify
  ln -s xreftable template 
  ln -s nltable number
  ln -s deptable depend
  ln -s indextable index
  ln -s searchtable search
  ln -s rndtable random
  ln -s renamecol rename
  ln -s uniontable union
  ln -s totaltable total
  ln -s updtable update
  ln -s repairtable repair
)

# Perform compatibility stuff in ./help/
(
  cd $PREFIX/help
  rm -f *.txt
)

# Copy perl(1) files.
echo "$0: copying perl(1) files ..." >&2
for i in perl/*
do
   case $i in
	*RCS*|*CVS*|*,v) continue ;;
   esac
   j=`basename $i`
   $AWK 'NR==1 {print "'"$PERL"'"; next} {print}' $i \
   > $PREFIX/bin/$j.$$
   mv $PREFIX/bin/$j.$$ $PREFIX/bin/$j
   chmod 755 $PREFIX/bin/$j
done

# Copy documentation files.
echo "$0: copying documentation files ..." >&2
for i in doc/*
do
   case $i in
	*RCS*|*CVS*|*,v) continue ;;
   esac
   cp $i $PREFIX/doc
   if $TEST $? -ne 0
   then
      echo "$0: unable to copy documentation files to '$PREFIX/doc/'" >&2
      exit 1
   fi
done

# Copy help files.
echo "$0: copying help files ..." >&2
for i in help/*
do
   case $i in
	*RCS*|*CVS*|*,v) continue ;;
   esac
   cp $i $PREFIX/help
   if $TEST $? -ne 0
   then
      echo "$0: unable to copy help files to '$PREFIX/help/'" >&2
      exit 1
   fi
done

# Copy misc. library stuff.
echo "$0: copying extra library stuff ..." >&2
for i in lib/*
do
   case $i in
	*RCS*|*CVS*|*,v) continue ;;
   esac
   if [ $i = nosql.sh ]
   then
      sed -e "s,@NOSQL_INSTALL@,$NOSQL_INSTALL,g" \
	  -e "s,@SWU_INSTALL@,$SWU_INSTALL,g" \
	  -e "s,@LOCAL_PATH@,$LOCAL_PATH,g" $i > $PREFIX/lib/$i
   else
      cp $i $PREFIX/lib
   fi
   if $TEST $? -ne 0
   then
      echo "$0: unable to copy misc. library stuff to '$PREFIX/lib/'" >&2
      exit 1
   fi
done

# Copy extra documentation stuff.
echo "$0: copying extra documentation stuff ..." >&2
for i in $DOCSTUFF
do
   case $i in
	*RCS*|*CVS*|*,v) continue ;;
   esac
   cp $i $PREFIX/doc
   if $TEST $? -ne 0
   then
      echo "$0: unable to copy misc. documentation stuff to '$PREFIX/doc/'" >&2
      exit 1
   fi
done

# Compile the source programs.

echo "$0: compiling source code ..." >&2

cd src
if [ $? -ne 0 ]
   then
   echo "$0: could not cd into src/" >&2
   exit 1
fi

cp $SWUDIR/include/util.h .
cp $SWUDIR/src/misc.c .
cp $SWUDIR/src/strlist.c .

make PREFIX=$PREFIX install
make clean

if [ $? -ne 0 ]
   then
   echo "$0: source code compile failed" >&2
   exit 1
fi

# Set version information.
rm -f $PREFIX/nosql_version
echo $VERSION "($SNAPSHOT)" > $PREFIX/nosql.version

chmod 644 $PREFIX/lib/*

# Create the global configuration file if necessary.
if $TEST -e $PREFIX/etc/nosql.rc
then :
else
cat <<EOF > $PREFIX/etc/nosql.rc
SWU_INSTALL=$SWUDIR
NOSQL_INSTALL=$PREFIX
PATH=$LOCAL_PATH
export PATH SWU_INSTALL NOSQL_INSTALL
EOF
fi

cat <<EOF

	The NoSQL installation program completed successfully.
	Before you use NoSQL insert the following statement in
	your shell .profile or similar place:

	eval \$(nosql setenv)

	The above instruction is for the Bourne shell and its
	derivatives. If you use a different shell please adapt
	it accordingly.

	You may also want to add $PREFIX/man to your MANPATH.

EOF

exit 0

# All done.
