#!/bin/sh
# $Id: ginstall,v 1.4 89/11/06 16:58:07 chrisb Exp $

#------------------------------------------------------------------------------
#	INSTALL (WITH tar) THE GNU/960 TOOLS AND/OR NINDY.
#
# Gives help message if invoked without arguments.
#
# The user is given the ability to load selectively.
#
# WARNING:
#	Many assumptions about archived directory structure and supported hosts
#	hard-wired in.  Will need update if these change.
#------------------------------------------------------------------------------

PATH=/bin:/usr/bin:/etc		# sysV has tar on /etc (at least, ours did)
export PATH
umask 002
MYNAME=`basename $0`
BASE=`pwd`

if test $# = 0 ; then
	echo "Usage: $MYNAME [-f filename] items"
	cat <<!EOF!

	[-f filename]
		Optional name of tarfile.  If omitted, tar will read from its
		default tape device.

	items	One or more items to be loaded from the tape, from this list:
		    bin 	 Tools for *all* hosts.
		      i386v	 Tools for i386, running system V.
		      sun3	 Tools for sun3, running OS4.
		      sun386i	 Tools for sun386i, running OS4.
		      sun4	 Tools for sun4, running OS4.
		      vax-bsd	 Tools for VAX running BSD Unix.
		      vax-ultrix Tools for VAX running Ultrix.
		    src		 *All* of the source code.
		      libsrc	 Source code for libraries.
		      toolsrc	 Source code for tools and libraries.
		      nindysrc	 Source code for NINDY ROM images.
		    rom		 NINDY ROM images.
		    all		 The whole shooting match.

	The requested items will be loaded into the current working directory,
	which will be the tools base directory.  The 'admin' subdirectory must
	already be present (tar'd off the tape).
!EOF!
	exit 1
fi

if test ! -d admin ; then
	echo "$MYNAME: you must be in the tools base directory to run"
	exit 1
fi

#------------------------------------------------------------------------------
# Make sure directory is writable,
# determine types of links that work on this system.
#------------------------------------------------------------------------------
if touch foo$$; then
	if ln -s foo$$ bar$$ >/dev/null 2>&1 ; then
		LN="ln -s"
	elif ln foo$$ bar$$ >/dev/null 2>&1 ; then
		LN="ln"
	else
		LN="cp"
	fi
	rm -f foo$$ bar$$
else
	echo "$MYNAME: you don't have write permission here!"
	exit 1;
fi

#------------------------------------------------------------------------------
# Process arguments
#------------------------------------------------------------------------------
TARFLAGS=xvo
DEV=
DIR=
BINLIST=
SRCLIST=
ROMLIST=
ALL_HOSTS="i386v sun3 sun386i sun4 vax-bsd vax-ultrix"
while test $# != 0 ; do
	case "$1" in
	-f)
		TARFLAGS="$TARFLAGS"f
		shift
		DEV=$1
		;;
	rom)		ROMLIST=ROMS					;;
	bin)	 	BINLIST=$ALL_HOSTS				;;
	i386v)		BINLIST="$BINLIST i386v"			;;
	sun3)		BINLIST="$BINLIST sun3"				;;
	sun386i)	BINLIST="$BINLIST sun386i"			;;
	sun4)		BINLIST="$BINLIST sun4"				;;
	vax-bsd)	BINLIST="$BINLIST vax-bsd"			;;
	vax-ultrix)	BINLIST="$BINLIST vax-ultrix"			;;
	src)		SRCLIST=src					;;
	libsrc)		SRCLIST="$SRCLIST src/lib src/include"		;;
	toolsrc)	SRCLIST=src					;;
	nindysrc)	SRCLIST="$SRCLIST src/nindy src/include"	;;
	all)	 
		ROMLIST=ROMS
		BINLIST=$ALL_HOSTS
		SRCLIST=src
		;;
	*)
		echo "$MYNAME: bad argument: '$1'"
		echo "Enter '$MYNAME' without arguments to get usage info"
		exit 1
		;;
	esac;

	shift
done

if test "x$ROMLIST" = x -a "x$BINLIST" = x -a "x$SRCLIST" = x ; then
	echo "Enter '$MYNAME' without arguments for usage info"
	exit 1
fi

#------------------------------------------------------------------------------
# Read from tar file/tape.
#------------------------------------------------------------------------------
echo tar $TARFLAGS $DEV doc include $ROMLIST $BINLIST $SRCLIST
tar $TARFLAGS $DEV doc include $ROMLIST $BINLIST $SRCLIST
if test $? != 0 ; then
	exit 1
fi

#------------------------------------------------------------------------------
# If gcc source got installed, make sure the files generated by 'bison' (the
# GNU version of 'yacc') are newer than the corresponding ".y" files -- we
# don't want the recipient to have to build 'bison' unless they specifically
# want to modify the front end of gcc (which should never be necessary).
#------------------------------------------------------------------------------
sleep 1
for i in c-parse.tab.c cexp.c ; do
	if test -f src/gcc960/common/$i ; then
		touch src/gcc960/common/$i
	fi
done


#------------------------------------------------------------------------------
# The rest of the script only gets executed if host-specific binary
# directories were installed.  All of them share the same include and
# man directories, via links (symbolic, hard, or copies, as appropriate).
#------------------------------------------------------------------------------
if test "x$BINLIST" = x ; then
	exit 0
fi

#------------------------------------------------------------------------------
# Build links to man and include files.
#------------------------------------------------------------------------------
if test "$LN" = "ln -s" ; then
	for h in $BINLIST; do
		if test -d $BASE/$h ; then
			cd $BASE/$h && $LN ../doc/man && $LN ../include
		fi
	done
else
	for h in $BINLIST; do
		if test -d $BASE/$h ; then

			cd $BASE/$h
			mkdir man include include/sys
			if test "$?" != 0 ; then
				continue
			fi

			cd $BASE/$h/man
			for f in $BASE/doc/man/*; do
				if test ! -d $f ; then
					$LN $f .
				fi
			done

			cd $BASE/$h/include 
			for f in $BASE/include/*.h; do
				if test ! -d $f ; then
					$LN $f .
				fi
			done

			cd $BASE/$h/include/sys
			for f in $BASE/include/sys/*.h; do
				if test ! -d $f ; then
					$LN $f .
				fi
			done
		fi
	done
fi
