#!/bin/sh
#
# Tue Aug 23 15:22:57 PDT 1994
#
#######################################################################
#
# This slave script performs, at most, the following major functions...
#
#   1) Modifies DPS NX Basic launch script
#
# Argument list:
# $1	INSTALLDIR, installation dir, somewhere under /usr/adobe
# $2	License file, not used
#
#######################################################################

INSTALLDIR="$1"

DEBUG=0
INSTALLED_DIR_NAME=DPSNXBasic
INSTALLED_MAJOR=2
INSTALLED_MINOR=1
INSTALLED_REVISION=1
INSTALLED_APP_VERSION=2.1

EXTRAS="/usr/local/bin:/usr/tekxp:/tekxp"

abort_install()
{
    echo "*** Installation cancelled ***"
    exit 1
}

trap abort_install 2

# Things that are either platform-specific or potentially platform-specific.
osname="UNKNOWN"
osversion="UNKNOWN"
osmajorminor="UNKNOWN"
osmajor="UNKNOWN"
osminor="UNKNOWN"
if [ -x /bin/uname ] ; then
	osname=`/bin/uname -s`
	# 64 bit IRIX6 returns IRIX64 for uname -s
        if [ `expr substr "$osname" 1 4` = "IRIX" ] ; then
                osname="IRIX"
	fi
	osversion=`/bin/uname -r`
	check=`echo "$osversion" | egrep '^[0-9]\.[0-9]*'`
	if [ "$check" != "" ] ; then
		# osmajorminor will be like "4.2". osmajor: "4". osminor:"2".
		osmajorminor=`echo "$osversion" | sed -e 's/^\([0-9]\.[0-9]\).*/\1/'`
		osmajor=`echo "$osversion" | sed -e 's/^\([0-9]\).*/\1/'`
		osminor=`echo "$osversion" | sed -e 's/^[0-9]\.\([0-9]\).*/\1/'`
	fi
fi
if [ $osname = "SunOS" ] ; then
	STDBINDIR="/usr/bin"
	if [ $osmajor -ge 5 ] ; then
		# For echo without newline under Sys V, set EB and EE variables
		EB=""
		EE="\c"
		PATH=/usr/sbin:/usr/bin:/bin:/usr/bin/X11:/usr/openwin/bin:/sbin:/usr/sbin:/etc:/usr/etc:"$EXTRAS"
	else
		# For echo without newline under BSD, set EB and EE variables
		EB="-n "
		EE=""
		PATH=/usr/ucb:/usr/bin:/bin:/usr/bin/X11:/usr/openwin/bin:/sbin:/etc:/usr/etc:"$EXTRAS"
	fi
	CURDATE=`/usr/bin/date -u`
	AWK="$STDBINDIR/awk"


elif [ $osname = "IRIX" ] ; then
	STDBINDIR="/usr/bin"
	# For echo without newline under Sys V, set EB and EE variables
	EB=""
	EE="\c"
	PATH=/usr/sbin:/sbin:/usr/bin:/bin:/usr/bsd:/usr/bin/X11:/usr/openwin/bin:/etc:/usr/etc:"$EXTRAS"
	CURDATE=`/bin/date -u`
	AWK="$STDBINDIR/awk"

elif [ $osname = "HP-UX" ] ; then
	STDBINDIR="/bin"
	# For echo without newline under Sys V, set EB and EE variables
	EB=""
	EE="\c"
	PATH=/usr/sbin:/sbin:/usr/bin:/bin:/usr/bin/X11:/etc:/usr/etc:"$EXTRAS"
	CURDATE=`/bin/date -u`
	AWK="/usr/bin/awk"
fi
if [ "$osname" = "UNKNOWN" -o "$osversion" = "UNKNOWN" ] ; then
	echo ""
	echo "Unrecognized operating system: $osname $osversion."
	echo "Cannot install: $DISTRIB_DESC"
	eval ${ABORTCMD}
fi

export PATH

# The following commands MUST be defined properly
MKDIR="$STDBINDIR/mkdir -p "
RMDIR="$STDBINDIR/rmdir "
CP="$STDBINDIR/cp"
LN="$STDBINDIR/ln -s"
RM="$STDBINDIR/rm"
MV="$STDBINDIR/mv"
CAT="$STDBINDIR/cat"
SED="$STDBINDIR/sed"
EXPR="$STDBINDIR/expr"
PWDCMD="$STDBINDIR/pwd"
DIRCP="$STDBINDIR/cp -pr"
DIRRM="$STDBINDIR/rm -rf"
LS="$STDBINDIR/ls"
EGREP="$STDBINDIR/egrep"
CHMOD="$STDBINDIR/chmod"
CD="cd"
E="$STDBINDIR/echo"
EN="$STDBINDIR/echo"

#######################################################################
#
# Global script configuration options
#
# TMP		Temporary file directory
# TMPFILE	Temporary file
# XMODE		Permissions for executable programs (chmod)
# RMODE		Permissions for read-only documentation (chmod)
# USERFILE	File name for customized user information
#
#######################################################################

TMP=/tmp
TMPFILE=/tmp/foo.$$
XMODE="555"
RMODE="444"
USERFILE="user.adobe"
APP_DPSNX="$INSTALLED_DIR_NAME"_"$INSTALLED_APP_VERSION"

#
# Simple macro functions
#

fix_path()
{
    dir="$1"
    if [ ! -d "$dir" ] ; then
    	return 1
    fi

    # Undo automounter stuff
    ORIGDIR=`($CD $dir ; /bin/pwd)`
    TESTDIR=`expr $ORIGDIR : '/[^/]*\(.*\)'`
    HEAD=`expr $ORIGDIR : '\(/[^/]*\)'/`
    
    if [ -z "$HEAD" -o -z "$TESTDIR" ] ; then
        if [ `expr $ORIGDIR : '/[/]*[^/]*$'` -gt 0 ] ; then
	    FIXED_PATH="$ORIGDIR"
	    return 0
	fi
	return 1
    fi
    
    if [ ! -d "$ORIGDIR" ] ; then
        return 1
    fi
    
    if [ ! -z "$TESTDIR" -a ! -d "$TESTDIR" ] ; then
        # definitely not automounted
	FIXED_PATH="$ORIGDIR"
	return 0
    fi
 
    if [ `cd $ORIGDIR && /bin/pwd` = `cd $TESTDIR && /bin/pwd` ] ; then
        # definitely automounted
	FIXED_PATH="$TESTDIR"
	return 0
    fi
    
    # definitely not automounted
    FIXED_PATH="$ORIGDIR"
    return 0
}

warn()
{
	$E ${1+"$@"} 1>&${_errfd-2}
	return ${_errstat:-1}
}

err()
{
	warn ${1+"$@"}
	exit 1
}

check_dir()
{
	test -d "$1" || warn "$1 does not exist"
}

write_dir()
{
	test -w "$1" || warn "$1 is not writeable"
}

debugging()
{
    test $DEBUG -eq 1
    return $?
}

debugging && $E "INSTALLDIR="$INSTALLDIR
#
# Initial path finding
#

if fix_path $INSTALLDIR ; then
    TOPDIR=$FIXED_PATH
else
    err "ERROR: can't figure out where to get $APP_DPSNX"
fi

#
# Check for existing installation, determine if upgrade needed
#

OLD_SCRIPT=dpsnx.agent
if [ -d "$TOPDIR/bin" ] ; then
    OLD_BIN=$TOPDIR/bin
elif [ -f /usr/bin/$OLD_SCRIPT ] ; then
    OLD_BIN=/usr/bin
elif [ -f /bin/$OLD_SCRIPT ] ; then
    OLD_BIN=/bin
elif [ -f /usr/local/bin/$OLD_SCRIPT ] ; then
    OLD_BIN=/usr/local/bin
fi

if [ ${OLD_BIN:-"None"} = "None" ] ; then
    # Try to find it on the path (yeah, right!)
    USING="`(type $OLD_SCRIPT | $EGREP ' is '| $AWK '{print $3}')`"
    if [ -z "$USING" ] ; then
        # Not on path, leave OLD_BIN undefined
	true ;
    else
        OLD_BIN = "`expr $USING : '\(.*\)/dpsnx.agent'`"
    fi
fi

debugging && $E OLD_BIN="$OLD_BIN"

if [ ${OLD_BIN:-"None"} != "None" ] ; then
    # Grep for version numbers
    OLD=$OLD_BIN/$OLD_SCRIPT
    eval `$EGREP MAJOR_VERSION $OLD`
    eval `$EGREP MINOR_VERSION $OLD`
    debugging && $E MAJOR_VERSION="$MAJOR_VERSION"
    debugging && $E MINOR_VERSION="$MINOR_VERSION"
    if [ ${MAJOR_VERSION:-0} -lt $INSTALLED_MAJOR -o \
         \( ${MAJOR_VERSION:-0} -eq $INSTALLED_MAJOR -a ${MINOR_VERSION:-0} -lt $INSTALLED_MINOR \) ] ; then
	# Either not a script, or really old version
	debugging && $E "REALLY OLD SCRIPT $OLD"
	# Check for tekxp
	eval `$EGREP LOCAL_BIN $OLD` 
	if [ ${LOCAL_BIN:-"None"} != "None" ] ; then
	    $E "*** During installation of $APP_DPSNX, found a Tektronix"
	    $E "*** XpressWare installation of dpsnx.agent on your path."
	    $E "*** You will have to choose between the Tektronix agent"
	    $E "*** and the one installed with this product by altering"
	    $E "*** your path."
	fi
	# Move it
	$RM -f "$OLD".old
	$MV $OLD "$OLD".old
    elif [ ${MAJOR_VERSION:-0} -gt $INSTALLED_MAJOR -o \
           \( ${MAJOR_VERSION:-0} -eq $INSTALLED_MAJOR -a ${MINOR_VERSION:-0} -ge $INSTALLED_MINOR \) ] ; then
	fixedalready="`$EGREP FilledInByInstall $OLD`"
	if [ -z "$fixedalready" ] ; then
            $E "*** DPS NX Release $MAJOR_VERSION"."$MINOR_VERSION is already installed."
	    $E "*** Skipping installation of DPS NX Release $INSTALLED_MAJOR"."$INSTALLED_MINOR"
	    exit 0
	fi
    fi
fi
   
DPSNXDIR_ROOT=$TOPDIR
DPSNXDIR_BIN=$DPSNXDIR_ROOT/bin
DPSNXDIR_EXEC=$DPSNXDIR_ROOT/exec
DPSNXDIR_PSRES=$DPSNXDIR_ROOT/psres

#
# Copy files
#

#$CP -p $DISTDIR/readme.nxb $DPSNXDIR_ROOT/README

#
# Customize agent launch script
#

valid_orig()
{
    FNAME="$1"
    ROM="$2"
    DASD="$3"
    FORIG="$FNAME".orig
    if [ ! -f $FORIG ] ; then
	$MV $FNAME $FORIG
	$CHMOD $ROM $FORIG
    fi
    if [ -f $FNAME -a -f $FORIG ] ; then
	$CHMOD $DASD $FNAME
	$RM $FNAME
    fi
}

debugging && $E "*** Customizing launch script (dpsnx.agent) for DPS NX ..."

$CHMOD 755 $DPSNXDIR_BIN

valid_orig "$DPSNXDIR_BIN/dpsnx.agent" 555 755
$SED 's%INSTALL_DIR=FilledInByInstall$%INSTALL_DIR="'$DPSNXDIR_ROOT'"%' \
< $DPSNXDIR_BIN/dpsnx.agent.orig \
| $SED 's%LAST_INSTALL_DATE=.*$%LAST_INSTALL_DATE="'"$CURDATE"'"%' \
| $SED 's%DEFAULT_PSRES=.*$%DEFAULT_PSRES="'"$DPSNXDIR_PSRES"'"%' \
| $AWK '/FilledInByInstall/ { next; } { print; }'  \
> $DPSNXDIR_BIN/dpsnx.agent
$CHMOD 755 $DPSNXDIR_BIN/dpsnx.agent


debugging && $E "*** Done ***"

exit 0

