#!/bin/bash

# log_echo
#
# a shell function that calls echo and also logs the output to install.log
#
log_echo ( ) {
	echo "$*"
	echo ">>>>> $*" >> install.log
	echo >> install.log
}

# log_command
#
# a shell function that runs the given command line, logging a header and
# the command's output to install.log. this function returns the exit code
# of the executed command
#
log_command ( ) {
	echo ">>>>> Running command:" >> install.log
	echo ">>>>>   $1" >> install.log
	eval "{ $1; } 2>&1 >> install.log"
	_STATUS=$?
	echo >> install.log
	return $_STATUS
}

# die
#
# a shell function to exit the script with an error. simply prints a message
# telling the user to look in the log for more info
#
die ( ) {
	echo "See install.log for detailed information."
	exit 1
}

# rollback_and_die
#
# a shell function to exit the script with an error, first trying to undo the
# actions of the installer
#
rollback_and_die ( ) {
	log_echo "Undoing previous install actions."
	if ! log_command ./uninstall-teragrid-client
	then
		log_echo "ERROR: failed to roll back partial installation."
	fi
	die
}

# make sure the working dir is inside the untarred package
#
if [ ! -x install-teragrid-client ]
then
	echo "ERROR: Please run this installer from" \
	     "inside the untarred package."
	exit 1
fi

# now that we know we're in the right directory, blow away install.log
# if it exists so we can start a new one
#
rm -f install.log

# say who we are and what we're doing
#
log_echo "Installing the TeraGrid Client Toolkit."

# check if everything's already installed
#
if [ -f install-marker ]
then
	log_echo "Installation already present; exiting."
	exit 0
fi

# check for a previous unfinished install and remove it if present
#
if [ -f tarball-file-list ]
then
	if ! log_command ./uninstall-teragrid-client
	then
		log_echo "ERROR: Unable to remove previously" \
		         "unfinished install."
		die
	fi
fi

# dump out a list of what's in our working dir; this will be used
# by the unintaller as a list of what to keep around
#
log_command "ls > tarball-file-list"

# untar Pacman and setup environment
#
if ! log_command "tar xzf pacman-3.21.tar.gz"
then
	log_echo "ERROR: Unable to untar Pacman."
	rollback_and_die
fi
if ! log_command "cd pacman-3.21 && . ./setup.sh && cd .."
then
	log_echo "ERROR: Error setting up Pacman."
	rollback_and_die
fi

# setup environment to pre-answer the VDT installer's questions
#
VDTSETUP_AGREE_TO_LICENSES=y
export VDTSETUP_AGREE_TO_LICENSES
VDTSETUP_INSTALL_CERTS=l
export VDTSETUP_INSTALL_CERTS
VDTSETUP_EDG_CRL_UPDATE=n
export VDTSETUP_EDG_CRL_UPDATE

# initialize the contents of the Pacman trusted.caches file
#
if ! log_command "cp pacman-caches trusted.caches"
then
	log_echo "ERROR: Error initializing trusted.caches."
	rollback_and_die
fi

# initialize Pacman to use a web proxy if one is given in the environment
#
if [ -n "$TERAGRID_CLIENT_HTTP_PROXY" ]
then
	log_command "pacman -http-proxy ${TERAGRID_CLIENT_HTTP_PROXY}"
fi

# run pacman
#
if ! log_command "./run-pacman"
then
	log_echo "ERROR: Problem installing packages via Pacman."
	rollback_and_die
fi

# install the TeraGrid-specific post-setup scripts
#
if ! log_command "mkdir post-setup && cp setup-teragrid-client.*sh post-setup"
then
	log_echo "ERROR: Problem installing setup scripts."
	rollback_and_die
fi

# touch a marker file to indicate the install succeeded
#
if ! log_command "touch install-marker"
then
	log_echo "ERROR: Couldn't create install marker."
fi

# output an enouraging success message
#
log_echo "TeraGrid Client Toolkit successfully installed."
