#!/bin/sh
# *********************************************************************
# $Id: install-sh 45 2007-09-24 09:06:24Z carlo $
#
# install-sh: SWU installation script.
#
# Copyright (c) 2006,2009 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.5
SNAPSHOT=rc1		# this can be rc1, rc2, etc., or 'final'.

DOCSTUFF='AUTHORS COPYING README'
DOCSTUFF="$DOCSTUFF INSTALL WARRANTY"

# 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

# 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 -We"
       # 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"
       break
    ;;
    /*/gawk)
       AWKEXE=$PREFIX/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"

# 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/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 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 _awk; ln -s $AWK _awk
)

# 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

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

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

exit 0

# All done.
