#!/bin/sh
#*******************************************************************************
# E.S.O. - VLT project
#
# "@(#) $Id: setEnvironment,v 1.160 1999/04/14 09:29:34 vltsccm Exp $" 
#
# setEnvironment
#
# who        when        what
# ---------  ----------  ----------------------------------------------
# G.FILIPPI  06/09/1993  created
# G.FILIPPI  08/09/1993  added -test option
# G.FILIPPI  09/09/1993  execute .vueprofile after copying
# G.FILIPPI  06/12/1993  extended to te19
# G.FILIPPI  24/08/1998  Add two links in order to use the emacs setup
#

#************************************************************************
#   NAME
#   setEnvironment - toggle between VLT standard and users's shell and  
#                    window environments 
#
#   SYNOPSIS
#       /vlt/System/setEnvironment [-test]
#
#   DESCRIPTION
#   
#   This script provides a quick toggle between standard and users's 
#   shell and window VLT  environments 
# 
#   -test      to use the version under development (systemFiles/bin)
#              Restricted to development.
#
#   FILES
#   /vlt/System/*     where to take files
#   $HOME/*           where to put files for the new environment
#   $HOME/old_home    directory where copying existing files
#
#   ENVIRONMENT
#
#   RETURN VALUES
#       0 success, 1 installation aborted.
#
#   CAUTIONS
#
#   EXAMPLES
#
#   SEE ALSO   
#
#   BUGS     
#   The error management is very poor. WHEN STARTED, DO NOT INTERRUPT. 
#   
#   Except those made using the file .*local, at present, does not save 
#   customization done on top of the standard environment.
#
#   The -test option shall be improved, because some file are calling others
#   using absolute path.
#------------------------------------------------------------------------------

SCCS_ID="@(#)  setEnvironment    version 1.1    06/09/1993  "

#
# find out whether the version under development or the official ones should
# be used and set up environment accordingly.
if [ "$1" = "-test" ]
then 
    BIN=/home1/gfilippi/vlt/standards/systemFiles/bin
    SAVEDIR=old_home_test
    LOCK_FILE=.0_WARNING_installation_not_complete_test
    FLAG="- TEST DEVELOPMENT!!!"
else
    BIN=/vlt/System
    SAVEDIR=old_home
    LOCK_FILE=.0_WARNING_installation_not_complete
fi

HEADER="$SCCS_ID $FLAG"

#
# Where am I?
os=`uname`

#
# Everything is relative to HOME
cd $HOME

#
#------------------------------------------------------------------------------
# what do I have to do?
#
#   SAVEDIR does not exist ---> install VLT standard environment
#                                 - create save directory
#                                 - save and install C/TC files files
#                                 - if .xsession is present 
#                                         -> save and install MOTIF files
#                                 - if .vue is present 
#                                         -> save VUE files and create lock file
#                                            to enable pahse 2
#
#   SAVEDIR exists as well as the LOCK_FILE 
#                          ---> install VLT standard environment phase 2 (HP ONLY)
#                                 - install some VUE files
#                                 - drive the user to configure the VUE environemnt
#                                 - remove lock file
#
#   only SAVEDIR exists    ---> reset to user's environment
#                                 - restore C/TC files  files
#                                 - if .xsession is present in the save directory 
#                                        -> restore MOTIF files
#                                 - if .vue is present in the save directory 
#                                        -> restore VUE files 
#                                   remove save directory
#------------------------------------------------------------------------------
#
if [ -d  $SAVEDIR ] 
then
    if [ -f  $LOCK_FILE ] 
    then
        install=VLT-2
    else
        install=USER
    fi
else
    install=VLT
fi


clear

echo "------------------------------------------------------------------------------"
echo "$HEADER"
echo ""

case $install in
    VLT)
        title="         INSTALLING VLT ENVIRONMENT."
        echo "$title" 
        echo ""
        echo ""
        echo "               existing files --------> \$HOME/$SAVEDIR"
        echo "               VLT standard   --------> \$HOME ($HOME)"
        echo ""
        echo ""
        ;;

    VLT-2)
        title="         INSTALLING VLT ENVIRONMENT. PHASE 2 (HP ONLY)"
        echo "$title" 
        echo ""
        echo ""
        echo "               Configuring the VUE environment" 
        echo ""
        echo ""
        ;;

    USER)
        title="         RESTORING USER ENVIRONMENT."
        echo "$title" 
        echo ""
        echo ""
        echo "               existing files   --------> deleted"
        echo "               \$HOME/$SAVEDIR   --------> $HOME"
        echo ""
        echo ""
        ;;
esac
echo ""
echo "     >>>>ONCE STARTED, DO NOT INTERRUPT THE PROCEDURE<<<<<<"
echo "                      ==================                   "
echo ""
echo ""
echo ""
echo "     IN CASE OF ERROR, CONCTACT YOUR SYSTEM MANAGER FOR HELP"
echo ""
echo ""
echo "\007 PRESS <Enter> TO CONTINUE OR  <^C>  TO STOP ...\c"
read a

clear

echo "------------------------------------------------------------------------------"
echo "$HEADER"
echo ""
echo "$title" 
echo ""

if [ $install = VLT ] 
then 

    #
    # create save dir
    if mkdir $SAVEDIR
    then
        echo " Your existing configuration will be saved in \$HOME/$SAVEDIR "
        echo ""
    else
        echo ""
        echo "***ERROR: cannot create save dir $SAVEDIR. INSTALLATION ABORTED."
        echo "------------------------------------------------------------------------------"
        exit 1
    fi

    #
    # Shell environment (the same for HP and SUN)
    #
    echo " Save current and install VLT C/TC files\c"
    #just to impress the user with the computer thinking for a while!!
    sleep 1; echo " .\c"
    #
    # Save files
    for file in .login .logout .cshrc .tcshrc 
    do
        if [ -f $file ] 
        then
            mv $file $SAVEDIR 
        fi
    done
    echo " .\c"; sleep 1; echo " .\c"
    #
    # install VLT C/TC files
    ln -s $BIN/Cshrc  .cshrc
    ln -s $BIN/TCshrc .tcshrc
    ln -s $BIN/Login  .login
    ln -s $BIN/Logout .logout
    echo " .\c"; sleep 1; echo " .\c"
    #
    # if not already existing, provide templates for user's customization
    if [ ! -f .cshrc.local  ] ; then cp $BIN/Cshrc.local   .cshrc.local ; fi
    if [ ! -f .tcshrc.local ] ; then cp $BIN/TCshrc.local  .tcshrc.local; fi
    if [ ! -f .login.local  ] ; then cp $BIN/Login.local   .login.local; fi
    if [ ! -f .logout.local ] ; then cp $BIN/Logout.local  .logout.local; fi
    echo " .\c"; sleep 1; echo " .\c"

    #
    # if not already existing, provide Emacs definitions 
    if [ ! -f .emacs        ] ; then ln -s $BIN/Emacs       .emacs       ; fi
    if [ ! -f .emacs.local  ] ; then ln -s $BIN/Emacs.local .emacs.local ; fi

    echo " .\c"; sleep 1; echo " .\c"
    echo " done"

    #
    # MOTIF environment on Sun machines (both Solaris 1 & 2)
    #
    if [ "$os" = "SunOS" ]
    then 
        echo ""
        echo " Save current and install VLT MOTIF environment \c"
        sleep 1; echo " .\c"
        #
        # Save files
        for file in .xsession .xclients .Xresources .mwmrc
        do
            if [ -f $file ] 
            then
                mv $file $SAVEDIR 
            fi
        done
        sleep 1; echo " .\c"
        #
        # install VLT MOTIF environment
        ln -s $BIN/Xsession  .xsession
        ln -s $BIN/Xclients  .xclients
        ln -s $BIN/Mwmrc     .mwmrc
        sleep 1; echo " .\c"
        echo " done"
    fi

    #
    # VUE environment (if present)
    #
    if [ -d .vue ] 
    then 
        echo ""
        echo " Save current HP Visual User Environment (VUE)\c"
        sleep 1; echo " .\c"
        if [ -f .vueprofile ] ; then mv .vueprofile $SAVEDIR; fi
        echo " .\c"; sleep 1; echo " .\c"
        if [ -d .vue        ] ; then mv .vue        $SAVEDIR; fi
        sleep 1; echo " .\c"
        echo " done"
        #
        # warn the user about the need of a second phase
        exitMessage=PHASE-2
        #
        # set lock file, so next time phase 2 for VUE will be executed.
        cat <<xyz  >$LOCK_FILE 


          VLT STANDARD ENVIRONMENT -- PHASE 1 SUCCESSFULLY TERMINATED


     ISSUE THE COMMAND /vlt/System/setEnvironment TO PERFORM PHASE II

            >>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<
            >>       DO NOT DELETE THIS FILE BY HAND           <<
            >> IT WILL BE DELETED AT THE COMPLETION OF PHASE 2 <<
            >>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<
xyz

    else
        #
        # if VUE is not present the installation is completed 
        exitMessage=OK-VLT

    fi  # end if .vue

fi  # end if VLT


if [ $install = VLT-2 ] 
then 
    # 
    # Check whether VUE has been initialized:
    if [ ! -d .vue ]
    then 
        echo ""
        echo "***ERROR: VUE has been correctly initialized by HP-UX. INSTALLATION ABORTED."
        echo "------------------------------------------------------------------------------"
        exit 1
    fi

    # 
    echo ""
    echo " Set standard VUE environment\c"
    sleep 1; echo " .\c"

    # remove the .vueprofile provided by default by VUE, 
    # and link std file instead,
    rm -f .vueprofile
    ln -s $BIN/Vueprofile  .vueprofile

    # execute the file to update the xhosts list. (later, clients from
    # other hosts need to be opened.
    .vueprofile 1>/dev/null 2>&1

    sleep 1; echo " .\c"

    # link VUE rc file
    ln -s $BIN/Vuewmrc     .vue/vuewmrc
    sleep 1; echo " .\c"
    echo " done"

    echo ""
    echo "  TO COMPLETE THE INSTALLATION YOU NEED TO INTERACTIVELY CONFIGURE"
    echo "                     THE VUE WINDOW MANAGER"
    echo ""
    echo "   PLEASE FOLLOW STEP-BY-STEP THE INSTRUCTION ON THE NEXT SCREENS"
    echo ""
    echo ""
    echo " WARNING: IF BY ACCIDENT YOU PRESS <Enter> TWICE, YOU CAN INVOLUNTARILY"
    echo " -------  SKIP SOME IMPORTANT STEPS. IF THIS HAPPENS, USE <^C> TO"
    echo "          STOP THE PROCEDURE AND RESUME FROM THE BEGINNING"
    echo ""
    echo ""
    echo "\007 PRESS <Enter> WHEN READY ...\c"
    read a
    clear

    cat <<xyz
                                                                        1/3
     -----------------CONFIGURE THE VUE WINDOW MANAGER--------------- 
               
- click on the Style Manager (Front Panel, the icon with the color palette)

- click on each of the following icon and change as described hereunder:

  Font 
     click 13.5
     click OK.  Notice: click OK.

  Window
     Focus Follows Mouse     ON   (click on it to set ON)
     Raise Window With Focus OFF  (click on it til backgroud color appears)
     Opaque Move             ON   (click on it til foregroud color appears)

     Place On Desktop        ON   (click on it to set ON)
     click OK.  
     Restart Workspace Manager? click OK 


     >>>The screen is turned off while the wm restarts.<<<

xyz

    echo "\007 PRESS <Enter> WHEN THE SCREEN IS BACK ...\c"
    read a

    clear

    cat <<xyz
(IF YOU DO NOT WANT THIS SIMPLE HOME SESSION,  SKIP THE NEXT TWO SCREENS 
 BY PRESSING <Enter> TWICE)


                                                                        2/3
     -----------------CONFIGURE THE HOME SESSION--------------- 

- close all the clients except the Style Manager and this window.
  (to close: click on [-] and select close from the menu.) 

- open the mailbox flag (center-click with the mouse on the root window, i.e.,
  the background, and select "Mailbox flag" from the menu) and place it on the
  upper-right corner

Except the Style Manager, the current screen will be your Home Session.
If you want, you can now add other clients to these before continuing.

                                                    ../..
xyz
 
    echo "\007 PRESS <Enter> WHEN DONE ...\c"
    read a

    clear 
    cat <<xyz
                                                                        3/3
     -----------------CONFIGURE THE HOME SESSION (cont.)--------------- 

The current screen will be your Home Session.

- click the startup icon the Style Manager and change as described hereunder:

  Startup
     Return to Home session  ON   (click on it to set ON)
     Logout conf. dialog     ON   (click on it to set ON)

     click on  Set Home Session... Continue? click OK 
     click OK

Exit the Style Manager (Click File. Click Exit)

xyz

    echo "\007 PRESS <Enter> WHEN DONE ...\c"
    read a

    clear
    #
    rm $LOCK_FILE

    #
    exitMessage=OK-VLT

fi  # end if VLT-2


if [ $install = USER ] 
then 
    #
    # Shell environment (the same for HP and SUN)
    echo ""
    echo " Restore user C/TC Shell environment\c"
    sleep 1; echo " .\c"
    for file in .login .logout .cshrc .tcshrc
    do
        if [ -f $SAVEDIR/$file ] 
        then
            if [ -f $file ] ; then rm $file ; fi
            mv $SAVEDIR/$file .
        fi
    done
    echo " .\c"; sleep 1; echo " .\c"
    echo " done"

    if [ -f $SAVEDIR/.xsession ] 
    then 
        echo ""
        echo " Restore user MOTIF environment \c"
        sleep 1; echo " .\c"; sleep 1; echo " .\c"
        for file in .xsession .xclients .Xresources .mwmrc
        do
            if [ -f $SAVEDIR/$file ] 
            then
                if [ -f $file ] ; then rm $file ; fi
                mv $SAVEDIR/$file .
            fi
        done
        sleep 1; echo " .\c"; sleep 1; echo " .\c"; sleep 1; echo " .\c"
        echo " done"
    fi

    if [ -d $SAVEDIR/.vue ] 
    then 
        echo ""
        echo " Restore user VUE environment \c"
        sleep 1; echo " .\c"; sleep 1; echo " .\c"
        if [ -d .vue ] ; then rm -r .vue ; fi
        mv $SAVEDIR/.vue .
        sleep 1; echo " .\c"; sleep 1; echo " .\c"
        if [ -f $SAVEDIR/.vueprofile ]
        then 
            if [ -f .vueprofile ] ; then rm .vueprofile ; fi
            mv $SAVEDIR/.vueprofile .
        fi
        sleep 1; echo " .\c"; sleep 1; echo " .\c"
        echo " done"
        #
    fi
    #
    # remove save dir (now it should be empty)
    if rmdir $SAVEDIR 
    then
        exitMessage=OK-USER
    else
        echo ""
        echo "***ERROR: cannot remove save dir $SAVEDIR. INSTALLATION ABORTED."
        echo "------------------------------------------------------------------------------"
        exit 1
    fi

fi  # end if USER

#
# Termination message
# 
echo ""
echo "\007 PRESS <Enter> TO CONTINUE ...\c"
read a

clear

echo "------------------------------------------------------------------------------"
echo "$HEADER"
echo ""

case $exitMessage in
    OK-VLT)
    cat <<xyz

VLT STANDARD ENVIRONMENT INSTALLATION SUCCESSFULLY TERMINATED


         ******************************************************
         * LOGOUT AND REBOOT YOUR TERMINAL TO HAVE IT WORKING *
         ******************************************************


SEE VLT SOFTWARE STANDARD DEVELOPMENT ENVIRONMENT USER MANUAL 
(VLT-MAN-ESO-10000-0477) FOR MORE ON HOW TO CUSTOMIZE THE ENVIRONMENT


------------------------------------------------------------------------------ 
xyz
        ;;

    OK-USER)
    cat <<xyz

USER ENVIRONMENT RESTORED


         ******************************************************
         * LOGOUT AND REBOOT YOUR TERMINAL TO HAVE IT WORKING *
         ******************************************************


SEE VLT SOFTWARE STANDARD DEVELOPMENT ENVIRONMENT USER MANUAL 
(VLT-MAN-ESO-10000-0477) FOR MORE


------------------------------------------------------------------------------ 
xyz
        ;;

    PHASE-2)
        cat <<xyz

                ***********************************
                * PHASE 1 SUCCESSFULLY TERMINATED *
                ***********************************

TO COMPLETE THE INSTALLATION OF VUE NOW YOU NEED TO REBOOT YOUR TERMINAL 
(LOGOUT DOES NOT WORK!). WHEN IN AGAIN:

  - open a terminal windows (click on the terminal symbol on the bottom line
    of the front panel

  - open a full xterm window, using:    xterm -ls -sb & 

  - close the first window ( click on [-] and select close from the menu)

  - in the xterm window call agin this procedure to start the second phase 
    of the installation and follow the instruction given by the procedure.

------------------------------------------------------------------------------ 
xyz
        ;;
esac

exit 0
#
#___oOo___
