#!/bin/sh
# *********************************************************************
# install-sh: NoSQL installation script.
#
# Copyright (c) 1998,2006 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.
#
# *********************************************************************
# $Id: install-sh,v 1.22 2006/03/10 11:31:38 carlo Exp $

# *********************************************************************
# Install NoSQL scripts, programs and datafiles
#
# Author: Carlos Strozzi <carlos@linux.it>
#
# To install to a target directory other than the default /usr/local/nosql
# 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.
# *********************************************************************

VERSION=4.0.14
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/nosql
   export PREFIX
fi

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

# 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,2006 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 a shell as close as possible to ash(1)
for i in ash 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 mawk(1)
AWK=`which mawk`
case "$AWK" in
   /*)	;;
   *)
	echo "$0: unable to find mawk(1) in PATH" >&2
	exit 1
   ;;
esac

# 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

# 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/nosql
#if [ $? -ne 0 ]
#   echo "$0: unable to 'cd' into '$PREFIX/nosql'" >&2
#   exit 1
#fi

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

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

# 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 \
       > $PREFIX/bin/$j.$$
   mv $PREFIX/bin/$j.$$ $PREFIX/bin/$j
   chmod 755 $PREFIX/bin/$j
done

# Copy mawk(1) files.
echo "$0: copying mawk(1) files ..." >&2
for i in mawk/*
do
   case $i in
	*RCS*|*CVS*|*,v) continue ;;
   esac
   j=`basename $i`
   awk 'NR==1 {print "'"#!$AWK -We"'"; 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

  ln -s getcolumn column
  ln -s getcolumn project
  ln -s getcolumn rmcolumn
  ln -s getcolumn notcolumn
  ln -s getrow select
  ln -s getrow row
  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
   cp $i $PREFIX/lib
   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

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 > $PREFIX/nosql.version

cat <<EOF

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

	NOSQL_INSTALL=$PREFIX
	export NOSQL_INSTALL

	The above statements are for the Bourne shell and its
	derivatives. If you use a different shell please adapt
	them accordingly.

EOF

exit 0

# All done.
