#!/bin/sh
# @(#)wabi	2.159   04 Dec 1995
#
#	wabi:	Wabi startup script.
#
#		Set WABIDIR env var, $HOME/wabi is the default.
#
#		Create WABIDIR if necessary.
#
#		Create WABIDIR/fc if necessary.
#
#		Invoke wabiprog.
#
#	Copyright (c) 1991-95, Sun Microsystems, Inc.  All Rights Reserved
#	Sun considers its source code as an unpublished, proprietary
#	trade secret, and it is available only under strict licens
#	provisions.  This copyright notice is placed here only to protect
#	Sun in the event the source is deemed a published work.  Dissassembly,
#	decompilation, or other means of reducing the object code to human
#	readable form is prohibited by the license agreement under which
#	this code is provided to the user or company in possession of this
#	copy.
# 
#	RESTRICTED RIGHTS LEGEND: Use, duplication, or disclosure by the
#	Government is subject to restrictions as set forth in subparagraph
#	(c)(1)(ii) of the Rights in Technical Data and Computer Software
#	clause at DFARS 52.227-7013 and in similar clauses in the FAR and
#	NASA FAR Supplement.

__WABI_ORIG_PATH=$PATH
PATH=/usr/bin:/bin:$PATH
export PATH

# Definitions
FILE_CHECK=/etc/.vold.wabi
WABIVERSION=2.1
WABIFCS=0		# 1 for FCS, otherwise 0


chase_symlink()
{

#   To check for a symlink, use the test "-h" switch for all platforms 
#   except AIX, which only supports the "-L" switch. 
 
    if [ "`$UNAME`" = "AIX" ] 
    then 
	TEST_SYMLINK="-L" 
    else
	TEST_SYMLINK="-h" 
    fi

#   is the command a symlink?

    if [ $TEST_SYMLINK "$COMMAND" ]
    then

        FILE="/bin/file -h"

#	strip off the uninteresting stuff, leaving the path to the real file
	NEW_PATH=`$FILE $COMMAND|/bin/sed -e 's/.*://'|/bin/sed -e 's/symbolic link to//'`

	echo $NEW_PATH
    else
	echo $COMMAND
    fi
}

# Commands
if [ -x /usr/bin/df ]; then
	DF="env LANG=C /usr/bin/df -k"
else
if [ -x /usr/bin/bdf ]; then
	DF="/usr/bin/bdf"
else
	DF="/bin/df -k"
fi
fi

if [ -x /usr/bin/ln ]; then
	LN="/usr/bin/ln -s"
else
	LN="/bin/ln -s"
fi

if [ -x /usr/bin/rm ]; then
	RM="/usr/bin/rm -f"
else
	RM="/bin/rm -f"
fi


SWAP="/usr/sbin/swap -s"

# start getting system information...
SYSTEM_OS_NAME="`uname`"
SYSTEM_OS_REL="`uname -r`"
SYSTEM_HW_NAME="`uname -m`"
SYSTEM_NODE_NAME="`uname -n`"


# Swap space required to start wabi, 20M bytes.
SWAPREQD=20000

# WABIDIR directory space requirement for initial installation, 1M bytes.
WABIDIRREQD=1000

# Determine WABIDIR
WABIDIR=${WABIDIR:-"$HOME/wabi"}
export WABIDIR

# Record the starting point so we can return after determining WABIHOME.
INITIAL_DIR=`pwd`

# Determine PWD for those shells that don't have it
PWD=${PWD:-"$INITIAL_DIR"}
export PWD

# Find out where this script lives to determine WABIHOME.

if [ "$SYSTEM_OS_NAME" = "SCO_SV" ]
then
	COMMAND=$0

	# see if the command is a symbolic link and chase it if necessary
	COMMAND="`chase_symlink`"

	FROM_PATH=`echo "$COMMAND" | grep "/"`

	if [ -n "$FROM_PATH" ]
	then
		#in this case $COMMAND is a directory component.
		FIRSTARG=`echo ${FROM_PATH} | awk -F/ '{ print $1 }'`
 
		if [ -z "$FIRSTARG" ]
		then
			SOURCE3=`echo ${COMMAND} | awk -F/ '{ for (i = 1; i < NF; i++)  { printf "%s", $i; printf "/" } }'`
		else
			SOURCE3=`echo ${COMMAND} | awk -F/ '{ if ($1 == "") printf "/"; for (i = 1; i < NF; i++)  { printf "%s", $i; printf "/" } }'`
		fi
	else
		#in this case $COMMAND is not a directory component.
		SOURCE3="./"
	fi
 
	cd "$SOURCE3"
	cd ..
else
cd "`dirname $0`/.."
fi

WABIHOME=`pwd`
export WABIHOME

if [ "$SYSTEM_OS_NAME" = "SunOS" ]
then
	# -
	# check to see if /etc/vold.conf should change.
	# -
	#
	# if this is not already fixed...
	#
	if [ ! -f $FILE_CHECK ]
	then
  	  #
	  # only change 5.4  and 5.5 systems (should we do this?)
	  #
	  if [ "$SYSTEM_OS_REL" = "5.4" -o "$SYSTEM_OS_REL" = "5.5" -o "$SYSTEM_OS_REL" = "5.5.1" ]
	  then
	    #
	    # only do this is they have a floppy
	    #
	    if [ -c /dev/rdiskette ]
	    then
	      # 
	      # are they even running vold?
	      #

	      PID=`ps -ef | grep /usr/sbin/vold | grep -v grep | nawk '{ print $2 }'`

	      if [ ! -z "$PID" ]
	      then
	        #
	        # look to see if line exists in /etc/vold.conf
	        #

	        cat /etc/vold.conf | nawk '
	            BEGIN { FOUND = 0 } 
	            {
	    	      if(($1 == "use") && ($2 == "floppy") && ($3 == "drive")) 
		        FOUND = 1
	            }
	            END { 
		      exit (FOUND == 1)
	            } 
	        '

	        if [ "$?" = "1" ]	# if we have found the line to change
	        then
		  echo
		  echo "To use your diskette drive under Wabi, we recommend that you"
		  echo "disable Volume Manager's control of the diskette drive. To do"
		  echo "this, enter the following command as superuser:"
		  echo "	$WABIHOME/lib/vold.switch -disable"
		  echo
		  echo "See the Wabi 2.1 Release Notes for more detailed information."
		  echo
		fi
	      fi
	    fi
          fi
	fi
fi

# Check for adequate swap space to start Wabi.
case "$SYSTEM_OS_NAME" in
	
	SunOS )
   	        SWAPS=`$SWAP`
    		SWAPAVAIL=`echo ${SWAPS} | sed -e 's/.*, //' -e 's/k.*//'`
    		if [ -z "$SWAPAVAIL" ]
    		then
		    SWAPAVAIL=0
		fi

# Physical memory space recommended to run wabi, Sparc: 32M bytes; i86: 24M bytes
		if [ "$SYSTEM_HW_NAME" -eq "i86pc" ]
		then
		    MEMREQD=24
		else
		    MEMREQD=32
		fi

		MEMAVAIL=`/usr/sbin/prtconf|/bin/nawk ' {
		if($1 == "Memory" && $2 == "size:") print $3 }'`
    		if [ -z "$MEMAVAIL" ]
    		then
				MEMAVAIL=0
    		fi

    		if [ "$MEMAVAIL" -lt "$MEMREQD" ]
   		then
				echo "Warning: The memory size is less than "$MEMREQD" Mbytes recommended for Wabi."
				echo "Warning: Wabi performance may be affected."
		fi
		;;

	SCO_SV )
		# Size of required swap space in 512 byte blocks (20 Mb)
		SWAPREQD=40000
		SWAPAVAIL=`/etc/swap -l | grep /dev/swap | awk '{ print $4 }'`
		FAKE_INIT=
    	if [ -z "$SWAPAVAIL" ]
    	then
			FAKE_INIT=1
			SWAPAVAIL=$SWAPREQD
    	fi

    	if [ "$SWAPAVAIL" -lt "$SWAPREQD" ]
    	then
			echo "There may not be enough swap space on your system to run the wabi software."
			echo ""$SWAPAVAIL" 512 byte blocks are available, "$SWAPREQD" Kbytes are required."
			echo "To increase the amount of swap space, see your system administrator."
			echo "Swap space can be increased through the use of the swap and marry commands."
			SWAPAVAIL=$SWAPREQD
    	fi
		;;

	AIX )
# Need to use lsps to get real swap numbers...
		DF="/bin/df"
		SWAPAVAIL=$SWAPREQD
                MALLOCTYPE=3.1
                export MALLOCTYPE
		;;

	HP-UX )
# Have no clue here.
		SWAPAVAIL=$SWAPREQD
		;;

	UNIX_SV )
# Have no clue here either.
		SWAPAVAIL=$SWAPREQD
		;;

	$SYSTEM_NODE_NAME )
# Have no clue here.
		SWAPAVAIL=$SWAPREQD
		;;
		
esac

if [ "$SWAPAVAIL" -lt "$SWAPREQD" ]
then
    echo "There is not enough swap space on your system to run the wabi software."
    echo ""$SWAPAVAIL" Kbytes are available, "$SWAPREQD" Kbytes are required."
    exit 1
fi


# Create the user's wabi directory, if necessary.
if [ ! -d "$WABIDIR" ]
then  
    echo "Creating $WABIDIR"
    mkdir "$WABIDIR"
 
    DISKFREE=`$DF $WABIDIR 2> /dev/null`
    if [ -z "$DISKFREE" ]
    then
	# Failed to make $WABIDIR.
	echo "Could not create directory $WABIDIR; check files and permissions."
	exit 1
    fi

    if [ "$SYSTEM_OS_NAME" = "SCO_SV" ]
    then
    	WABIDIRAVAIL=`echo ${DISKFREE} | awk -F" " '{ print $4 }'`
	WABIDIRAVAIL=`expr ${WABIDIRAVAIL} / 2`
    else
    	WABIDIRAVAIL=`echo ${DISKFREE} | cut -f11 -d" "`
    fi

    if [ "$WABIDIRAVAIL" -lt "$WABIDIRREQD" ]
    then
	echo "There is not enough space in the $WABIDIR directory to run the wabi software."
	echo " "
	echo ""$WABIDIRAVAIL" Kbytes are available, "$WABIDIRREQD" Kbytes are required."
	exit 1
    fi
fi

#
#	write some system information into ~/wabi 
#

if [ -w $WABIDIR ] 
then
	/bin/rm -f $WABIDIR/sysinfo.txt
	(
		echo "USER: `id | cut -f2 -d'(' | /bin/sed -e 's/).*//`"
		echo "HOST: $SYSTEM_NODE_NAME"
		echo "DISPLAY: $DISPLAY"
		echo "OS: $SYSTEM_OS_REL"
		echo "ARCH: $SYSTEM_HW_NAME"
		case "$SYSTEM_OS_NAME" in
		    SunOS )
			echo "FB: `/bin/file -h /dev/fb|/bin/sed -e 's/.*://'`"
			echo "MEMORY: ${MEMAVAIL}MB"
			echo "SWAP: `expr ${SWAPAVAIL} / 1024`MB"
			break;;
		    *)
			echo "FB: unknown"
			echo "MEMORY: unknown"
			echo "SWAP: unknown"
			break;
		esac
	) > $WABIDIR/sysinfo.txt
	

fi


# Check for adequate swap space to start Wabi.
# To reduce the amount of space required for the user's WABIDIR, we create symbolic links
# for static files. However, to allow the user's WABIDIR to be independent of a particular
# system, we determine the symbolic link WABIDIR/wabihome each time this script is run.
# The relative symbolic links in the user's WABIDIR/windows and WABIDIR/windows/system
# are resolved through the WABIDIR/wabihome symbolic link pointing to WABIHOME.
$RM "$WABIDIR/wabihome"
$LN "$WABIHOME" "$WABIDIR/wabihome"
WABIMERGEFILE="$WABIHOME/lib/mergefile"
export WABIMERGEFILE


# Create the users windows directory if necessary.
if [ ! -d "$WABIDIR/windows" ]
then
    mkdir "$WABIDIR/windows"
fi


# Copy default wabi.ini file to the windows directory.
if [ ! -f "$WABIDIR/windows/wabi.ini" ]
then
    cp "$WABIHOME/wbin/wabi.ini" "$WABIDIR/windows"
    touch "$WABIDIR/new.tmp"
    __WABIDIR_UPDATE="yes"
else
    CURVERSION="`grep Config= $WABIDIR/windows/wabi.ini`"
    CURVERSION=`expr "$CURVERSION" : '.*=\([1-9][0-9\.]*.*\)[
]$'`
  
    if [ "$CURVERSION" = "$WABIVERSION" ]
    then
	# Put this condition only for testing.
    	MERGEFLG=`echo ${WABIMERGE}`
    	if [ -n "$MERGEFLG" -a  "$MERGEFLG" = "1" ]
	then
	    MERGE="yes"
	    __WABIDIR_UPDATE="yes"
        else
	    MERGE="no"
    	fi
    else
	MERGE="yes"
        __WABIDIR_UPDATE="yes"
    fi

    # Check if windows is installed.
    if [ -f "$WABIDIR/windows/system/setup.inf" ]
    then
        __WABI_MSWIN_VER="`grep MS_Win= $WABIDIR/windows/wabi.ini`"
        __WABI_MSWIN_VER=`expr "$__WABI_MSWIN_VER" : '.*=\([A-Z][a-z\.]*.*\)[
]$'`
    fi

    # If we're PowerPC-Solaris, make certain our wabi.ini has the new section added
    if [ "$SYSTEM_HW_NAME" = "prep" -a "$SYSTEM_OS_NAME" = "SunOS" ]
    then
	WABIBIGMEM=16777216
	export WABIBIGMEM
	PPCSECTION="`grep PowerPC $WABIDIR/windows/wabi.ini`"
	if [ -z "$PPCSECTION" ]
	then
		MERGE="yes"
		__WABIDIR_UPDATE="yes"
	fi
    fi
	
    if [ "$MERGE" = "yes" ]
    then
        echo "$CURVERSION" >"$WABIDIR/upgrade.tmp"
	mv "$WABIDIR/windows/wabi.ini" "$WABIDIR/windows/wabi.ini.$WABIVERSION"
	$WABIMERGEFILE -P "$WABIDIR/windows/wabi.ini.$WABIVERSION" "$WABIHOME/wbin/wabi.ini" "$WABIDIR/windows/wabi.ini"
	echo "Saving existing wabi.ini to wabi.ini.$WABIVERSION and updating wabi.ini."
    fi

	
fi


if [ -z "$__WABI_MSWIN_VER" ]
then
    __WABI_NEW_SHELL="win_inst"
fi

#
# Define subroutines
#

Add_DoRegSync_Yes()
{
# add the entry "WabiDoRegSync=Yes" to the "ShellData" section of wabi.ini.
# This will turn on the RegSync mechanism in the Windows Installer.

    sed '/[Ww][Aa][Bb][Ii][Dd][Oo][Rr][Ee][Gg][Ss][Yy][Nn][Cc]/d' "$WABIDIR/windows/wabi.ini" > "$WABIDIR/windows/wabi.ini.tmp" 
    $RM "$WABIDIR/windows/wabi.ini"
    sed '/^\[[Ss][Hh][Ee][Ll][Ll][Dd][Aa][Tt][Aa]\]/a\
WabiDoRegSync=Yes
' "$WABIDIR/windows/wabi.ini.tmp" > "$WABIDIR/windows/wabi.ini" 
    $RM "$WABIDIR/windows/wabi.ini.tmp"
}



#
# Define subroutines end
#

# Copy system.ini to $WABIDIR/windows
if [ ! -f "$WABIDIR/windows/system.ini" ]
then
    cp "$WABIHOME/wbin/system.ini" "$WABIDIR/windows"

    if [ "$SYSTEM_OS_NAME" = "SunOS" -a ${WABIFCS} -eq 1 ]
    then
        __WABI_NEW_SHELL="regiwabi"

    fi
else
    if [ "$MERGE" = "yes" ]
    then
	mv "$WABIDIR/windows/system.ini" "$WABIDIR/windows/system.ini.$WABIVERSION"
	$WABIMERGEFILE -P "$WABIDIR/windows/system.ini.$WABIVERSION" "$WABIHOME/wbin/system.ini" "$WABIDIR/windows/system.ini"
	echo "Saving existing system.ini to system.ini.$WABIVERSION and updating system.ini."
        if [ "$SYSTEM_OS_NAME" = "SunOS" -a ${WABIFCS} -eq 1 ]
	then

	    # make the windows install program sync up registration databases
	    # from the previous wabi version.
	    Add_DoRegSync_Yes

	    if [ ${WABIFCS} -eq 1 ]
	    then
                 __WABI_NEW_SHELL="regiwabi"
	    fi
	fi
    fi
fi

if [ -n "$__WABI_NEW_SHELL" ]
then
  # the following SED command deletes any line starting with (case-insensitive)
  # "shell=" within the set of lines that start with the line starting with
  # (case-insensitive) "[boot]" and ending (inclusive) with the next line
  # starting with "[".

    sed '/^\[[Bb][Oo][Oo][Tt]\]/,/^\[/{
/^[Ss][Hh][Ee][Ll][Ll]\=/d
}' "$WABIDIR/windows/system.ini" > "$WABIDIR/windows/system.tmp" 
    $RM "$WABIDIR/windows/system.ini"
    mv "$WABIDIR/windows/system.tmp" "$WABIDIR/windows/system.ini"	

  # the following SED command appends the line "shell=regiwabi.exe" or
  # "shell=win_inst.exe" after a line starting with (case-insensitive)
  # "[boot]".

    if [ "$__WABI_NEW_SHELL" = "regiwabi" ]
    then
	sed '/^\[[Bb][Oo][Oo][Tt]\]/ a\
shell\=regiwabi\.exe
' "$WABIDIR/windows/system.ini" > "$WABIDIR/windows/system.tmp"
    else
        sed '/^\[[Bb][Oo][Oo][Tt]\]/ a\
shell\=win\_inst\.exe
' "$WABIDIR/windows/system.ini" > "$WABIDIR/windows/system.tmp"
    fi

    $RM "$WABIDIR/windows/system.ini"
    mv "$WABIDIR/windows/system.tmp" "$WABIDIR/windows/system.ini"	    
fi


if [ -n "$__WABIDIR_UPDATE" -a "$__WABIDIR_UPDATE" = "yes" ]
then
    . "$WABIHOME/lib/wabidirupdate"
fi


# Create the wabi tmp directory if necessary.
if [ ! -d "$WABIDIR/tmp" ]
then
    mkdir "$WABIDIR/tmp"
fi


# Create the wabi font cache directory if necessary.
if [ ! -d "$WABIDIR/fc" ]
then
   mkdir "$WABIDIR/fc"
fi


if [ "$SYSTEM_OS_NAME" = SunOS ]
then
    WABIDRV_INSTALLED=`grep wabi /etc/driver_aliases`
	
    if [ -z "$WABIDRV_INSTALLED" ]
    then
	 # Test if wabi is installed in the local machine.
	 WHICHWABI="S`echo ${WABIHOME} | cut -f2 -dS`"
    	 LOCALWABI="`pkginfo | grep $WHICHWABI`"

	if [ -z "$LOCALWABI" ]
    	then
	    # Wabi is not installed in local machine
	    echo "Warning: Package $WHICHWABI is not installed in this machine."
	    echo "Run $WABIHOME/bin/wabiclientinstall as root to update" 
	    echo "the desktop environment."
	    exit 1
  	else
	    # Wabi is installed in local mechine but driver is not installed.
	    echo "Warning: The Wabi kernel driver is not installed."
            echo "Run $WABIHOME/drvr/wabiload as root to install the driver"
	fi
    fi
fi


# Until libvolmgt.so.1 becomes permanent in Solaris 2.3 we have to issue
# a warning to install the Volume Manager patch on Solaris 2.2 - if it's
# not already installed.
if [ "$SYSTEM_OS_REL" = "5.2" ]
then
    if [ ! -f /usr/lib/libvolmgt.so.1 ]
    then
	echo " "
	echo "The Volume Manager patch should be installed on Solaris 2.2, otherwise"
	echo "Wabi may not be able to use the diskette drive."
	echo " "
    fi
fi

# Go to start point
cd "$INITIAL_DIR"

#setup arguments
IFLAG="no"
if [ $# != 0 ]
then
	FIRSTFLAG=`echo $* | cut -f1 -d' '`
	# echo $FIRSTFLAG
	if [ $FIRSTFLAG = -i ]
	then
		IFLAG="yes"
	fi

	# make sure this and all children show up on the right display
	num=0
	for arg in $*
	do
	    if [ "$arg" = "-display" ]
	    then
	      eval DISPLAY=$`expr "$num" + 2`
	      break
	    fi
	    num=`expr "$num" + 1` 2>&-
	done
fi

export DISPLAY
	

# HP specific code
if [ -f $WABIHOME/vue/custom.vue ]
then
	$WABIHOME/vue/custom.vue
 	if [ $? = 1 ]
	then
		exit
	fi
fi

# Ignoring eject diskette signal
trap "" 16

#
# remove this stuff after beta
#
 
#
# automatically start surveytool if it's been a week or more
# since the last time it was run
#
 
if [ `uname -s` = "SunOS" -a -f $WABIDIR/.surveystamp ]
then
        if [ "`find $WABIDIR/.surveystamp -mtime +6 -print`" != "" ]
        then
                $RM -f $WABIDIR/.surveystamp
                touch $WABIDIR/.surveystamp
                (
                        sleep 30                # wait until wabiprog starts
                        $WABIHOME/bin/wabiprog -s c:\\wabihome\\wbin\\surveys.exe
                              
                ) >/dev/null 2>&1 &
        fi
else
        touch $WABIDIR/.surveystamp
fi
 
#
# end remove this stuff after beta
#

#remove these next two lines for PowerPC FCS
	WABIBIGMEM=16777216
	export WABIBIGMEM

if [ "$IFLAG" = "yes" ]
then
    shift 1
    # Start wabiprog.i
    PATH=$__WABI_ORIG_PATH $WABIHOME/bin/wabiprog.i  -c $*
else 
    # Start wabiprog
    PATH=$__WABI_ORIG_PATH $WABIHOME/bin/wabiprog -c $*
fi


# Restart Wabi automatically
while [ $? = 111 ]
do
    # If user choose restart wabi after MS Windows is installed, do merging when wabi restart.
    if [ -f "$WABIDIR/windows/control.src" ]
    then
    	if [ -f "$WABIDIR/windows/control.ini" ]
    	then
            mv "$WABIDIR/windows/control.ini" "$WABIDIR/windows/control.ini.$WABIVERSION"
    	    $WABIMERGEFILE -P "$WABIDIR/windows/control.src" "$WABIDIR/windows/control.ini.$WABIVERSION" "$WABIDIR/windows/control.ini"
            $RM "$WABIDIR/windows/control.src"
            $RM "$WABIDIR/windows/control.ini.$WABIVERSION"
        fi
    fi

#remove these next two lines for PowerPC FCS
    WABIBIGMEM=16777216
    export WABIBIGMEM

    if [ "$IFLAG" = "yes" ]
    then
    	# Start wabiprog.i
    	PATH=$__WABI_ORIG_PATH $WABIHOME/bin/wabiprog.i -c $*
    else 
    	# Start wabiprog
     	PATH=$__WABI_ORIG_PATH $WABIHOME/bin/wabiprog -c $*
    fi
done

# If user do not choose restart wabi after MS Windows is installed, do merging when wabi exit.
if [ -f "$WABIDIR/windows/control.src" ]
then
    if [ -f "$WABIDIR/windows/control.ini" ]
    then
	mv "$WABIDIR/windows/control.ini" "$WABIDIR/windows/control.ini.$WABIVERSION"
  	$WABIMERGEFILE -P "$WABIDIR/windows/control.src" "$WABIDIR/windows/control.ini.$WABIVERSION" "$WABIDIR/windows/control.ini"
      	$RM "$WABIDIR/windows/control.src"
        $RM "$WABIDIR/windows/control.ini.$WABIVERSION"
    fi
fi

exit $?
