#!/bin/sh

# $Id: makexite.tmpl,v 1.36 1997/09/05 08:42:06 svein Exp $

############################################################################
#
# This script will perform the second stage of the installation of the
# XITE software.
#
# For usage, execute (while in the main XITE directory)
#
#     etc/makexite -help
#
# This script depends on:
#
#     XITE_HOME     (command-line parameter, environment variable or
#                    hardcoded in this script)
#
# The XITE_HOME parameter has a default value given either by the environment
# variable XITE_HOME or else by the value which was edited in automatically
# by the XITE install_xite script.
#
# Author: Svein Bøe, email: svein@ifi.uio.no or blab@ifi.uio.no
#
############################################################################

# A value for XITE_HOME should have been automatically edited in by the
# install_xite script. This can be overridden by a command-line argument or
# an environment variable.

if test -z "$XITE_HOME"; then
    XITE_HOME=
fi

# Look for a command-line argument for XITE_HOME. Do not check the other
# arguments yet, just store them.

clean=0; inc=0; lib=0; pro=0; doc=0; xpro=0; help=0; dirHelp=0;
dirs=""; tmpDirs=""; make_opt=""; make_macro=""; verbose=0; Verbose=0;
targets=""; message=""; loc_dir=0; do_make=0

while test $# -gt 0; do
    case "$1" in
    -c)   
	if test "$clean" -eq 1;	then
	    clean=2
	else
	    clean=1
	fi
	shift;;
    -i)   inc=1; shift;;
    -l)   lib=1; shift;;
    -e)   pro=1; shift;;
    -d)   doc=1; shift;;
    -p)   loc_dir=1; shift;;
    -all) clean=2; inc=1; lib=1; pro=1; doc=1; shift;;
    -h|-help)
	help=1; shift; break
	;;
    -xe)  xpro=1; shift;;
    -xite) shift
	XITE_HOME="$1"; shift
	;;
    -v) verbose=1; shift;;
    -vv) Verbose=1; verbose=1; shift;;
    -y) shift
	while test $# -gt 0; do
   	    case "$1" in
		-z) break;;
		*)  make_opt="$make_opt $1"; shift;;
	    esac
	done;;
    -z) shift
	while test $# -gt 0; do
	    case "$1" in
		-y) break;;
		*) make_macro="$make_macro $1"; shift;;
	    esac
	done;;
    -*) help=1; shift;
	message="Unknown option $1"
	break;;
    *)	tmpDirs="$tmpDirs $1"; shift;;
    esac
done

# XITE_HOME may still be unset.

if test -z "$XITE_HOME"; then
    echo "usage: $0 [<option>...] [<dir>...] [-y <m-opt>...] [-z <m-macro>]" \
	 1>&2
    echo "The XITE home directory is not known." 1>&2
    dirHelp=1
fi

if test ! -d "$XITE_HOME" -a "$dirHelp" -eq 0; then
    # XITE_HOME has been set in some way

    echo "usage: $0 [<option>...] [<dir>...] [-y <m-opt>...] [-z <m-macro>]" \
	 1>&2
    echo "The XITE home directory, $XITE_HOME, does not exist." 1>&2
    dirHelp=1
fi

if test "$dirHelp" -eq 1; then
    echo "It must be given in one of the following ways:" 1>&2
    echo "   - As a command-line parameter (first priority)." 1>&2
    echo "   - As the environment variable XITE_HOME (second priority)." 1>&2
    echo "   - Edited into this script (third priority)." 1>&2

    exit 1
fi

scriptdir=$XITE_HOME/etc

if test ! -d "$scriptdir"; then
    echo "XITE script directory, $XITE_HOME/etc, does not exist." 1>&2
    exit 1
fi

utildir=$XITE_HOME/etc/.install

if test ! -d "$utildir"; then
    echo "`basename $0`:Directory $utildir does not exist." 1>&2
    echo "Rerun the XITE installation script 'install_xite'." 1>&2
    echo "If it has been removed, you must get XITE once again" 1>&2
    echo "with ftp from 'ftp.ifi.uio.no' in pub/blab/xite." 1>&2
    exit 1
fi

if test "$help" -eq 1; then
    if test -n "$message"; then
	echo "$0: $message"
    fi

    echo "usage: $0 [<option>...] [<dir>...] [-y <m-opt>...] [-z <m-macro>]" \
	 1>&2
    echo "where <option> is one of"                                     1>&2
    echo "  -h(elp)       : Print this usage message."                  1>&2
    echo "  -c            : Clean."                                     1>&2
    echo "  -i            : Install header files."                      1>&2
    echo "  -l            : Build all functions in library."            1>&2
    echo "  -e            : Build all executables."                     1>&2
    echo "  -d            : Build documentation."                       1>&2
    echo "  -all          : All of '-c', '-i', '-l', '-e' and '-d'."    1>&2
    echo "                  This is the default behaviour."             1>&2
    echo "  -v            : Verbose."                                   1>&2
    echo "  -vv           : Very verbose."                              1>&2
    echo "  -xe           : Build X executables."                       1>&2
    echo "  -xite         : XITE home directory."                       1>&2
    echo "  -p            : Leave object files in platform-dep subdir." 1>&2
    echo "and <m-opt> (default '-r') and <m-macro> are passed on"       1>&2
    echo "                  to make(1)."                                1>&2
    echo "<dir>           : Source dir to process, or 'src' (meaning"   1>&2
    echo "                  the whole source tree, which is default)."  1>&2

    exit 1
fi

if test "$clean" -eq 0 -a "$inc" -eq 0 -a "$lib" -eq 0 -a "$pro" -eq 0 \
    -a "$doc" -eq 0 -a "$xpro" -eq 0
then
    clean=2; inc=1; lib=1; pro=1; doc=1
fi

# Derived variables

if test "$clean" -gt 0; then
    targets="clean"
fi

if test "$inc" -eq 1; then
    targets="$targets include"
fi

if test "$lib" -eq 1; then
    targets="$targets lib"
fi

if test "$pro" -eq 1; then
    targets="$targets program"
elif test "$xpro" -eq 1; then
    targets="$targets xprog"
fi

if test "$doc" -eq 1; then
    targets="$targets doc"
fi

if test "$clean" -gt 1; then
    targets="$targets clean"
fi

echoNl=$utildir/echonl
getPlatform=$utildir/getPlatform
XITE_HOSTTYPE=`$getPlatform "" "$utildir" stderr`
makefile=$scriptdir/Makefile.$XITE_HOSTTYPE

if test ! -f "$makefile"; then
    echo "There is no makefile $makefile." 1>&2
    exit 1
fi

if test "$verbose" -eq 1; then

echo ""
echo "======================================================================="
echo "!                                                                     !"
echo "!        Second part of the installation of the XITE software.        !"
echo "!                                                                     !"
echo "!        This part concerns building of the library, the executables  !"
echo "!        and the documentation.                                       !"
echo "!                                                                     !"
echo "======================================================================="
echo ""

fi

#### Finish the installation of XITE

# Find directories to build.

if test -n "$tmpDirs"; then
    for dir in $tmpDirs; do
	arg=`basename $dir`

	if test "$arg" = '.'; then
	    arg=`basename \`pwd\``
	fi

	if test "$arg" = 'src'; then
	    cd $XITE_HOME/src
	    dirs=`/bin/ls`
	    break
	else
	    cd $XITE_HOME/src
	    if test ! -d "$arg"; then
		echo "Non-existent directory: $arg"
		exit 1
	    fi
	    if test -z "$dirs"; then
		dirs="$arg"
	    else
		dirs="$dirs $arg"
	    fi
	fi
        
    done
else
    cd $XITE_HOME/src
    dirs=`/bin/ls`
fi

cd $XITE_HOME/src

mask=`umask`
umask 022

# List of directories to except from making.
case "$XITE_HOSTTYPE" in
    *)  except="";;
esac

for t in $targets; do
    if test "$t" = "clean"; then
	text="Cleaning in"
    elif test "$t" = "include"; then
	text="Installing include files for"
    elif test "$t" = "lib"; then
	text="Compiling functions in"
    elif test "$t" = "program"; then
	text="Compiling and installing programs in"
    elif test "$t" = "xprog"; then
	text="Compiling and installing X programs in"
    elif test "$t" = "doc"; then
	text="Installing documentation for"
    fi

    # For each directory specified
    for dir in $dirs; do
	# Skip CVS directory
	if test -d "$dir" -a "$dir" != "CVS"; then
	    isExc=0
	    for exc in $except; do
		if test "$exc" = "$dir"; then
		   isExc=1; break
		fi
	    done

	    # Only process directories which were not excepted during
	    # installation
	    if test "$isExc" -eq 0; then

		if test "$verbose" -eq 1; then
		    $echoNl "--------------------------------------------"
		    echo "----------------"
		    echo "$text $dir."
		fi

		cd $dir

		# Check whether subdirectories exist. Avoid platform-specific
		# subdirectories and CVS-directories
		subdirs=`/bin/ls`
		if test -n "$subdirs"; then
		    for sub in $subdirs; do
			# Only make a directory if the file Makefile.defs
			# exists.
			do_make=0

			if test -d "$sub" -a "$sub" != "CVS" \
			; then
			    if test "$verbose" -eq 1; then
				echo "    ------------------------------"
				echo "    $text $sub."
			    fi # $verbose

			    cd $sub

			    # Step into platform-dependent subdirectory if so
			    # requested
			    if test "$loc_dir" -ne 0 -a -f Makefile.defs; then
				# This directory needs to be made
				do_make=1

				# Step into platform-dependent subdirectory
				if test ! -d "$XITE_HOSTTYPE"; then
				    mkdir $XITE_HOSTTYPE
				fi
				cd $XITE_HOSTTYPE

				# Make sure the necessary links exist
				if test ! -f Makefile.defs; then
				    ln -s ../Makefile.defs Makefile.defs
				    ln -s $makefile Makefile
				    if test -d ../XPM; then
					ln -s ../XPM XPM
				    fi

				    for f in ../*.[ch] ../*.sh ../*.3 \
                                      ../*_3.html; do
					if test -f $f; then
					    ln -s $f `basename $f`
					fi
				    done
				fi # ! -f Makefile.defs
			    elif test -f Makefile.defs; then
				# $loc_dir=0
				# Directory needs to be made.
				do_make=1

				# Don't step down. Remove Makefile, may
				# correspond to different platform
				rm Makefile > /dev/null 2>&1
				ln -s $makefile Makefile
			    fi # $loc_dir

			    if test "$do_make" -eq 1; then
				if test "$Verbose" -ne 0; then
				    echo "    make -r $make_opt $t $make_macro"
				fi # $Verbose

				make -r $make_opt $t $make_macro
			    fi

			    cd ..
			    if test "$loc_dir" -ne 0 -a "$do_make" -eq 1; then
				cd ..
			    fi
			fi # -d $sub
		    done # $sub
		fi # -n $subdirs

		# Only make a directory if the file Makefile.defs exists.
		do_make=0

		# Step into platform-dependent subdirectory if so requested
		if test "$loc_dir" -ne 0 -a -f Makefile.defs; then
		    # Directory needs to be made.
		    do_make=1

		    # Step into platform-dependent subdirectory
		    if test ! -d "$XITE_HOSTTYPE"; then
			mkdir $XITE_HOSTTYPE
		    fi
		    cd $XITE_HOSTTYPE

		    # Make sure the necessary links exist
		    if test ! -f Makefile.defs; then
			ln -s ../Makefile.defs Makefile.defs
			ln -s $makefile Makefile
			if test -d ../XPM; then
			    ln -s ../XPM XPM
			fi

			for f in ../*.[ch] ../*.sh ../*.3 ../*_3.html; do
			    if test -f $f; then
				ln -s $f `basename $f`
			    fi
			done
		    fi # ! -f Makefile.defs
		elif test -f Makefile.defs; then
		    # $loc_dir=0
		    # Directory needs to be made.
		    do_make=1

		    # Don't step down. Remove Makefile, may correspond to
		    # different platform
		    rm Makefile > /dev/null 2>&1
		    ln -s $makefile Makefile
		fi # $loc_dir

		if test "$do_make" -eq 1; then
		    if test "$Verbose" -ne 0; then
			echo "make -r $make_opt $t $make_macro"
		    fi # $Verbose

		    make -r $make_opt $t $make_macro
		fi # $do_make

		cd ..
		if test "$loc_dir" -ne 0 -a "$do_make" -eq 1; then
		    cd ..
		fi
	    fi # $isExc
	fi # -d $dir
    done # $dir

    if test "$t" = "doc"; then
    echo ""
    echo "You may want to check that the manual pages are not read protected."
    echo "Also, you may want to build a 'whatis' database for them."
    echo "Talk to your system manager if you don't understand this."
    fi # doc

done # targets

umask $mask
