#!/bin/sh
# *********************************************************************
# install-sh: CSA installation script.
#
# Copyright (c) 2001-2023 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 CSA 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
#
# Whatever the value of PREFIX, the suffix "/csa" will be automatically
# appended to it.
#
# *********************************************************************

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

CFLAGS="-Wall -ansi -pedantic -g -O"

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

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

DOCSTUFF='ACKNOWLEDGEMENTS AUTHORS BUGS COPYING README TODO NEWS'
DOCSTUFF="$DOCSTUFF INSTALL WARRANTY ChangeLog"

# 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

export PREFIX=$PREFIX/csa

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

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

# Look for a shell as close as possible to ash(1)
for i in 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"

# 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

# Look for required rcs(1).
RCSCI=`which ci`
case "$RCSCI" in
   /*)	;;
   *)
   	echo "$0: unable to find the Revision Control System (RCS)" >&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

      CSA, Copyright (c) 2001-2021 by Carlo Strozzi

CSA 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 CSA. Please refer to file WARRANTY
for more information.

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

read ANSWER

# Look for required Shell-Ware Utilities.
export PATH=$PATH:$SWUDIR/bin
SWULOCK=`which swu-shlock`
case "$SWULOCK" in
   /*)	;;
   *)
   	echo "$0: unable to find Shell-Ware Utilities" >&2
   	exit 1
   ;;
esac

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

RC="#!$RC"

# 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 sed(1)
SED=`which sed`
case "$SED" in
   /*)	;;
   *)
	echo "$0: unable to find sed(1) in PATH" >&2
	exit 1
   ;;
esac

SED="#!$SED -f"

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

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

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

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

# Look for required mimencode(1).
#MIMENCODE=`which mimencode`
#case "$MIMENCODE" in
#   /*)	;;
#   *)
#   	echo "$0: unable to find mimencode(1) in PATH" >&2
#   	echo "$0: make sure the 'metamail' package is installed" >&2
#   	exit 1
#   ;;
#esac

# Test for older, incompatible versions of CSA.

OLD_VERSION="`cat $PREFIX/csa.version 2>/dev/null`"
case $OLD_VERSION in
     0.[1-8]*|0.9.[0-2]*)
	cat <<EOF

>> Warning: you seem to already have an older, incompatible version   <<
>> of CSA. If you overwrite with the new version you will most likely <<
>> break your current CSA 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

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 subdirs in $PREFIX/ ..." >&2
mkdir $PREFIX/bin $PREFIX/lib $PREFIX/doc \
      $PREFIX/lib/include $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

rm -f $PREFIX/bin/*

if $TEST -e $PREFIX/etc
then
   # Skip directory if it exists, not to overwrite local config files.
   :
else
   mkdir $PREFIX/etc 2>/dev/null

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

# Copy library files.
echo "$0: copying CSA libraries ..." >&2
rm -f $PREFIX/lib/include/*.awk
for i in lib/*
do
   case $i in
	*RCS*|*CVS*|*,v) continue ;;
   esac
   cp $i $PREFIX/lib
done

# Copy AWK include files.
cp lib/include/*.awk $PREFIX/lib/include
if $TEST $? -ne 0
then
   echo "$0: unable to copy AWK files to '$PREFIX/lib/include'" >&2
   exit 1
fi

# Remove any obsolete files.
rm -f $PREFIX/lib/daplib.rc \
      $PREFIX/lib/authlib.rc \
      $PREFIX/lib/emaillib.awk \
      $PREFIX/lib/cgi_list2form.awk \
      $PREFIX/lib/csa_list2form.awk

# Copy rc(1) files.
echo "$0: copying rc(1) files ..." >&2
for i in rc/*
do
   case $i in
	*RCS*|*CVS*|*,v) continue ;;
   esac
   j=`basename $i`
   case $j in
	CSA.cgi) pgm="$RC -p" ;;
	*)   pgm="$RC" ;;
   esac
   $AWK -- 'NR==1 {print "'"$pgm"'"; next} {print}' $i \
       > $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

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

# Copy sh(1) files (none currently).
#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 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
rm -fv $PREFIX/help/*
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 the message tables.
echo "$0: copying the message tables ..." >&2
rm -rf $PREFIX/message.d/*

if find message.d/0 -maxdepth 1 -type f |
	cpio -pd --no-preserve-owner $PREFIX
then :
else
   echo "$0: unable to copy files to '$PREFIX/'" >&2
   exit 1
fi

# Get rid of obsolete directory, if any.
rm -rf $PREFIX/var/

# Copy misc. stuff.
#echo "$0: copying sample files ..." >&2
#for i in csa.rc-example cns-authlib.rc-example
#do
#   cp $i $PREFIX
#   if $TEST $? -ne 0
#   then
#      echo "$0: unable to copy $i to '$PREFIX/'" >&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

# Set up compatibility symlinks.
#(
#  cd $PREFIX/lib
#  rm -f $PREFIX/lib/cgimapi.awk \
#      $PREFIX/lib/cgimapi.rc \
#      $PREFIX/lib/emailmapi.awk \
#      $PREFIX/lib/emailmapi.rc
#      ln -s cgilib.awk cgimapi.awk
#      ln -s cgilib.rc cgimapi.rc
#      ln -s emaillib.awk emailmapi.awk
#      ln -s emaillib.rc emailmapi.rc
#)

# Compile the source programs.

echo "$0: compiling the 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

# Fixing permissions of target files/directories.
find $PREFIX -type d -exec chmod 755 {} \;
find $PREFIX -type f -exec chmod go+r {} \;

# Set version information.
echo "$0: setting CSA version information ..." >&2

# Executable files.
if sed "s,@CSA_INSTALL@,$PREFIX," $PREFIX/bin/CSA.cgi > $PREFIX/bin/CSA.cgi.$$
then
   mv $PREFIX/bin/CSA.cgi.$$ $PREFIX/bin/CSA.cgi
   chmod 755 $PREFIX/bin/CSA.cgi
else
   echo "$0: unable to set CSA version information in CSA Web driver" >&2
   exit 1
fi

now=$(date -u '+%a, %d %b %Y %H:%M:%S GMT')

# Non-executable files.

if $TEST -e $PREFIX/etc/csa.rc
then

   # I simply append stuff to the file, thus overriding any previous
   # settings of the same variables. Eventually the file may grow
   # a bit large, and a manual cleanup may become desirable.

   echo "CSA_INSTALL='$PREFIX'" >> $PREFIX/etc/csa.rc
   echo "CSA_VERSION='$VERSION'" >> $PREFIX/etc/csa.rc
   echo "CSA_INSTALL_UTC='$now'" >> $PREFIX/etc/csa.rc

   if [ $? -ne 0 ]
   then
      echo "$0: unable to append to $PREFIX/etc/csa.rc" >&2
      exit 1
   fi
else
   if sed "s~@CSA_INSTALL@~$PREFIX~;s~@CSA_VERSION@~$VERSION~;s~@CSA_INSTALL_UTC@~$now~;s~@SWU_INSTALL@~$SWUDIR~" etc/csa.rc > $PREFIX/etc/csa.rc
   then :
   else
      echo "$0: unable to create $PREFIX/etc/csa.rc" >&2
      exit 1
   fi
fi

if sed "s~@CSA_INSTALL@~$PREFIX~" lib/csaMainlib.rc > $PREFIX/lib/csaMainlib.rc
then :
else
   echo "$0: unable to write to $PREFIX/lib/csaMainlib.rc" >&2
   exit 1
fi

echo "$VERSION ($SNAPSHOT)" > $PREFIX/csa.version

cat <<EOF

	The CSA installation program completed successfully.

EOF

exit 0

# All done.
