#! /bin/bash

################################################################################
# Usage: $0 [<in-filename>]
# 
# arguments
# 1. [optional] package-names would be read from this files, instead of prompting user for the same
################################################################################

clear
echo "--------------------------------------------------------------------------------"
echo "                 (rapt) Script for installing packages (client)                 "
echo "--------------------------------------------------------------------------------"

# ensure script is run as root/sudo
if [ "$(id -u)" != "0" ]
then
	echo ""
	echo "Must execute the script as root user."
	echo "--------------------------------------------------------------------------------"
	exit 1
fi

# check the argument count
if [ $# -gt "1" ]
then
	echo ""
	echo "Usage: $0 [<package-name.lst filename>]"
	echo "--------------------------------------------------------------------------------"
	exit 1
fi

################################################################################
#### getPackageNames
#### operation: input package name(s) to be installed
#### args: (1)
#### 1. [out] List of input package names
################################################################################
function getPackageNames
{
	if [ $# -ne "1" ]
	then
		echo ${FUNCNAME} " - Missing Arguments."
		echo "Usage: " ${FUNCNAME} " <out package list>"
		return 1
	fi
	
	local PACKAGE_LIST="" 
	choice="y"
	while [[ "$choice" == "y" || "$choice" == "Y" ]]
	do
		typeset -a inputArray
		echo -n "Please enter the package name(s): "
		read -a inputArray
		PACKAGE_LIST=$(echo ${PACKAGE_LIST} " " "${inputArray[@]}")
		choice="n"
		echo -n "Do you wish to enter more packages (y/n): (n)"
		read choice
	done
	eval "$1=\"${PACKAGE_LIST}\""
}

# package names to be installed
PACKAGE_NAME_LIST=""

# check if filename was supplied as comand line parameter
if [ $# -eq "1" ]
then
	PACKAGE_NAME_LIST=$(cat $1 | grep -v -e "^#" | cut -f1)
else
	getPackageNames PACKAGE_NAME_LIST
fi

echo ""
echo "Installing packages:" ${PACKAGE_NAME_LIST}
echo "--------------------------------------------------------------------------------"

apt-get install ${PACKAGE_NAME_LIST}

echo ""
echo "Done"
echo "--------------------------------------------------------------------------------"


