#!/bin/sh
# ProvideSupportConsole install script

# installation mode: global = 0, local = 1. Default global.
INSTALL_MODE=0

HOME_CONFDIR=$HOME/.ProvideSupportConsole
GLOBAL_PREFIX=/usr
CONSOLE_SOURCE=console
CONSOLE_NAME=ProvideSupportConsole

CP_BIN="cp --preserve=mode -r"

function usage
{
	echo "Usage: $0 [local]"
	echo "	local  - install application into \$HOME directory"
	exit 1
}

if [ $# -gt 1 ]; then
	echo "Too many arguments."
	usage
elif [ $# -eq 1 ]; then
	if [ "$1" != "local" ]; then
		echo "Incorrect option."
		usage
	else
		INSTALL_MODE=1
	fi
fi

if [ $INSTALL_MODE -ne 1 -a $UID -ne 0 ]; then
	echo "Root privileges are required to install this application globally."
	echo "Run this script with 'local' option to install this application locally "
	echo
	usage
fi

if [ $INSTALL_MODE -eq 0 ]; then
	BIN_DIR=$GLOBAL_PREFIX/bin
	SHARE_DIR=$GLOBAL_PREFIX/share
	PS_SHARE_DIR=$SHARE_DIR/$CONSOLE_NAME

	echo "Installing $CONSOLE_NAME application."
else
	BIN_DIR=$HOME/bin
	SHARE_DIR=$HOME_CONFDIR
	PS_SHARE_DIR=$SHARE_DIR
	echo "Installing $CONSOLE_NAME application locally (into $HOME directory)."
fi

echo -n "Do you want to proceed?(y/n)"

read YN
YN=`echo $YN | tr A-Z a-z`

if [ $YN != "y" -a $YN != "yes" ]; then
 echo "Aborting."
 exit 1
fi

if [ ! -d $BIN_DIR ]; then
	echo "Directory $BIN_DIR doesn't exist. Creating it."
	mkdir -m 755 -p $BIN_DIR
fi

$CP_BIN $CONSOLE_SOURCE/$CONSOLE_NAME $BIN_DIR

if [ ! -d $PS_SHARE_DIR ]; then
	echo "Directory $PS_SHARE_DIR doesn't exist. Creating it."
	mkdir -m 755 -p $PS_SHARE_DIR
fi

for TRG in sound lex; do
	if [ ! -d "$PS_SHARE_DIR/$TRG" ]; then
		echo "Directory $PS_SHARE_DIR/$TRG doesn't exist. Creating it."
		mkdir -m 755 -p $PS_SHARE_DIR/$TRG
	fi

	$CP_BIN $CONSOLE_SOURCE/$TRG/* $PS_SHARE_DIR/$TRG
done

if [ $INSTALL_MODE -eq 0 ]; then
	if [ ! -d "$SHARE_DIR" ]; then
		echo "Directory $SHARE_DIR doesn't exist. Creating it."
		mkdir -m 755 -p $SHARE_DIR
	fi

	for TRG in applications pixmaps icons; do
		if [ ! -d "$SHARE_DIR/$TRG" ]; then
			echo "Directory $SHARE_DIR/$TRG doesn't exist. Creating it."
			mkdir -m 755 -p $SHARE_DIR/$TRG
		fi

		$CP_BIN $CONSOLE_SOURCE/$TRG/* $SHARE_DIR/$TRG
	done
fi

echo "Installation complete."
echo "ProvideSupportConsole executable is located in the $BIN_DIR directory."
