#!/bin/sh
# Copyright (c) 1994 by Adobe Systems Incorporated.
#
# Shell script for integrating Photoshop with OpenWindow desktop.
# $Id: deskinstall,v 1.2 1994/03/30 16:06:36 achan Exp $
###################################################################

# Make sure openwin stuff is on user's path so ce_db_merge can be found
if [ "$OPENWINHOME" = "" ] ; then
	OPENWINHOME=/usr/openwin
fi
PATH=$PATH:$OPENWINHOME/bin
export PATH

LAUNCHSCRIPT=photoshop
OUR_CETABLE=psd.cetable
CETABLE_ORIG=orig.cetable.$$
CETABLE_FILE=new.cetable.$$
CHECKSTRING="<AdobePhotoshop-prog>"
FILE_ICON=Photoshopdoc.icon
APP_ICON=Photoshopprog.icon
UNAME=/bin/uname
ECHO=/usr/bin/echo
EGREP=/usr/bin/egrep
SED=/usr/bin/sed
IDPROG=/usr/bin/id
LS=/usr/bin/ls
EXPR=/usr/bin/expr
PWDPROG=/usr/bin/pwd
AWK=/usr/bin/awk
RM=/bin/rm
CAT=/usr/bin/cat
WHICH=/usr/bin/which
MKDIR="/usr/bin/mkdir -p "

# Things that are either platform-specific or potentially platform-specific.
osname="UNKNOWN"
osversion="UNKNOWN"
osmajorminor="UNKNOWN"
osmajor="UNKNOWN"
osminor="UNKNOWN"
if [ -x $UNAME ] ; then
	osname=`$UNAME -s`
	osversion=`$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/'`
	else
		osversion="UNKNOWN"
	fi
fi
if [ $osname = "SunOS" ] ; then
	if [ $osmajor -ge 5 ] ; then
		WHOAMI=`$IDPROG | $SED 's/^[^(]*.\([^)]*\).*/\1/'`
	else
		WHOAMI=`/usr/ucb/whoami`
		WHICH=/usr/ucb/which
	fi
fi

# Whether to install on user/system/network.
if [ "$WHOAMI" = "root" ] ; then
	mergetype=system
else
	mergetype=user
fi

# Process command line parameters.
while [ $# -gt 0 ] ; do

	if [ "$1" = "-user" ] ; then
		mergetype=user
		shift
	elif [ "$1" = "-system" ] ; then
		mergetype=system
		shift
	elif [ "$1" = "-network" ] ; then
		mergetype=network
		shift
	else
		break
	fi
done

# Try to determine the parent directory to this launch script.
thisscriptname=$0

# Follow any symbolic links in thisscriptname.
while : ; do
	lsresult=`$LS -l $thisscriptname`
	symlink=`$EXPR "$lsresult" : '.*>'.*`

	if [ "$symlink" -gt 0 ] ; then
		oldscriptname="$thisscriptname"
		thisscriptname=`$ECHO $lsresult|$SED -e 's/^.*-> *\(.*\) *$/\1/g'`
		abspath_new=`$EXPR "$thisscriptname" : '^/'`
		if [ $abspath_new != 1 ] ; then
			abspath_old=`$EXPR "$oldscriptname" : '^/'`
			if [ $abspath_old != 1 ] ; then
				olddir=`$PWDPROG`
			else
				olddir=`$EXPR "$oldscriptname" : '\(.*\)/'`
			fi
			thisscriptname="$olddir/$thisscriptname"
		fi
	else
		break
	fi
done

# Set CE_DIR based on $mergetype
if [ "$mergetype" = "user" ] ; then
	CE_DIR=$HOME/.cetables
elif [ "$mergetype" = "system" ] ; then
	CE_DIR=/etc/cetables
elif [ "$mergetype" = "network" ] ; then
	CE_DIR=$OPENWINHOME/include/images
else
	echo "Invalid mergetype: $mergetype. Exiting."
	exit 1
fi

# Determine directory containing this shell script.
scriptdir=`$EXPR $thisscriptname : '\(.*\)/'` 
if [ "$scriptdir" = "" -o "$scriptdir" = "." ] ; then
	scriptdir=`$PWDPROG`
fi

# Determine root directory for installation ($scriptdir/..).
parentdir=`$EXPR $scriptdir : '\(.*\)/'` 
if [ "$parentdir" = "" ] ; then
	tempdir=`$PWDPROG`
	parentdir=`$EXPR $tempdir : '\(.*\)/'`
fi

if [ ! -x "$parentdir/bin/$LAUNCHSCRIPT" ] ; then
	echo "Unable to determine $PROGNAME installation directory...exiting."
	exit 1
fi

# Make sure ce_db_build is on the user's path.
WHICHCHECK=`$WHICH ce_db_build`
firstword=`$ECHO $WHICHCHECK | $AWK '{ print $1; }'`
if [ "$firstword" = "no" ] ; then
	echo "Unable to find ce_db_build program...exiting."
	exit 1
fi

# Determine if database already exists.
if ce_db_build $mergetype -to_ascii /tmp/$CETABLE_ORIG 2> /dev/null ; then status=0; else status=1; fi
if [ "$status" = "1" ] ; then
	cmd=ce_db_build
else
	cmd=ce_db_merge
	if grep $CHECKSTRING /tmp/$CETABLE_ORIG > /dev/null 2> /dev/null ; then status=0; else status=1; fi
	if [ "$status" = "0" ] ; then
		echo ""
		echo "Icons appear to be already integrated into your desktop."
		echo ""
		echo "If you find that the icons do not appear or work properly,"
		echo "you might want to run binder to remove the old icons and"
		echo "then rerun $0 to install new icons."
		echo ""
		$RM /tmp/$CETABLE_ORIG
		exit 0
	fi
fi
$RM /tmp/$CETABLE_ORIG

# Make sure $cmd is on the user's path.
WHICHCHECK=`$WHICH $cmd`
firstword=`$ECHO $WHICHCHECK | $AWK '{ print $1; }'`
if [ "$firstword" = "no" ] ; then
	echo "Unable to find $cmd program...exiting."
	exit 1
fi

# Create directory, if necessary.
if [ ! -d "$CE_DIR" ] ; then
	if $MKDIR $CE_DIR ; then error=0; else error=1; fi
	if [ $error != 0  ] ; then
		echo ""
		echo ""
		echo "Cannot create directory $CE_DIR"
		echo "Desktop installation failed...exiting."
		exit 1
	fi
fi

# Copy icon files into the appropriate place.
if cp $scriptdir/$FILE_ICON $CE_DIR ; then status=0 ; else status=1; fi
if [ "$status" = "1" ] ; then
	echo "Unable to copy icon $FILE_ICON into $CE_DIR."
	echo "Desktop installation failed...exiting."
	exit 1
fi
if cp $scriptdir/$APP_ICON $CE_DIR ; then status=0 ; else status=1; fi
if [ "$status" = "1" ] ; then
	echo "Unable to copy icon $APP_ICON into $CE_DIR."
	echo "Desktop installation failed...exiting."
	exit 1
fi

# Copy cetable file to /tmp, replacing $ICONDIR with $CE_DIR.
$RM -f /tmp/$CETABLE_FILE
$CAT $scriptdir/$OUR_CETABLE | $SED 's%$ICONDIR%'$CE_DIR'%' > /tmp/$CETABLE_FILE

# Integrate into OpenWindows Desktop
if $cmd $mergetype -from_ascii /tmp/$CETABLE_FILE ; then status=0 ; else status=1; fi
if [ "$status" = "1" ] ; then
	echo ""
	echo "Command $cmd failed."
	echo "Installation onto the OpenWindows desktop failed...exiting."
	echo "You may need to install icons manually using the OpenWindows binder program."
	$RM /tmp/$CETABLE_FILE
	exit 1
fi

# remove temporary file
$RM /tmp/$CETABLE_FILE

echo "Integration with OpenWindows desktop completed."
exit 0
