#!/bin/sh

# *********************************************************************
#  Written by and copyright Carlo Strozzi <carlos@linux.it>.
#
#  install-sh: NoSQL installation script.
#  Copyright (C) 1998-2001 Carlo Strozzi <carlos@linux.it>
# 
#  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; either version 2 of the License, or
#  (at your option) any later version.
# 
#  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: Carlos 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
#
# *********************************************************************

# $Id$

####################### User configurable stuff #######################

CFLAGS='-g -O2 -Wall'

#################### End of user configurable stuff ###################

VERSION=3.1
DOCSTUFF='ACKNOWLEDGEMENTS AUTHORS COPYING ChangeLog README* TODO INSTALL'
#LIBSTUFF='nosql.lsm nosqldb.dtd'
LIBSTUFF=nosql.lsm

# Try and work-out this Unix name.
UNAME=`uname -s`

# Default compiler stuff. Pick gcc if possible.

CC=`which gcc`

if [ X$CC = X ]
then
   CC=cc
   CFLAGS='-g -O2 -Wall'
fi

case $UNAME in
   Linux*)
      CC=gcc
      CFLAGS="$CFLAGS -Wstrict-prototypes -Wmissing-prototypes -Wshadow -Wpointer-arith -Wbad-function-cast -Wcast-qual -Wcast-align -Wwrite-strings -Wmissing-declarations -Wnested-externs"
      CDEFS="$CDEFS -DHAVE_CRYPT_H"
   ;;
esac

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

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

# Get a sane test(1). Not all shells provide a standard builtin one.
TEST=`which test`

if [ X$TEST = X ]
then
   echo "$0: unable to find test(1)" >&2
   exit 1
fi

# Look for required printf(1).
PRINTF=`which printf`

if [ X$PRINTF = X ]
then
   echo "$0: unable to find printf(1)" >&2
   exit 1
fi

# Get a pager program.
PAGER=`which less`

[ X$PAGER = X ] && PAGER=more

# Show Licensing Terms and Conditions.

cat <<EOF

	NoSQL DBMS, Copyright (C) 1998-2001 by Carlo Strozzi

To install this software you are requested to accept the Terms and
Conditions under which the software itself is distributed. Please
EOF
$PRINTF 'press ENTER to display the licensing document, or CTRL-C to quit: '

read ANSWER

if $TEST -e t-and-c.txt
then :
else
   echo "$0: unable to find the licensing document" >&2
   exit 1
fi

cat t-and-c.txt | $PAGER

echo ''
$PRINTF 'Do you accept the above Terms and Conditions ? (yes/no): '
read ANSWER OTHER

ANSWER=`echo "$ANSWER" | tr A-Z a-z`

case "$ANSWER" in
     y|yes)	   ;;
     *)	    exit 1 ;;
esac

# Try and be portable with respect to standard i/o streams.
STDIN=/dev/fd/0
STDOUT=/dev/fd/1
STDERR=/dev/fd/2

if $TEST -e /dev/stdin
then
   STDIN=/dev/stdin
fi

if $TEST -e /dev/stdout
then
   STDOUT=/dev/stdout
fi

if $TEST -e /dev/stderr
then
   STDERR=/dev/stderr
fi

# Look for required lockfile(1).
LOCKFILE=`which lockfile`
if [ X$LOCKFILE = X ]
then
   echo "$0: unable to find lockfile(1) in \$PATH" >&2
   echo "$0: please make sure the procmail package is installed" >&2
   exit 1
fi

# Look for required mktemp(1).
MKTEMP=`which mktemp`
if [ X$MKTEMP = X ]
then
   echo "$0: unable to find mktemp(1) in \$PATH" >&2
   exit 1
fi

# Look for required Metamail.
METAMAIL=`which mimencode`
if [ X$METAMAIL = X ]
then
   echo "$0: unable to find mimencode(1) in \$PATH" >&2
   echo "$0: please make sure the Metamail package is installed" >&2
   exit 1
fi

# Look for recommended ash(1)
SH=`which ash`
if [ X$SH = X ]
then
   # No ash(1). Is ksh(1) available maybe ?
   SH=`which ksh`
   if [ X$SH = X ]
   then
      # Nope. Try with bash(1)
      SH=`which bash`
      if [ X$SH = X ]
      then
	 # We're out of luck, let's settle to sh(1).
   	 SH=/bin/sh
      fi
   fi
fi
SH="#!$SH"

# Look for required mawk(1)
MAWK=`which mawk`
if [ X$MAWK = X ]
then
   echo "$0: unable to find mawk(1) in \$PATH" >&2
   exit 1
fi
MAWK="#!$MAWK -We"

# Look for required perl(1)
PERL=`which perl`
if [ X$PERL = X ]
then
   echo "$0: unable to find perl(1) in \$PATH" >&2
   exit 1
fi
PERL="#!$PERL"

# 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

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

if $TEST -d $PREFIX/nosql
then :
else
   echo "$0: unable to create target directory '$PREFIX/nosql'" >&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/nosql/ ..." >&2
mkdir $PREFIX/nosql/bin $PREFIX/nosql/lib \
      $PREFIX/nosql/doc $PREFIX/nosql/help 2>/dev/null

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

# Copy sh(1) files.
echo "$0: copying sh(1) files ..." >&2
for i in sh/*
do
   j=`basename $i`
   awk 'NR==1 {print "'"$SH"'"; next} {print}' $i |
   sed -e 's,@NOSQLPATH@,'$PREFIX',g' \
       -e 's,@STDIN@,'$STDIN',g' \
       -e 's,@STDOUT@,'$STDOUT',g' \
       -e 's,@STDERR@,'$STDERR',g' > $PREFIX/nosql/bin/$j.$$
   mv $PREFIX/nosql/bin/$j.$$ $PREFIX/nosql/bin/$j
   chmod 755 $PREFIX/nosql/bin/$j
done

# Copy mawk(1) files.
echo "$0: copying mawk(1) files ..." >&2
for i in mawk/*
do
   j=`basename $i`
   awk 'NR==1 {print "'"$MAWK"'"; next} {print}' $i |
   sed -e 's,@NOSQLPATH@,'$PREFIX',g' \
       -e 's,@STDIN@,'$STDIN',g' \
       -e 's,@STDOUT@,'$STDOUT',g' \
       -e 's,@STDERR@,'$STDERR',g' > $PREFIX/nosql/bin/$j.$$
   mv $PREFIX/nosql/bin/$j.$$ $PREFIX/nosql/bin/$j
   chmod 755 $PREFIX/nosql/bin/$j
done

# Set up compatibility symlinks.
rm -f $PREFIX/nosql/bin/project \
      $PREFIX/nosql/bin/select \
      $PREFIX/nosql/bin/justify
ln -s $PREFIX/nosql/bin/column $PREFIX/nosql/bin/project
ln -s $PREFIX/nosql/bin/row $PREFIX/nosql/bin/select
ln -s $PREFIX/nosql/bin/prtable $PREFIX/nosql/bin/justify

# Copy perl(1) files.
echo "$0: copying perl(1) files ..." >&2
for i in perl/*
do
   j=`basename $i`
   awk 'NR==1 {print "'"$PERL"'"; next} {print}' $i |
   sed 's,@NOSQLPATH@,'$PREFIX',g' > $PREFIX/nosql/bin/$j.$$
   mv $PREFIX/nosql/bin/$j.$$ $PREFIX/nosql/bin/$j
   chmod 755 $PREFIX/nosql/bin/$j
done

# Copy documentation files.
echo "$0: copying documentation files ..." >&2
if cp doc/* $PREFIX/nosql/doc
then :
else
   echo "$0: unable to copy documentation files to '$PREFIX/nosql/doc/'" >&2
   exit 1
fi

# Copy help files.
echo "$0: copying help files ..." >&2
for i in help/*
do
   j=`basename $i`
   sed 's,@NOSQLPATH@,'$PREFIX',g' $i > $PREFIX/nosql/help/$j.$$
   mv $PREFIX/nosql/help/$j.$$ $PREFIX/nosql/help/$j
done

# Copy misc. library stuff.
echo "$0: copying extra library stuff ..." >&2
if cp $LIBSTUFF $PREFIX/nosql/lib
then :
else
   echo "$0: unable to copy misc. library stuff to '$PREFIX/nosql/lib/'" >&2
   exit 1
fi

# Copy extra documentation stuff.
echo "$0: copying extra documentation stuff ..." >&2
if cp $DOCSTUFF $PREFIX/nosql/doc
then :
else
   echo "$0: unable to copy misc. documentation stuff to '$PREFIX/nosql/doc/'" >&2
   exit 1
fi

# Compile the source programs.

echo "$0: compiling source code ..." >&2
for i in src/*.c
do
   j=`basename $i .c`
   $CC $CFLAGS $CDEFS -o $PREFIX/nosql/bin/$j $i
   if [ $? -ne 0 ]
   then
      echo "$0: compile of $i failed" >&2
      exit 1
   fi
   # Not all Unix boxes have strip(1).
   # strip $PREFIX/nosql/bin/$bin_file
done

# Set version information.
echo $VERSION > $PREFIX/nosql/nosql_version

exit 0

# All done.
