#!/bin/sh
##############################################################################
#
#     user.setup
#
#     generated by genscript on Thu May  1 13:10:47 CDT 1997
#
#     ***************************************************************
#     * DO NOT EDIT * * DO NOT EDIT * * DO NOT EDIT * * DO NOT EDIT * 
#     ***************************************************************
#
#     (c) Copyright 1992-1997 Metrowerks Corporation
#     All Rights Reserved.
#
##############################################################################
# Linked STATICLY
#
##############################################################################

#
# Execute this script under a posix sh if available.
# (The Bourne shell is broken under HP-UX).
#
if [ "$1" != posix -a -x /bin/posix/sh ]
then
    exec /bin/posix/sh $0 posix
fi

PATH="/usr/5bin:/bin:/usr/ucb:/usr/bin:/etc:/usr/bsd:/usr/sbin:/usr/bin/X11"
export PATH

inst_user()
{
  umask 0

  # Establish the starting dir
  PROGNAME=`basename $0`
  cd `dname $0`
  START_DIR=`cur_dir`
  cd ..
  INST_ROOT=`cur_dir`

  # defaults for options
  TTYMODE="Y"

  # make sure that we're dealing with a supported OS
  OS=`os`
  case $OS in
  IRIX-5)
    ;;
  IRIX-6)
    ;;
  SunOS-5)
    ;;
  HP-UX-9|HP-UX-10)
    ;;
  *)
    echo2 "System $OS not supported. Aborting."
    pause_exit $TTYMODE 1
    ;;
  esac

  # Product & Version info
  set -a
  PRODUCT="Latitude"
  VERSION="3.0"
  PRODUCT_W_VER="$PRODUCT $VERSION"

  if(isroot)
  then
    echo2 "
This script is normally run by non-privileged users to set up their
home directories. You are currently running this script as root.
"
    ANS=`yesno "Do you really wish to continue (y/n)?" "n"`
    if [ $ANS = "N" ]
    then
      echo2 "
User setup aborted at user's request.
"
      pause_exit $TTYMODE 1
    fi
  fi

  # say hello
  echo "
#############################################################################

                           $PRODUCT_W_VER
                     USER ENVIRONMENT INSTALLATION
                    `date`

#############################################################################

To quit the installation process at any time, press Control-C in
response to an installation question.

The default choice for an installation question is shown enclosed by
angle brackets (<>). To enter the default response, press Return.

To enter something other than the default, type your response at the
end of the question, then press Return.

In order to use $PRODUCT most effectively, this program creates a
directory for your Macintosh files, user preferences, and fonts. 
(For further information on file system integration, see
the \"$PRODUCT_W_VER Developer's Guide.\")

In addition, the $PRODUCT user environment installation program will place
some X application default files in your home directory. 
"
  # make sure the user really wants to proceed
  ANS=`yesno "OK to proceed with installing these files for the user (y/n)?" \
             "y"`
  if [ $ANS = "N" ]
  then
    echo "$PRODUCT user environment installation aborted at user's request."
    exit 0
  fi
        

  # get the name of the installation area
  echo2 "
The $PRODUCT user environment installation required some files
from your $PRODUCT installation.
"
  ANS=`yesno "Is $INST_ROOT the directory where $PRODUCT was installed (y/n)?"\
              "y"`
  echo2 " "
  if [ $ANS != "Y" ]
  then
    INST_ROOT=`converse \
      "In which directory is your $PRODUCT installed?" \
      $INST_ROOT`
  fi

  # verify that INST_ROOT is really a LATITUDE_ROOT
  if [ ! -r ${INST_ROOT}/lib/liblg.a ]
  then
    echo2 "
$INST_ROOT doesn't seem to be an $PRODUCT_W_VER installation
directory, Please examine the installation log there and make sure
that $PRODUCT was successfully installed.
"
    pause_exit $TTYMODE 1
  fi

  # get the username
  USER=`whoami`

  # get the name of the user's home directory
  HOME_DIR=$HOME


  MEM=`ram`
  if [ $MEM -lt 32 ]
  then
    echo2 "
------------------------------------------------------------------------
Please Note:

        Your system has ${MEM}MB of RAM. This is less than the 32 MB
of RAM we recommend for running $PRODUCT. You should still be able to
run $PRODUCT but may experience some performance degradation. 
------------------------------------------------------------------------
"
  fi

  ################################################
  # Here's where we actually do the installation #
  ################################################

  echo2 "
Setting up environment for user $USER. Installing $PRODUCT files from
$INST_ROOT into $HOME_DIR for $OS
"
  # Give the user its own system folder
  INSTALL_SYS="Y"
  MACDIR="$HOME_DIR/$USER.MacFiles"

  if [ -d $MACDIR/System ]
  then 
    echo2 "
You seem to have a Mac System folder already installed. It will be
saved as the following directory:

        $MACDIR/System.orig.
"
    # save the existing System Folder
    save_old_file $MACDIR/System | tee $MACDIR/.prev_system
  fi

  echo2 
  echo2 Creating a new Macintosh System Folder.
  echo2 

  #blindly try to create the MACDIR
  silentdo mkdir -p  $MACDIR

  # create an empty .Latituderc or just update the timestamp if it exists
  silentdo touch $MACDIR/.Latituderc

  # Copy in the system folder from the installation area
  cd $INST_ROOT/Latitude.MacFiles
  silentdo "find System -depth -print | cpio -pduvm $MACDIR"
  silentdo find $MACDIR/System -exec chmod u+w {} \;

  # make sure the system folder was created
  if [ ! -d $MACDIR/System ]
  then 
    echo2 "
Couldn't create your Mac System Folder. Make sure
you have the necessary permissions and try again.
"
    pause_exit $TTYMODE 1
  else
    echo2 "
A Macintosh System folder has been created in the following directory:

        $MACDIR"

    # preserve any user preferences from a previous installation
    if [ -r $MACDIR/.prev_system ]
    then

      ANS=`yesno "
Since you are already an $PRODUCT user, you may wish to have this script
copy your existing $PRODUCT preferences into your new system
folder. Note, though, that doing so will overwrite the default
preferences provided with this version of $PRODUCT.

Would you like to copy your existing application preferences into  
your new system folder (y/n)?" "n"`

      if [ $ANS = "Y" ]
      then
        echo2 "
  Copying your application preferences to your new system folder..."

        PREV_SYS=`cat $MACDIR/.prev_system|awk '{ print $4 }'|sed 's/\.$//'`
        cd $PREV_SYS/Preferences
        find . -depth -print | cpio -pdum $MACDIR/System/Preferences > \
            /dev/null 2>& 1
      fi

      rm -f $MACDIR/.prev_system

    fi
  fi

  echo2 
  echo2 Copying application defaults...
  echo2 

  # handle the app-defaults
  for i in $INST_ROOT/app-defaults/*
  do
    DESTFILE="$HOME_DIR/`basename $i`"

    if [ -r $DESTFILE ]
    then 
      save_old_file $DESTFILE
    fi

    cp $i $DESTFILE
  done

  echo2 "
Application defaults installed.
"

  echo2 " 
Please remember to add the following directory to your search path:

        $INST_ROOT/bin

You can now proceed with configuring your UNIX environment.

To do so, follow the instructions given in

        \"Configuring Your UNIX Environment\"

in the \"$PRODUCT_W_VER Guide\".
"
  pause_exit $TTYMODE 0
}
##########################################################
###############################################################################
#           $Id: util.shlib,v 1.2 1996/06/14 19:10:50 pinky Exp $
#
#                  Latitude utility Package: Requires: None            
###############################################################################

#
# echo2 arg_list
#
#        Behaves exactly like echo, but echoes its arguments to stderr
#
echo2()
{
  echo "$*" 1>&2
}

#
# echodo arg_list
#
#        Both echoes and executes its argument list
#
echodo()
{
  echo2 $*
  eval trap intr_handler 1 2 3 15 \; $*
}

#
# silentdo arg_list
#
#        evaluates arg_list discarding any output to stdout/stderr
#
silentdo()
{
  eval trap intr_handler 1 2 3 15 \; $* > /dev/null 2>&1
}

#
# isroot
#
#       returns TRUE (0) exit status is invoker is the super user 
#       otherwise FALSE (non-zero) exit status is returned
#
isroot()
{       
  if [ -w /etc/passwd ]
  then 
    return 0
  else
    return 1
  fi
}

#
# converse prompt default
# 
#      Converse with the user. Issue the supplied prompt
#      and echo the user's reply. If the user gives a null
#      reply then default is echoed.
#
converse()
{
  echo2 "$1 <$2> \c"
  read REPLY

  echo ${REPLY:-$2}
}

# yesno is behaves exactly like converse except that it keeps
# prompting the user 'till it gives a response in [YyNn].
# When an acceptable response is received then its upper case 
# equivalent is echoed
yesno()
{
  DONE="N"
  while [ $DONE = "N" ]
  do
    case `converse "$1" $2` in
      [yY]*)
          echo "Y" 
          DONE="Y"
          ;;
      [nN]*)
          echo "N"
          DONE="Y"
          ;;
    esac
  done
}

dir_empty()
{
  DIR=$1; shift

  if [ `/bin/ls $DIR | wc -l` = 0 ]
  then
    echo "Y"
    return 0
  else
    echo "N"
    return 1
  fi
}

dir_writable()
{
  TESTFILE=$1/.$$.write
  rm -f "$TESTFILE"
  touch $TESTFILE > /dev/null 2>&1

  if [ -w "$TESTFILE" ]
  then
    rm -f $TESTFILE
    echo "Y"
    return 0        
  else
    echo "N"
    return 1        
  fi
}

os()
{
  to_production_os_name `real_os_name`
}

real_os_name()
{
  # Get OS name, substituting '-' for all spaces 
  uname -sr | sed 's/ /-/g'
}

to_production_os_name()
{
  tmp_os=$1

  # Handle some HP wierdness wrt Maj.Min.Rel given as Rel.Maj.Min
  case $tmp_os in
    HP-UX*)
      tmp_os=`echo $tmp_os | sed 's/X-.\./X-/' | sed 's/-0*/-/g'`
    ;;
  esac

  # Strip off minor version info
  tmp_os=`echo $tmp_os | sed 's/\..*$//'`

  echo $tmp_os
}

req_space()
{
  case `os` in
  IRIX-5)
    du -sk $1 | awk '{print $1}'
    ;;
  IRIX-6)
    du -sk $1 | awk '{print $1}'
    ;;
  SunOS-5)
    /bin/du -sk $1 | awk '{print $1}'
    ;;
  HP-UX-9|HP-UX-10)
    expr `/bin/du -s $1 | awk '{print $1}'` "*" 2
    ;;
  esac
  
}

avail_space () 
{
    file="$1"
    while [ ! -r "$file" ]
    do
	file=`dname "$file"`
    done

    case `os` in
    IRIX-5)
        echo `df -k "$file"` | awk -e '{ print $13 }'
      ;;
    IRIX-6)
        echo `df -k "$file"` | awk -e '{ print $13 }'
      ;;
    SunOS-5)
      echo `/bin/df -k "$file"` | awk '{ print $11 }'
      ;;
    HP-UX-9|HP-UX-10)
      echo `bdf "$file"` | awk '{ print $11 }'
      ;;
    esac
}

swap_space () 
{
    case `os` in
    IRIX-5)
      swap -l | awk -e 'NR > 1 { sum += $4 / 2 } END { print sum }'
      ;;
    IRIX-6)
      swap -l | awk -e 'NR > 1 { sum += $4 / 2 } END { print sum }'
      ;;
    SunOS-5)
      swap -l | awk -e 'NR > 1 { sum += $4 / 2 } END { print sum }'
      ;;
    HP-UX-9|HP-UX-10)
      echo `/etc/swapinfo -d` | awk '{ print $16}'
      ;;
    esac
}

ram () 
{
    # broken for HP
    case `os` in
    IRIX-5)
      hinv -t memory | awk -e '{ print $4 }'
      return 0
      ;;
    IRIX-6)
      hinv -t memory | awk -e '{ print $4 }'
      return 0
      ;;
    SunOS-5)
      # echo2 "Unimplemented for SunOS 5.x: ram()"
      echo 1024
      return 0
      ;;
    HP-UX-9|HP-UX-10)
      # echo2 "Unimplemented for HP-UX: ram()"
      echo 1024
      return 0
      ;;
    esac
}

pause()
{
  if [ "${NOPAUSE:-NULL}" != NULL ]
  then
    return 0
  else
    converse "Please press RETURN to continue or Control-C to quit" "" \
    > /dev/null
  fi
}

get_pid()
{
  PNAME=$1; shift

  case `os` in
  IRIX-5)
    PID=`ps -ef | awk "/awk/{next}; /$PNAME/{print \\$2}"`
    ;;
  IRIX-6)
    PID=`ps -ef | awk "/awk/{next}; /$PNAME/{print \\$2}"`
    ;;
  HP-UX-9|HP-UX-10)
    PID=`ps -ef | awk "/awk/{next}; /$PNAME/{print \\$2}"`
    ;;
  SunOS-5)
    PID=`/bin/ps -ef | awk "/awk/{next}; /$PNAME/{print \\$2}"`
    ;;
  esac

  echo ${PID:-"NONE"}
}

running_nis()
{
  PID=`get_pid ypbind`
  if [ $PID = "NONE" ]
  then 
    echo "N"
  else 
    echo "Y"
  fi
}

x_installed()
{
  if [ -r /usr/lib/X11 ]
  then 
    echo "Y"
    return 0
  fi

  echo "N"
  return 1
}

x_running()
{
  # Broken for HP, but we can always assume that X is running
  if(/usr/bin/X11/xdpyinfo 2>&1 | grep -i "name of display" > /dev/null)
  then 
    echo "Y"
    return 0
  else
    echo "N"
    return 1
  fi
}

devname()
{
  file=$1; shift

  case `os` in
  IRIX-5)
    echo `df -k "$file"` | awk -e '{ print $9 }'
    ;;
  IRIX-6)
    echo `df -k "$file"` | awk -e '{ print $9 }'
    ;;
  SunOS-5)
    echo `df -k "$file"` | awk -e '{ print $8 }'
    ;;
  HP-UX-9|HP-UX-10)
    echo `bdf "$file"` | awk '{ print $8 }'
    ;;
  esac
}

pause_exit()
{
  TTYMODE=$1; shift
  RETVAL=$1; shift

  if [ $TTYMODE = "N" ]
  then
    converse "Press RETURN to exit..." " "
  fi

  exit $RETVAL
}

beep()
{
  echo2 " \c"
}

# like pwd but eliminates /tmp_mnt from the path 
cur_dir()
{
  pwd | sed s#/tmp_mnt##
}

# a replacement for the SYSV DIRNAME script
dname()
{
  /bin/expr "${1:-.}/" : '\(/\)/*[^/]*//*$'  \| \
            "${1:-.}/" : '\(.*[^/]\)//*[^/][^/]*//*$' \| \
             .
}

# This function takes the name of a file or directory, $CURNAME, which
# is about to be overwritten. If the file/dir $CURNAME.orig does not
# exist, then $CURNAME will be renamed $CURNAME.orig. Otherwise, the 
# user will be prompted for a name under which to save $CURNAME.
save_old_file()
{
  CURNAME=$1; shift

  NEWNAME="$CURNAME.orig"

  if [ -r "$NEWNAME" ]
  then
    DONE="N"
    while [ "$DONE" = "N" ]
    do
      LASTNAME="$NEWNAME"

      NEWNAME=`converse "
$NEWNAME already exists. 
Under what name would you like to save it?" $NEWNAME`

      if [ ! -r "$NEWNAME" ]
      then
        DONE="Y"
      else
        if [ "$LASTNAME" = "$NEWNAME" ]
        then
          rm -fr $NEWNAME
          if [ ! -r $NEWNAME ]
	  then
	    DONE="Y"
	  fi
        fi
      fi
    done
  fi

  echo "Saving $CURNAME as $NEWNAME."
  mv $CURNAME $NEWNAME
  if [ $? != 0 ]
  then
    echo2 "Unable to move $CURNAME to $NEWNAME."
    echo2 "Remove $NEWNAME manually, then rerun this program."
    echo2 "Aborting..."
    exit 1
  fi
}

# convert arguments from lower to upper case
upcase()
{
  echo $* | tr "[a-z]" "[A-Z]"
}

# interrupt handling mechanism
BACKGROUND_PIDS=""; export BACKGROUND_PIDS
intr_handler()
{
  echo "
Terminating at user's request...
" > /dev/tty
  kill -TERM $BACKGROUND_PIDS > /dev/null 2>&1
  exit 0
}
trap intr_handler 1 2 3 15
##########################################################

inst_user $*
