#!/bin/sh
# *********************************************************************
# install-sh: NoSQL installation script.
#
# Copyright (c) 1998,1999,2000,2001,2002,2003 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; 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.
#
# *********************************************************************
# $Id: install-sh,v 1.14 2003/10/09 10:53:29 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/
# use the following command-line string:
#
#         PREFIX=/some/target/dir ./install-sh
#
# *********************************************************************

VERSION=4.0.7
DOCSTUFF='ACKNOWLEDGEMENTS AUTHORS NEWS COPYING 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
   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

# Show license.

cat <<EOF

	NoSQL DBMS, Copyright (c) 1998-2003 by Carlo Strozzi

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

read ANSWER

if $TEST -e COPYING
then :
else
   echo "$0: unable to find the licensing document" >&2
   exit 1
fi

cat COPYING | $PAGER

echo ''
$PRINTF 'Do you accept the above licensing terms ? (yes/no): '
read ANSWER OTHER

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

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

# 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"

# 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
   case $i in
	*RCS*|*CVS*|*,v) continue ;;
   esac
   j=`basename $i`
   awk 'NR==1 {print "'"$SH"'"; next} {print}' $i \
       > $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
   case $i in
	*RCS*|*CVS*|*,v) continue ;;
   esac
   j=`basename $i`
   awk 'NR==1 {print "'"#!$AWK -We"'"; next} {print}' $i \
       > $PREFIX/nosql/bin/$j.$$
   mv $PREFIX/nosql/bin/$j.$$ $PREFIX/nosql/bin/$j
   chmod 755 $PREFIX/nosql/bin/$j
done

# Perform compatibility stuff in ./bin/
(
  cd $PREFIX/nosql/bin
  rm -f project select justify template notcolumn
  ln -s column project
  ln -s rmcolumn notcolumn
  ln -s row select
  ln -s prtable justify
  ln -s xreftable template 
  rm -f $PREFIX/bin/nosql
  ln -s $PREFIX/nosql/bin/nosql $PREFIX/bin/nosql
  rm -f spottable unspot addheader headon makedata
)

# Perform compatibility stuff in ./help/
(
  cd $PREFIX/nosql/help
  rm -f addheader.txt headon.txt makedata.txt spottable.txt unspot.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/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
for i in doc/*
do
   case $i in
	*RCS*|*CVS*|*,v) continue ;;
   esac
   cp $i $PREFIX/nosql/doc
   if $TEST $? -ne 0
   then
      echo "$0: unable to copy documentation files to '$PREFIX/nosql/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/nosql/help
   if $TEST $? -ne 0
   then
      echo "$0: unable to copy help files to '$PREFIX/nosql/help/'" >&2
      exit 1
   fi
done

# Copy misc. library stuff.
echo "$0: copying extra library stuff ..." >&2
for i in $LIBSTUFF
do
   case $i in
	*RCS*|*CVS*|*,v) continue ;;
   esac
   cp $i $PREFIX/nosql/lib
   if $TEST $? -ne 0
   then
      echo "$0: unable to copy misc. library stuff to '$PREFIX/nosql/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/nosql/doc
   if $TEST $? -ne 0
   then
      echo "$0: unable to copy misc. documentation stuff to '$PREFIX/nosql/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/nosql_version
echo $VERSION > $PREFIX/nosql/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/nosql
	export NOSQL_INSTALL

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

EOF

exit 0

# All done.
