#!/bin/bash
#
# install.sh
#
# Copyright (C) 2011-2013 Cesnet z.s.p.o
#
# Use of this source is governed by a BSD-style license, see LICENSE file.


VERSION="2.2"

#-------------------------------------------------------------------------------
#				Script functions
#-------------------------------------------------------------------------------
usage()
{
	echo "Usage: ${0##*/} -d <directory> -k <ssl_key_file> -c <ssl_cert_file> -a <ssl_ca_file> [-s <directory>] [-hV]"
	echo "-d <directory>            installation directory of Warden server"
	echo "-k <ssl_key_file>         path to SSL certificate key file"
	echo "-c <ssl_cert_file>        path to SSL certificate file"
	echo "-a <ssl_ca_file>          path to CA certificate file"
	echo "-s <directory>            directory for symlinks to Warden server control scripts (optional)"
	echo "-h                        print this help"
	echo "-V                        print script version number and exit"
	echo
	echo "Example: $ ./${0##*/} -d /opt -k /etc/ssl/private/server.key -c /etc/ssl/certs/server.pem -a /etc/ssl/certs/tcs-ca-bundle.pem -s /usr/local/bin"
	echo
	echo "For more information about installation process, see README file (section Installation)."
	echo
	exit 0
}


version()
{
	echo "${0##*/} - current version is $VERSION"
	exit 0
}


err()
{
	echo "FAILED!"
	cat "$err"
	rm -f "$err" "$modules_file"
	echo
	echo "Installation of $package_version package FAILED!!!"
	exit 1
}


errClean()
{
	echo "FAILED!"
	echo " -> Uninstalling server package ... OK"
	rm -rf "$basedir/"* > /dev/null 2>&1	# BUG: smaze vse na co ma USER pravo
	cat "$err"
	rm -f "$err" "$modules_file"
	echo
	echo "Installation of $package_version package FAILED!!!"
	exit 1
}


paramsChck()
{
	if [ -z "$basedir" ]; then
		echo "Parameter -d <directory> is not set!"
		exit 1
	fi
	if [ -z "$key_file" ]; then
		echo "Parameter -k <ssl_key_file> is not set!"
		exit 1
	fi
	if [ -z "$cert_file" ]; then
		echo "Parameter -c <ssl_cert_file> is not set!"
		exit 1
	fi
	if [ -z "$ca_file" ]; then
		echo "Parameter -a <ssl_ca_file> is not set!"
		exit 1
	fi
}


#-------------------------------------------------------------------------------
#				Installation functions
#-------------------------------------------------------------------------------

oldPackageChck()
{
	echo -n "Checking previous Warden server installation ... "
	old_package_version_file=$(find $basedir -name package_version)
	if [ -f "$old_package_version_file" ]; then
		echo "FAILED!"
		echo
		old_package_version=$(< "$old_package_version_file")
		echo "Sorry, but $old_package_version package is installed!"
		echo "For update of warden server package please use update.sh"
		exit 1
	fi
	echo "OK"
}


perlChck()
{
	echo -n "Checking Perl interpreter ... "
	if which perl 1> /dev/null; then
		echo "OK"
	else
		echo "FAILED!"
		echo
		echo "Error: Perl interpreter is not installed!"
		exit 1
	fi
}

modulesChck()
{
	sed '/^use [A-Z]/!d; /Warden/d' $(find "${dirname}" -type f) 2>/dev/null | cut -f 2 -d " " | sed 's/;//' | sort -u > "$modules_file"
	for module in $(<"$modules_file");
	do
		echo -n "Checking $module module ... "
		perl -e "use $module" 2> "$err" && echo "OK" || err
	done
}


createWardenDir()
{
	echo -n "Creating Warden server directory ... "
	test -d "$basedir" && test -w "$basedir" || mkdir -p "$basedir" 2> "$err" || err
	for dir in "${dirname}/"*
	do
		if [ -d "$dir" ]; then
			cp -R "$dir" "$basedir" 2> "$err" || errClean
		fi
	done
	ln -s "${sh}/uninstall.sh" "$basedir"
	rm -f "${etc}/"*.tmpl
	echo "OK"
}


doTemplate()
{
        vars=""
        while (($#)); do
                vars="'$1'=>'$2', $vars"
                shift 2
        done
        perl -e "my %repl=($vars); foreach my \$l (<>) {\$l =~ s/\$_/\$repl{\$_}/g for keys %repl; print \$l}"
}


createSymlinks()
{
	echo "Creating symbolic links to $symbin ..."
	for file in "${bin}/"*
	do
		echo "- making symlink: ${symbin}/${file##*/} -> $file"
		ln -s "$file" "${symbin}/${file##*/}"
	done
}


#-------------------------------------------------------------------------------
#				MAIN
#-------------------------------------------------------------------------------
# read input
while getopts "d:k:c:a:s:Vh" options; do
	case "$options" in
		d ) basedir="$OPTARG";;
		k ) key_file="$OPTARG";;
		c ) cert_file="$OPTARG";;
		a ) ca_file="$OPTARG";;
		s ) symbin="$OPTARG";;
		h ) usage;;
		V ) version;;
		* ) usage;;
	esac
done

# remove last char (slash) from name of directories
[[ $basedir == */ ]] && basedir="${basedir%?}"
[[ $symbin == */ ]] && symbin="${symbin%?}"

# params test
paramsChck

# set variables
err="/tmp/warden-err"
modules_file="/tmp/warden-modules.txt"
hostname=$(hostname -f || echo "unspecified.server")
dirname=${0%/*}
bin="${basedir}/bin"
lib="${basedir}/lib"
doc="${basedir}/doc"
etc="${basedir}/etc"
sh="${basedir}/sh"
server_conf="${etc}/warden-server.conf"
apache_conf="${etc}/warden-apache.conf"
watchdog_conf="${etc}/warden-watchdog.conf"
server_conf_tmpl="${dirname}/etc/warden-server.conf.tmpl"
apache_conf_tmpl="${dirname}/etc/warden-apache.conf.tmpl"
watchdog_conf_tmpl="${dirname}/etc/warden-watchdog.conf.tmpl"
package_version=$(< "${dirname}/etc/package_version")

echo
echo "----------------------- Checking for Dependencies -----------------------"

# check if warden-server is installed
[[ ! -z $basedir ]] && [[ -d $basedir ]] && oldPackageChck

# Perl interpreter test
perlChck

# Perl modules test
modulesChck

echo
echo "------------------------- Installation process --------------------------"

# make warden server directory
createWardenDir

# create server configuration file
echo -n "Creating server configuration file ... "
doTemplate \
        _BASEDIR_       "$basedir" \
        < $server_conf_tmpl \
        > $server_conf && echo "OK" || errClean

# create Apache configuration file
echo -n "Creating Apache configuration file ... "
doTemplate \
        _CERT_FILE_	"$cert_file" \
        _KEY_FILE_	"$key_file" \
        _CA_FILE_	"$ca_file" \
        _LIB_		"$lib" \
        < $apache_conf_tmpl \
        > $apache_conf && echo "OK" || errClean

# create wardenWatchdog configuration file
echo -n "Creating wardenWatchdog configuration file ... "
doTemplate \
        _HOSTNAME_	"$hostname" \
        < $watchdog_conf_tmpl \
        > $watchdog_conf && echo "OK" || errClean

# crate symlinks from warden server bin to user path if -s option is set
[[ ! -z $symbin ]] && [[ -d $symbin ]] && [[ -w $symbin ]] && createSymlinks

echo
echo "Please check configuration files:"
echo " - $server_conf"
echo " - $apache_conf"
echo
echo "Warden server directory: $basedir"
echo
echo "Please follow post-installation steps in ${doc}/INSTALL!"
echo
echo "Installation of $package_version package was SUCCESSFUL!!!"
echo

# cleanup section
rm -f "$err" "$modules_file"

exit 0
