#!/bin/sh
#Tag 0xFFFFFF11
#
# $Id: makeEnv,v 1.8 1999/06/30 12:31:47 david Exp $
#
# Copyright (C) Comunicacion Integral, Las Palmas de Gran Canaria, 1997
#
# $Log: makeEnv,v $
# Revision 1.8  1999/06/30 12:31:47  david
# Removed some options of the interface
# and modified the output of -h option
#
# Revision 1.7  1999/06/10 13:27:36  david
# Check if the user is root
#
# Revision 1.6  1999/05/19 22:02:30  david
# refine
#
# Revision 1.5  1999/04/28 22:01:56  david
# Changed the format of the '-h' documentation option.
#
# Revision 1.4  1999/03/16 23:00:19  david
# Updated to copy ~/.icons/, ~/JALEO-ENV/config/, ~/JALEO-ENV/rtfx/,
# ~/JALEO-ENV/etc/audio & ~/JALEO-ENV/etc/setup.
#
# Revision 1.3  1999/02/22  23:06:05  david
# Update
#
# Revision 1.2  1997/06/12  15:43:43  cm
# general_update
#
# Revision 1.1  1997/06/12  13:28:57  cm
# Initial revision
#
# Shell script for preparing a user account to run Jaleo
#
#    J_SOURCE_DIR
#    J_TARGET_DIR
#    J_SOURCE_VERSION
# Use option -h for a help message
###################################################################

##################################################################
# Definitions
##################################################################

# Activate (J_DEBUG="TRUE") only in case of Debugging:
J_DEBUG="FALSE"
#J_DEBUG="TRUE"

# Minimum path needed for normal execution of unix script
PATH=$PATH:.:~/bin:/usr/sbin:/usr/bsd:/usr/ucb:/bin:/usr/bin:/usr/sbin:/usr/bin/X11
export PATH

# Commands
LS="/usr/bin/ls"
RM="/usr/bin/rm -f"

#Single file copies
CP_SECURE="/usr/bin/cp -f"
CP_NORMAL="/usr/bin/cp"
CP=$CP_SECURE

#Recursive copies
CPR_SECURE="/usr/bin/cp -fr"
CPR_NORMAL="/usr/bin/cp -r"
CPR=$CPR_SECURE

MV="/usr/bin/mv"
MKDIR="/usr/bin/mkdir -p"
LN="/usr/bin/ln -s"
CAT="cat"

# Files to copy
JALEO_ENV="JALEO-ENV"
JALEO_ENV_CP="../.icons .jaleorc JALEO_PRJ JALEO_WORK utils etc/devices etc/colors etc/audio etc/setup config rtfx"
JALEO_ENV_LINK="bin extern lib etc/filters etc/app-defaults etc/curves etc/pixmap etc/config"
JALEO_ENV_DIR="etc"

# Default source and destination setup
J_SOURCE_DIR="/usr/people/jaleo"	
J_SOURCE_VERSION="Jaleo 2.6"
J_TARGET_DIR=$HOME

# Source for creating login files
J_LOGSRC="JALEO-ENV/etc/config/login"  

# Login file handling variables
J_FULL=".cshrc .login .profile .Xdefaults"
J_SGI=".Sgiresources"
J_PREFIX="jaleo"
J_AUXCHEST=".auxchestrc"

# Option variable for update only mode
NO_COPY="FALSE"

# Option variable for overwrite protection
NO_OVER="TRUE"

##################################################################
# Utility Functions
##################################################################

###################################################################
# Validate the Source Directory
validateSourceDirectory() {
    if [ ! -d "$J_SOURCE_DIR/$JALEO_ENV" ] ; then
	echo
	echo "The source path '$J_SOURCE_DIR' does"
	echo "not contain a Jaleo configuration."
	echo "Fix it using a correct '-s' option"
	echo
	echo Please choose a valid Jaleo source path
	echo
	exit 1
    fi
}

###################################################################
# Validate the Update Option
validateUpdateOption() {
    if [ ! -d "$J_TARGET_DIR/$JALEO_ENV" ] ; then
	echo
	echo You can not use the update option -u
	echo while no target configuration exists
	echo
	echo Run makeEnv without the -u option to create a new setup
	echo or run makeEnv -h for help
	echo
	exit 1
    fi
}

##################################################################
# Creating Directories
makeDirs() {
    echo
    echo Checking and/or creating necessary directories
    if [ ! -d "$J_TARGET_DIR/$JALEO_ENV" ] ; then
	    if [ $J_DEBUG = "TRUE" ]      ; then
		    echo "    ***Debug: Creating $J_TARGET_DIR/$JALEO_ENV" 
	    else
		    ${MKDIR} "$J_TARGET_DIR/$JALEO_ENV"
	    fi
    else
	    if [ $J_DEBUG = "TRUE" ]	; then 
		    echo "    ***Debug: Dir $J_TARGET_DIR/$JALEO_ENV already exist."
	    fi
    fi
    
    
    for dir in $JALEO_ENV_DIR ; do
	dir="$J_TARGET_DIR/$JALEO_ENV/$dir"
	if [ ! -d "$dir" ] ; then
	    if [ $J_DEBUG = "TRUE" ]	; then
		    echo "    ***Debug: Creating $dir" 
	    else
		    ${MKDIR} "$dir"
	    fi
	else
	    if [ $J_DEBUG = "TRUE" ]	; then 
		    echo "    ***Debug: Dir $dir already exist."
	    fi
	fi
    done
}

##################################################################
# Copying Directories
copyDirs() {
    echo
    echo Copying configurable files and directories
       
    for dir in $JALEO_ENV_CP ; do
	destino="$J_TARGET_DIR/$JALEO_ENV/$dir"
	source="$J_SOURCE_DIR/$JALEO_ENV/$dir"
	if [ -r "$source" ] ; then
	    if [ $J_DEBUG = "TRUE" ]	; then
		    echo "    ***Debug: Copying $source $destino" 
	    else
		rm -rf $destino
		${CPR} "$source" "$destino"
	    fi
	else
	    if [ $J_DEBUG = "TRUE" ]	; then 
		    echo "    ***Debug: File $destino already exist or can not find $source " 
	    fi
	    if [ ! -r $source ] ; then
	    	echo
		echo "*** Error: Source $source does not exist"
		echo "           This script will try to proceed, but the account will"
		echo "           propably not be usable for Jaleo. Check the source"
		echo "           path for a valid Jaleo installation"     
	    fi
	fi
    done

    ##################################################################
    # Coping the Desktop
    # Note that is needed use tar to avoid the recursive link
    # mkdir $J_TARGET_DIR/Desktop
    # cd $J_SOURCE_DIR/Desktop ; tar cBf - . | (cd $J_TARGET_DIR/Desktop && tar xBf -)
}

##################################################################
# Linking Directories
linkDirs () {
    echo
    echo Linking non-configurable directories
    for dir in $JALEO_ENV_LINK ; do
	destino="$J_TARGET_DIR/$JALEO_ENV/$dir"
	source="$J_SOURCE_DIR/$JALEO_ENV/$dir"	    
	if [ -r "$source" ] ; then
	    if [ $J_DEBUG = "TRUE" ] ; then
		echo "Debug: Linking $source   $destino" 
	    else
		rm -f $destino
		${LN}   "$source" "$destino" 
	    fi
	else
	    if [ $J_DEBUG = "TRUE" ] ; then 
		echo "    ***Debug: File $destino already exist or can not find $source "
	    fi
	fi
    done    
}

##################################################################
# Usage Message
usageMsg () {
    echo 
    echo "NAME"
    echo "        makeEnv   Makes a second Jaleo environment."
    echo 
    echo "SYNOPSIS"
    echo "        makeEnv [-s SourcePath] [-o] [-u] [-h]"
    echo 
    echo "DESCRIPTION"
    echo "        This script makes a second Jaleo's environment in"
    echo "        an empty account using an installed one as source."
    echo "        This second account takes up less than the source"
    echo "        account, as the same files are substituted by links"
    echo "        to the source account."
    echo 
    echo "        A complete description of the format and additional"
    echo "        capabilities are given below."
    echo 
    echo "OPTIONS"
    echo "        [-s SourcePath]   Source path, default is /usr/people/jaleo"
    echo 
    echo "        [-o]   This option is used to force override. You can use"
    echo "               it to recover the configuration."
    echo
    echo "        [-u]   It does not makes a full installation account, it"
    echo "               only updates executables and leaves configuration"
    echo "               files untouched."
    echo 
    echo "        [-h]   This help message"
    echo
    echo "EXAMPLE"
    echo "        To run Jaleo from <new user> account follow these steps:"
    echo "          1) make the new account"
    echo "          2) log in it"
    echo "          3) execute ~MyJaleo/JALEO-ENV/utils/makeEnv -s ~MyJaleo"
    echo 
    echo "        Note that ~MyJaleo is a shortcut"
    echo 
    echo "NOTE"
    echo "        In order to update desktop icons you must do so manually"
    echo "        by dragging the icons you wish from a directory view window"
    echo "        of JALEO-ENV/bin into the desktop of the new account."
    echo 
}

###################################################################
# Main Program
###################################################################
clear

# Before and after check 'whoami', therefore independently of whoami the -h is usefull
if [ "$1" = "-h" ] ; then
    usageMsg
    exit 0
fi

while [ $# -gt 0 ] ; do
    if [ "$1" = "-s" ] ; then
	shift
	if [ $# -gt 0 ] ; then
	    J_SOURCE_DIR=$1
	    shift
	else
	    echo Option -s specified without a new source path
	    exit
	fi
    elif [ "$1" = "-u" ] ; then
	NO_COPY="TRUE"
	shift
    elif [ "$1" = "-o" ] ; then
	NO_OVER="FALSE"
	shift
    elif [ "$1" = "-h" ] ; then
	usageMsg
	exit 0
    else
	echo
	echo Unknown option: $1
	echo 
	echo "See the documentation below."
	echo 
	usageMsg
	exit 0
    fi
done    

echo    =====================================
echo
echo    Jaleo makeEnv
echo
echo	Preparing Account for Jaleo operation  
echo	=====================================
echo
echo

# Set the default X server.
if [ ${DISPLAY:-setdisplay} = setdisplay ]
then
    if [ ${REMOTEHOST:-islocal} != islocal ]
    then
        DISPLAY=${REMOTEHOST}:0
    else
        DISPLAY=:0
    fi
    export DISPLAY
fi

# Getting Commandline Variables
if [ $J_DEBUG = "TRUE" ] ; then 
		echo "    ***Debug: Arguments $# $*"
fi


# Checks
validateSourceDirectory
if [ "$NO_COPY" = "TRUE" ] ; then
    validateUpdateOption # Validate update option only if it is activated
    echo
    echo "WARNING: -u selected. Only executables will be relinked"
    echo "Configuration will remain untouched"
fi

# Output of information related with the below checks
echo
echo Source path option selected: $J_SOURCE_DIR
echo


# Check; exit if source and target directory are equal
if [ "$J_SOURCE_DIR" = "$J_TARGET_DIR" ] ; then
    echo
    echo Source and target directory must not be identical
    echo For help, use option -h
    echo
    exit 1
else
    if [ ! -d "$J_TARGET_DIR/$JALEO_ENV" ] ; then
	echo
	echo Jaleo Target Configuration does not yet exist
    else
	if [ $NO_OVER = "TRUE" ] ; then
	    echo
	    echo Jaleo Target Configuration in selected source path does already exist.
	    echo To force overwrite of an existing installation, use option -o
	    echo For help, use option -h
	    echo
	    exit 1
	else
	    echo
	    echo Jaleo Target Configuration does already exist. It will be overwritten
	    echo
	fi
    fi	
fi


# create directories
makeDirs

# copy directories of configurable files
if [ $NO_COPY = "FALSE" ] ; then
    copyDirs    
fi

# linking directories of non-configurable files
linkDirs

#
# Creating the login files
#
if [ $NO_COPY = "FALSE" ] ; then
    echo 
    echo
    for file in $J_FULL ; do
	echo Creating $file
	${CP} $J_SOURCE_DIR/$J_LOGSRC/$file $J_TARGET_DIR
    done
    
    echo
    for file in $J_SGI ; do
	if [ -f $J_TARGET_DIR/$file ] ; then
	    echo 
	    echo $file already exists - ignored	
	else
	    echo Creating $file
	    ${CP} $J_SOURCE_DIR/$J_LOGSRC/$file $J_TARGET_DIR
	fi
    done
    
    echo
    if [ -f $J_TARGET_DIR/$J_AUXCHEST ] ; then
	echo A desktop configuration file $J_AUXCHEST
	echo does already exist. These files can not
	echo automatically be merged. Creating a file
	echo $J_PREFIX$J_AUXCHEST in the target directory
	echo with the Jaleo toolchest configuration. 
	echo You must do a manual update if you want to 
	echo use the Jaleo toolchest.
	${CP_NORMAL} $J_SOURCE_DIR/$J_LOGSRC/$J_AUXCHEST $J_TARGET_DIR/$J_PREFIX$J_AUXCHEST
    else
	echo Creating desktop configuration file
	${CP} $J_SOURCE_DIR/$J_LOGSRC/$J_AUXCHEST $J_TARGET_DIR
    fi

    echo
    echo "Note: makeEnv does not update desktop icons. You can do so manually"
    echo "      by dragging the icons you wish from a directory view window"
    echo "      of JALEO-ENV/bin in the home directory of the new account."
    echo
    echo "By default,  the new Jaleo environment shares the WORK directory and projects"
    echo "with the source installation. Edit JALEO-ENV/JALEO-WORK to set up a different"
    echo "work directory, and then use the Project Manager to update the project list"
    echo
    echo "You must log out and log in again before you can use Jaleo"
    echo

fi
