#!/bin/sh
#
#
# Copyright 1997 Xi Graphics
#
# Uninstall - bootstrap Uninstall something.
#
# This is basically a copy of Install.  It will call install/install.bsh
#  to do the work...
#
# $XiGId: Uninstall,v 1.4 1998/02/02 20:45:19 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.

PATH=/bin:/usr/bin:/sbin:/usr/sbin:/etc

### Some vars

INSTALL_BASE=`pwd`			# where are we?
INSTALL_PROG=""				# the real guts (bash-style)
INSTALL_ROOT="/"			# used by scripts to base file refs of off
INSTALL_MODE="uninstall"                # flag to indicate pkg removal
SYSTYPE=""				# Type of OS
SYSBASE=""				# = $INSTALL_BASE/$SYSTYPE


### 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="### Deinstallation of $PRODUCT_NAME aborted."

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

echo "### Checking environment, please wait..."

# 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`

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

echo "### System Type: $SYSTYPE"

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

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

