#!/bin/sh
#
#
# Copyright 1997 Xi Graphics
#
# Install - bootstrap install something.
#
# $XiGId: Install,v 1.7 1998/02/18 22:24:20 jon Exp $
#
#########################################################################
# set -x			# uncomment for debuging

# remove stuff that will probably only confuse bash.
unset ENV
unset START	

### Off we go.  We can't be fancy since we can't assume that we're running
###  on something reasonably smart like ksh or bash.

### Some vars

if [ -z "$INSTALL_PATH" ]
then
	PATH=/bin:/usr/bin:/sbin:/usr/sbin:/etc
else
	PATH="$INSTALL_PATH"
fi

if [ -z "$INSTALL_BASE" ]
then	# not set
	INSTALL_BASE=`pwd`		# where are we?
fi

INSTALL_PROG=""				# the real guts (bash-style)

if [ -z "$INSTALL_ROOT" ]
then # it's not set...
	INSTALL_ROOT="/"		# used by scripts to base file refs of off
fi


INSTALL_MODE="install"                  # installing
unset SYSTYPE
SYSTYPE=""				# Type of OS
SYSBASE=""				# = $INSTALL_BASE/$SYSTYPE

AUTO_INSTALL="$*"			# packages to automatically install
					#  without seeing start screens

### Get the product info file

if [ ! -r "$INSTALL_BASE/install/PRODINFO" ]
then
	echo "Cannot open Product information file: $INSTALL_BASE/install/PRODINFO"
	exit 1
else
	. $INSTALL_BASE/install/PRODINFO
fi

# messages
INST_ABORT="### Installation of $PRODUCT_NAME aborted."

### Now some checks.  We must be root for starters

if [ -z "$AUTO_INSTALL" ]
then
    echo "### Checking environment, please wait..."
fi

# make sure we're root

if id |grep 'uid=0'>/dev/null 2>&1
then
	:
else
        echo "ERROR: This program must be run as root."
	echo "$INST_ABORT"

        exit 1
fi

### Check platform - see if we have it...
# get the OS type

SYSTYPE="`uname -s`"
export SYSTYPE

if [ -z "$SYSTYPE" ]
then	# Eh?
	echo "ERROR: Couldn't determine System type with 'uname -s'"
	echo "$INST_ABORT"
	
	exit 1
fi

# Check to see if this is BSD/OS, if so - rename to BSD_OS for obvious reasons.

if [ "$SYSTYPE" = 'BSD/OS' ]
then
	SYSTYPE="BSD_OS"
fi

# See if the platform returned exists on this media

if [ ! -d $INSTALL_BASE/$SYSTYPE ]
then		# SYSTYPE not on this dstribution.
	echo "ERROR: $PRODUCT_NAME for '$SYSTYPE' is not on this distribution."
	echo "$INST_ABORT"

	exit 1
fi

if [ -z "$AUTO_INSTALL" ]
then
    echo "### System Type: $SYSTYPE"
fi

SYSBASE="$INSTALL_BASE/$SYSTYPE"

					# default 'C' catalog file
DEFCATFILE="$SYSBASE/locale/C/Install.cat"

### find a catalog based on LANG, though we won't use it till we get to
###  the main installation script...

if [ ! -z "$LANG" ]
then
    CATFILE="$SYSBASE/locale/$LANG/Install.cat"

    if [ ! -r "$CATFILE" ]
    then  # we'll have to use the default C locale
	echo "### Catfile: $CATFILE doesn't exist. Defaulting to $DEFCATFILE"
	CATFILE="$DEFCATFILE"
    fi

else # default to C
    CATFILE="$DEFCATFILE"
    LANG="C"
fi

### Lets find the catgets program for this OS...
CATGETS="$SYSBASE/bin/catgets"

if [ ! -x "$CATGETS" ]
then
    echo "$CATGETS not found"
    echo "$INST_ABORT"
fi

### We use Set 1 for Install messages
###  Request/pre/postinstall scripts will each use 1 set per package 
###  starting at 10.
CATSET="1"

INSTALLSH="$SYSBASE/bin/installsh"

if [ ! -x "$INSTALLSH" ]
then
	echo "ERROR: Can't locate $INSTALLSH"
	echo "$INST_ABORT"

	exit 1
fi

# let's test it to make sure everythings ok...
# we want to see any stderr if there's a problem here.

$INSTALLSH -c "echo M5. This unit must survive." >/dev/null

if [ $? != 0 ]
then		# something bad happened
	echo "ERROR: Execution test of $INSTALLSH failed."
	echo "$INST_ABORT"

	exit 1
fi

### Now build the INSTALL_PROG and make sure it's there

INSTALL_PROG="$INSTALL_BASE/install/install.bsh"

if [ ! -f "$INSTALL_PROG" ]
then			# a no show
	echo "ERROR: Can't find install script: $INSTALL_PROG"
	echo "$INST_ABORT"

	exit 1
fi

### Now lets increase our IQ a bit

# export some stuff

GOOD_EXEC=1

export GOOD_EXEC SYSTYPE SYSBASE INSTALL_BASE INSTALL_PROG INSTALLSH PATH TARGET_BASE 
export INSTALL_ROOT INSTALL_MODE
export LANG CATFILE CATSET CATGETS
export AUTO_INSTALL
exec $INSTALLSH "$INSTALL_PROG"

### if we're here, something still died unexpectedly despite my best efforts.

echo "FATAL: exec $INSTALLSH $INSTALL_PROG failed, status $?"
echo "$INST_ABORT"

exit 1

