#!/bin/sh
#
# installTreeps - install the treeps application
#
# SYNOPSIS: 
#      installTreeps [-d] [-c colorDepthSuffix] [-r installRoot] [-p packagingPrefix] 
#
#      colorDepthSuffix is one of [bw,loColor,hiColor]

INSTALL_ROOT=
PREFIX=

DESKTOP_ONLY=


# Set the default color depth, this is the suffix appended to the resource file
# i.e.  to pick one of Treeps.bw, Treeps.loColor, Treeps.hiColor 


	    
while [ "$#" -gt "0" ]
do
        case $1 in

            -d ) DESKTOP_ONLY="Y" ; shift ;;

            -r ) INSTALL_ROOT="$2" ; shift ; shift  ;;

            -p ) PREFIX="$2" ; shift ; shift ;;

            -c ) COLOR_DEPTH="$2" ; shift ; shift ;;

             * ) break ;;
        esac
done

if [ "$PREFIX" != "" ]
then
	if echo "$PREFIX" | grep "^/" > /dev/null
	then
		PREFIX="$PREFIX"
	else
		PREFIX="/$PREFIX"
	fi
fi

PREFIX_PATH="$INSTALL_ROOT$PREFIX"

if [ "$PREFIX_PATH" != "" ]
then
	if [ ! -d "$PREFIX_PATH" ]
	then
		mkdir -p "$PREFIX_PATH"
		if [ $? != 0 ]
		then
			echo "Can't create install area -> $PREFIX_PATH"
			echo "Install aborted"

			exit 3
	    	fi
	fi

	# Make sure we can write to the install area

	mkdir -p "$PREFIX_PATH/usr"
	if [ $? != 0 ]
	then
		echo "Can't write to install area -> $PREFIX_PATH"
		echo "Install aborted"
		exit 4
	else
		rm -f $PREFIX_PATH/installArea
	fi

fi

export PREFIX INSTALL_ROOT

if [ "$#" != "0" ]
then
	exit 1
fi

OS=`uname`

if [ "$OS" = "SunOS" ]
then
    # Need whoami, install ...
    PATH=/usr/ucb:$PATH
fi

. ./scripts/generic/setPath


RES_PATH=`getXAppLibPath`
MAN_PATH=`getInstallMan`

if [ -z "$RESDIR" ]
then
    RESDIR="$RES_PATH/app-defaults"
    export RESDIR
fi

if [ -z "$BINDIR" ]
then
    BINDIR="`getInstallBin`"
    export BINDIR
fi

if [ -z "$LIBDIR" ]
then
    LIBDIR="`getInstallLib`"
    export LIBDIR
fi

if [ -z "$MANDIR" ]
then
    MANDIR="$MAN_PATH/man1"
    export MANDIR
fi

if [ -z "$INSTALL_SETUID_ROOT" ]
then
    INSTALL_SETUID_ROOT="Y"
fi

if [ -z "$MUST_INSTALL_AS_ROOT" ]
then
	MUST_INSTALL_AS_ROOT="YES"
fi


if [ "$MUST_INSTALL_AS_ROOT" = "YES" ]
then
	if [ `getUserId` != "root" ] 
	then 
        	echo ""
        	echo "This package was built to be installed as root, thus you need to"
        	echo ""
        	echo "       su root; make install"
        	echo ""
        	echo "To complete the install."
        	echo ""
        	echo "If you don't want to(or can't) install as root then try:"
        	echo ""
        	echo "       make clobber"
        	echo "       make local     or    make personal"
        	echo "       make install"
        	echo ""
        	echo "Look at Install.params"
        	exit
	fi
fi

    SAVE_LOG=`getInstallLog`
    LOG=`pwd`
    LOG="$LOG/installLog"
    export LOG

    if [ -f "$LOG" ]
    then
        if [ ! -w "$LOG" ]
        then
            echo ""
            echo "Can't create log file - $LOG"
            echo "File exists but cannot be written to. Install aborted."
            echo ""
    	    exit
	fi
    fi
    
    if [ "$DESKTOP_ONLY" != "Y" ]
    then
	
	if [ "$MUST_INSTALL_AS_ROOT" = "YES" ]
	then
        	out "Doing Root install" 
	fi

	# do we need to do an uninstall?

        if [ -f "$INSTALL_ROOT$PREFIX$LIBDIR/uninstall" ]
        then
            answer="n"
	    echo 
    	    echo "WARNING!!! A version of treeps is already installed. WARNING!!!"
	    echo 
	    echo -n "Do you wish to uninstall the older treeps? (y/n) "

	    read answer

	    if [ "$answer" != "y" ]
	    then
	        echo 
	        echo "The old version of treeps needs to be removed before a new one can"
	        echo "be installed properly."
	        echo 

	        exit 1
	    fi

	    out "Running treeps uninstaller in 4 seconds"
	    sleep 4

	    ( cd $INSTALL_ROOT$PREFIX$LIBDIR ; ./uninstall -f )

	    echo
        fi


        # out "Checking for sufficient space" 

	# Should check list of install areas for permission before starting

	# Should check for enough space before starting

	# perform customization of resource file


	>$LOG

	PACKAGE_OWNER=`getUserId`
	if [ ! -z "$PACKAGE_OWNER" ]
	then
		installOwner $PACKAGE_OWNER
	fi

        if [ "$MUST_INSTALL_AS_ROOT" = "YES" ]
        then
            installFile -m 755 -o root 	$OS/treeps      $BINDIR/treeps 
	else
            installFile -m 755 	$OS/treeps              $BINDIR/treeps 
	fi

    	if [ "$INSTALL_SETUID_ROOT" = "Y" ]
	then
            chmod u+s                                   $PREFIX_PATH$BINDIR/treeps
	fi

        installFile -m 644         	doc/treeps.1    $MANDIR/treeps.1 

	# Get screen depth

        getXScreenDepth    # puts result in /tmp/screen_depth

        depth=`cat /tmp/screen_depth.treeps.tmp`
    
        COLOR_DEPTH=hiColor   # default

        if [ "$depth" = "1" ]
        then
	    COLOR_DEPTH=bw
        else 
	    if [ $depth -lt 9 ]
	    then
		COLOR_DEPTH=loColor 
	    else
		COLOR_DEPTH=hiColor 
	    fi
        fi

        rm -f /tmp/screen_depth.treeps.tmp

#	Install Resource file(s)
#
# 	The most specific X resource file is copied to the X resource directory
#
#	X Resource dirs will either have one Treeps file, or 3 files, one each
#	for the different color models supported bw/loColor/hiColor. If a OS
#	release specific resource can be found it is used.
	
	if [ -d Res/$OS ]
	then
	    if [ -f /etc/redhat-release ]
	    then
#		Do some linking magic so the code below finds a sensible OS Version directory

		cd Res/$OS
        	RedHat_Distro=`cat /etc/redhat-release | sed 's/.*release //' | awk '{print $1}'`

        	if [ -d ./RedHat/$RedHat_Distro  ]
        	then
                    if [ ! -s $OS_VERSION ]
		    then
                        ln -s ./RedHat/$RedHat_Distro ./$OS_VERSION
		    fi
        	else
            	    echo "Warning: Can't find specific RedHat distribution X resource directory."
                    echo "Warning: Using the latest available as the default, you may want to"
                    echo "Warning: edit the X resource file after installation is complete."

            	    ln -s ./RedHat/Latest $OS_VERSION

		    echo "Will continue install in 3 secs"
		    sleep 3
        	fi
		cd ../..
	    fi

	    # Hard code some values when building RPM

 	    if [ -f "/tmp/make_treeps_rpm" ]
 	    then
 		OS_VERSION="6.1"
 		COLOR_DEPTH="hiColor"
	    fi

	    if [ -d Res/$OS/$OS_VERSION ]
	    then
                installXResource  Res/$OS/$OS_VERSION/Treeps.bw       $RESDIR/Treeps.bw 
                installXResource  Res/$OS/$OS_VERSION/Treeps.loColor  $RESDIR/Treeps.loColor
                installXResource  Res/$OS/$OS_VERSION/Treeps.hiColor  $RESDIR/Treeps.hiColor
                installXResource  Res/$OS/$OS_VERSION/Treeps          $RESDIR/Treeps
	    else
	        if [ -d Res/$OS/Latest ]
	        then
                    installXResource  Res/$OS/Latest/Treeps.bw       $RESDIR/Treeps.bw 
                    installXResource  Res/$OS/Latest/Treeps.loColor  $RESDIR/Treeps.loColor 
                    installXResource  Res/$OS/Latest/Treeps.hiColor  $RESDIR/Treeps.hiColor 
                    installXResource  Res/$OS/Latest/Treeps          $RESDIR/Treeps
	        else
                    installXResource  Res/$OS/Treeps.bw       $RESDIR/Treeps.bw 
                    installXResource  Res/$OS/Treeps.loColor  $RESDIR/Treeps.loColor 
                    installXResource  Res/$OS/Treeps.hiColor  $RESDIR/Treeps.hiColor 
                    installXResource  Res/$OS/Treeps          $RESDIR/Treeps
	        fi
	    fi
	else
	     installXResource     Res/Treeps.bw       $RESDIR/Treeps.bw
	     installXResource     Res/Treeps.loColor  $RESDIR/Treeps.loColor
	     installXResource     Res/Treeps.hiColor  $RESDIR/Treeps.hiColor
	fi

	if [ -f "$PREFIX_PATH$RESDIR/Treeps.$COLOR_DEPTH" ]
	then
	     if [ -f "$PREFIX_PATH$RESDIR/Treeps" ]
	     then
	     	mv -f  $PREFIX_PATH$RESDIR/Treeps $PREFIX_PATH$RESDIR/Treeps.last
	     fi

	     #installLink $PRE_PATH$RESDIR/Treeps.$COLOR_DEPTH $RESDIR/Treeps

	     installRelLink $RESDIR Treeps.$COLOR_DEPTH Treeps
	fi


        installDir $LIBDIR

	# Set file ownerships for the installed tree's, i.e. root for
	# root installs, users id for other installs.

	FILE_OWNER=`getUserId`
	FILE_GROUP=`getGroupId`

	export FILE_OWNER FILE_GROUP

        out "Installing Program Icons" 

        installTree 			icons   	$LIBDIR

        out "Installing Scripts" 

        installTree 			scripts 	$LIBDIR

	if [ -z "$LOCAL_BIN" ]
	then
		LOCAL_BIN=/usr/local/bin
	fi

        out "Installing External Application Starter" 

	SILP="sysInfoLaunchPad"

	if [ -f $SILP/$SILP ]
	then
	    installFile $SILP/$SILP $LOCAL_BIN/$SILP
	    installFile $SILP/textStreamViewer $LOCAL_BIN/textStreamViewer
            installXResource  Res/SysInfoLaunchPad $RESDIR/SysInfoLaunchPad
            installXResource  Res/TextStreamViewer $RESDIR/TextStreamViewer
	fi

	TEAS="tpsExtAppStarter"

	installFile scripts/generic/$TEAS $LOCAL_BIN/$TEAS

        out "Installing Desktop Files" 

        installTree 			desktop 	$LIBDIR

	installFile ./scripts/generic/uninstallTreeps   $LIBDIR/uninstall

	# Install Setup file for later use by users/scripts to install desktops

	installFile Setup	       $LIBDIR/Setup

	installFile Install.defaults   $LIBDIR/Install.defaults
	installFile Install.params     $LIBDIR/Install.params

	installPrefixes $LIBDIR
    fi

    if [ "$MUST_INSTALL_AS_ROOT" = "YES" ]
    then

    	installDesktop

	out "Note: You may need to restart your desktop to get the treeps menu item."

    else
    	echo 
    	echo "A number of desktop integration steps require root access to perform,"
    	echo "these were skipped. You can do them later by becomming the root user"
    	echo "and performing ./Setup -d from $INSTALL_ROOT$PREFIX$LIBDIR ."
    fi

    if [ "$LOG" != "$SAVE_LOG" ]
    then
        cat $LOG >> $SAVE_LOG
    fi


