#!/bin/sh
#
#	MODIFIED VERSION FOR LINUX
#
# @(#)wabi	2.170	02 Feb 1996
#
#	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-96, 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.

#
# Define internationalized "echo" replacement routine
# Not yet supported.
#
msgprint(){
	echo $* 2>&1
}

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

# Definitions
WABIVERSION=2.2

LD_LIBRARY_PATH=/usr/X11R6/lib-old:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH
#
# WABIFCS is used to determine if the current Wabi package is an FCS release.
# It is set to 0 if ${WABIVERSION} contains "alpha", "beta", or "wip".
# Otherwise, it is set to 1, indicating an FCS release.  We only invoke the
# survey tool for non-FCS releases, while the registration card is for Wabi FCS.
#
# The Makefile in $SRC/unixsrc/solaris/pkg also does a similar check on
# the value of $WABIVERSION, to determine whether to include filebug.exe and
# surveys.exe in the prototype file for packaging, which is only for non-FCS.
#

echo ${WABIVERSION} | egrep -i "alpha|beta|wip" > /dev/null 2>&1
WABIFCS=$?


#
# Invoke wabiprog with the "-c" option for non-FCS releases
#

if [ $WABIFCS -ne 1 ]
then
    WABIFLAGS="-c"
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

# 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

# Physical memory space recommended to run Wabi
MEMREQD=24

# 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.
cd "`dirname $0`/.."
WABIHOME=/opt/wabi
export WABIHOME

# Check for adequate swap space to start Wabi.
SWAPAVAIL=`cat /proc/meminfo | awk '{if ($1 == "Swap:") print $4}'`
if [ -z "$SWAPAVAIL" ]
then
    SWAPAVAIL=0
fi
# Convert from bytes to kbytes
SWAPAVAIL=`expr ${SWAPAVAIL} / 1024`

MEMAVAIL=`cat /proc/meminfo | awk '{if ($1 == "Mem:") print $2}'`
# Convert from bytes to Mbytes
MEMAVAIL=`expr ${MEMAVAIL} / 1000000`
if [ -z "$MEMAVAIL" ]
then
	MEMAVAIL=0
fi

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

if [ "$SWAPAVAIL" -lt "$SWAPREQD" ]
then
	msgprint Warning: There is not enough swap space on your system to run Wabi.
	msgprint $SWAPAVAIL Kbytes are available, $SWAPREQD Kbytes are required.
    exit 1
fi

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

    WABIDIRAVAIL=`echo ${DISKFREE} | cut -f11 -d" "`

    if [ "$WABIDIRAVAIL" -lt "$WABIDIRREQD" ]
    then
	msgprint There is not enough space in the $WABIDIR directory to run Wabi.
	msgprint $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 --name --user`"
		echo "HOST: $SYSTEM_NODE_NAME"
		echo "DISPLAY: $DISPLAY"
		echo "OS: $SYSTEM_OS_REL"
		echo "ARCH: $SYSTEM_HW_NAME"
		echo "FB: unknown"
		echo "MEMORY: ${MEMAVAIL}MB"
		echo "SWAP: `expr ${SWAPAVAIL} / 1024`MB"
	) > $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
    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"
	msgprint 'Saving existing wabi.ini to wabi.ini.$WABIVERSION and updating wabi.ini.' 1>&2
    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"
	msgprint 'Saving existing system.ini to system.ini.$WABIVERSION and updating system.ini.' 1>&2
        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

            __WABI_NEW_SHELL="regiwabi"
	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"
    echo ""
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
	    msgprint 'Warning: Package $WHICHWABI is not installed in this machine.
Run $WABIHOME/bin/wabiclientinstall as root to update
the desktop environment.' 1>&2
	    exit 1
  	else
	    # Wabi is installed in local mechine but driver is not installed.
	    msgprint 'Warning: The Wabi kernel driver is not installed.
Run $WABIHOME/drvr/wabiload as root to install the driver.' 1>&2
	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
	msgprint '
The Volume Manager patch should be installed on Solaris 2.2, otherwise
Wabi may not be able to use the diskette drive.
' 1>&2
    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

#
# If this is a non-FCS Solaris release, invoke the survey tool if more 
# than a week has passed since its last invocation.
#

if [ ${WABIFCS} -ne 1 -a ${SYSTEM_OS_NAME} = "SunOS" ]
then

    if [ -f $WABIDIR/.surveystamp ]
    then

	#
	# If there is an existing .surveystamp and its more than a week
	# old, then run the survey tool and reset the survey time stamp.
	#

        if [ "`find $WABIDIR/.surveystamp -mtime +6 -print`" != "" ]
        then

	    $RM    $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

	#
	# For a new $WABIDIR, create a time stamp file for the survey tool.
	# During a non-FCS release, we check this file and start the survey
	# tool if it more than a week old.
	#

        touch $WABIDIR/.surveystamp

    fi

fi
 
# Start wabiprog
PATH=$__WABI_ORIG_PATH $WABIHOME/bin/wabiprog $WABIFLAGS $*
WABI_RETURN_VAL=$?


# Restart Wabi automatically
while [ $WABI_RETURN_VAL = 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

    if [ "$IFLAG" = "yes" ]
    then
    	# Start wabiprog.i
        PATH=$__WABI_ORIG_PATH $WABIHOME/bin/wabiprog.i $WABIFLAGS $*
    else 
    	# Start wabiprog
        PATH=$__WABI_ORIG_PATH $WABIHOME/bin/wabiprog${ARCH_EXT} $WABIFLAGS $*
    fi
    WABI_RETURN_VAL=$?
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 $WABI_RETURN_VAL
