#!/bin/sh
# *********************************************************************
#
# install-sh: SWU installation script.
#
# Copyright (c) 2006-2026 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 SWU 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 "/swu" will be automatically
# appended to it.
#
# *********************************************************************

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

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

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

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

DOCSTUFF='AUTHORS BUGS COPYING README 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

if [ X$PREFIX = X/usr/local ]
then
   # Remove obsolete stuff if any, and if appropriate.
   rm -fv $PREFIX/bin/old $PREFIX/bin/bsearch
fi

if [ X$SYSLIBDIR = X ]
then
   SYSLIBDIR=/usr/local/lib
fi

export PREFIX=$PREFIX/swu

# 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

# 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

             SWU, Copyright (c) 2006-2016 by Carlo Strozzi

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

# Look for bsearch(1). If none is found, then I will install my
# own (possibly patched) copy, otherwise I will use the one that
# is already available.
#
BSEARCH=`which bsearch`

# Look for AWK
AWK=
AWKOPTS=
for i in mawk gawk
do
    AWK=`which $i`
    case "$AWK" in
       /*/mawk)
       AWKEXE="$AWK -Wexec"
       # No extra options like the following can be passed to
       # self-contained mawk(1) programs, unless a '_mawk' wrapper
       # similar to the following '_gawk' wrapper is used, which is
       # currently not done. Short of such wrapper, setting AWKOPTS
       # in this way will therefore only be of use when mawk(1) is
       # invoked with "mawk ... -f program-file ..." .
       AWKOPTS="-Wsprintf=2048"
       AWKOPTS2="-Wsprintf=4096"
       break
    ;;
    /*/gawk)
       # The _gawk wrapper isn't without its own problems, but
       # reasonably recent versions of gawk(1) take the "-Wexec"
       # option, so I no longer need to use my own wrapper.
       #AWKEXE=$PREFIX/bin/_gawk	# will use SWU's gawk wrapper.
       AWKEXE="$AWK -Wexec"		# newer versions of gawk(1)
       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"

# 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/include \
      $PREFIX/src $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

rm -f $PREFIX/bin/*

# Copy executable scripts.
#echo "$0: copying SWU executable scripts ..." >&2
#for i in perl/*	# awk/* sh/* ... etc.
#do
#   case $i in
#	*RCS*|*CVS*|*,v) continue ;;
#   esac
#   cp $i $PREFIX/bin
#   if $TEST $? -ne 0
#   then
#      echo "$0: unable to copy scripts to '$PREFIX/bin'" >&2
#      exit 1
#   fi
#   chmod 755 $PREFIX/bin/$i
#done

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

# Perform compatibility stuff in ./bin/
(
  cd $PREFIX/bin
  rm -f awk
  # Leave any previous _awk link alone
  if test -e _awk
  then
     true
  else
     ln -s $AWK _awk
  fi
)

# 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).
#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 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 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 || exit $?
   make clean

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

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

   make PREFIX=$PREFIX install || exit $?
   make clean
)

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

if [ "X$BSEARCH" = X ]
then
   ln -s $PREFIX/lib/bsearch $PREFIX/bin/_bsearch
else
   ln -s $BSEARCH $PREFIX/bin/_bsearch
fi

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

# Create/update the SWU config file.

if $TEST -e $PREFIX/etc/swu.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 "SWU_VERSION='$VERSION'" >> $PREFIX/etc/swu.rc

   if [ $? -ne 0 ]
   then
      echo "$0: unable to append to $PREFIX/etc/swu.rc" >&2
      exit 1
   fi
else
   if sed "s~@SWU_VERSION@~$VERSION~" etc/swu.rc > $PREFIX/etc/swu.rc
   then :
   else
      echo "$0: unable to create $PREFIX/etc/swu.rc" >&2
      exit 1
   fi
fi

[ "X$AWKOPTS" = X ] || echo "SWU_AWK_OPTS='$AWKOPTS'" >> $PREFIX/etc/swu.rc
[ "X$AWKOPTS2" = X ] || echo "SWU_AWK_OPTS2='$AWKOPTS2'" >> $PREFIX/etc/swu.rc

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

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

cat <<EOF

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

	SWU_INSTALL=$PREFIX
	export SWU_INSTALL
	PATH=\$PATH:$PREFIX/bin

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

	IMPORTANT: please run 'ldconfig' to update your library cache

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

EOF

exit 0

# All done.
