#!/usr/bin/env bash

if [ "$1" = "--destdir" ]; then
	BBDESTDIR=$2
fi

BBCONFFILE=/etc/bashburnrc
BBROOTDIR=$(dirname $0)/
BBINSTDIR=$(pwd)

overwrite_selection()
{
while true; do
	echo -ne "\033[1;32m|> \033[1;0m"
	read choice
	if [[ "$choice" = "no" ]]; then
		echo
		echo -e "\033[1;31mNot\033[1;0m overwriting config file."
		echo
		uninstall_info
		echo
		echo "Installation finished. Now start BashBurn with the"
		echo -e "command \033[1;36mbashburn\033[1;0m, and remember to"
		echo "configure it before trying to use it."
		echo
		exit 0
	else
		break
	fi
done
}

install_files()
{
	echo; echo -n "Installing files..."
	# Does the path already exist
	if [[ ! -e "${BBDESTDIR}${BBINSTALL_PATH}" ]]; then 
		mkdir -p ${BBDESTDIR}${BBINSTALL_PATH}
	fi
	cp -Rfp ${BBROOTDIR}BashBurn.sh \
		${BBROOTDIR}burning \
		${BBROOTDIR}config \
		${BBROOTDIR}convert \
		${BBROOTDIR}lang \
		${BBROOTDIR}menus \
		${BBROOTDIR}misc \
		${BBROOTDIR}func \
		${BBROOTDIR}HOWTO \
		${BBDESTDIR}${BBINSTALL_PATH}
	
	echo -e "\033[1;32m[DONE]\033[1;0m"; echo

	echo -n "Creating symlink..."
	if [ "$SLACK" = "yes" ]; then
		[ -d ${BBDESTDIR}/usr/bin ] || mkdir -p ${BBDESTDIR}/usr/bin
        	cd ${BBDESTDIR}/usr/bin && ln -sf ${BBINSTALL_PATH}/BashBurn.sh bashburn && cd ${BBINSTDIR}
	else
	# Nick - is this next line right if user selects an alternative install location?
        	cd /usr/local/bin && ln -sf ${BBINSTALL_PATH}/BashBurn.sh bashburn && cd ${BBINSTDIR}
	fi
	echo -e "\033[1;32m[DONE]\033[1;0m"; echo

	if [ "$SLACK" = "yes" ]; then
	echo -e "Setting ownership correctly for"
	echo -en "\033[1;36mSlackware\033[1;0m [root:root]..."

		[ -e ${BBDESTDIR}/usr/bin/bashburn ] && chown root:root ${BBDESTDIR}/usr/bin/bashburn
	chown -R root:root ${BBDESTDIR}/${BBINSTALL_PATH}
	chown -R root:root ${BBDESTDIR}/${BBINSTALL_PATH}/lang
	chown root:root ${BBDESTDIR}/${BBINSTALL_PATH}/HOWTO

	echo -e "\033[1;32m[DONE]\033[1;0m"; echo
	fi

	if [[ -e ${BBCONFFILE} ]]; then
		echo "Config file found! (${BBCONFFILE})"
		echo " Do you want to overwrite it? (yes/no)"
		overwrite_selection
		echo; echo "Backing up existing config file..."
		suffice=$(date +%s)
		cp -f $BBCONFFILE $BBCONFFILE.$suffice
		sleep 1s
		echo "backed up file is $BBCONFFILE.$suffice"
		echo; echo -n 'Installing config file...'
		cp -f ${BBROOTDIR}bashburnrc ${BBCONFFILE} 
		sed -e "s°^BBROOTDIR.*°BBROOTDIR: $BBINSTALL_PATH°" -i ${BBCONFFILE}
		sleep 1s
		echo -e "\033[1;32m[DONE]\033[1;0m"
	else
		echo; echo -n "Installing config file..."

		[ -d $(dirname ${BBDESTDIR}${BBCONFFILE}) ] || mkdir -p $(dirname ${BBDESTDIR}${BBCONFFILE})
		cp -f ${BBROOTDIR}bashburnrc ${BBDESTDIR}${BBCONFFILE}
		sed -e "s°^BBROOTDIR.*°BBROOTDIR: $BBINSTALL_PATH°" -i ${BBDESTDIR}${BBCONFFILE}
		
		sleep 1s
		echo -e "\033[1;32m[DONE]\033[1;0m"
	fi

	echo; echo "Do you want regular users to be able to"
	echo "change the global config file? (yes/no)"
	echo "NOTE: On small systems (Say a computer you and your family uses)"
	echo " this might be a good idea."
	overwrite_selection
	echo; echo -n "Adjusting permissions on ${BBCONFFILE}..."
	chmod 666 ${BBCONFFILE}
	sleep 1s
	echo -e "\033[1;32m[DONE]\033[1;0m"; echo
	echo
	uninstall_info
	echo
	echo "Installation finished. Now start BashBurn with the"
	echo -e "command \033[1;36mbashburn\033[1;0m, and remember to"
	echo "configure it before trying to use it."
	echo 
}

uninstall_info()
{
	echo
	sleep 1s
	echo -e "To remove \033[1;32mBashBurn\033[1;0m from"
	echo "your system, delete these files:"
	if [ "$SLACK" = "yes" ] ; then
	echo "/usr/bin/bashburn  (symlink)"
	else
	echo "/usr/local/bin/bashburn  (symlink)."	
	fi
	echo "/etc/bashburnrc  (configuration file)."
	echo "$BBINSTALL_PATH  (directory)."
	echo 
	echo 
}


echo
echo "Installation script for BashBurn" # Nick - this was forgot to be updated? # Anders - Yes it was
echo

if whoami | grep root > /dev/null ; then
	
	# Nick start Slackware installation
	echo -e "Are you a \033[1;36mSlackware\033[1;0m user and wish to keep the"
	echo "standard installation locations (/usr/bin/)?"
	echo "Type exactly 'yes' or 'no' (default is no): "
	echo -ne "\033[1;32m|> \033[1;0m"
	read SLACK
	if [ "$SLACK" = "yes" ]
	then
	BBINSTALL_PATH="/usr/bin/BashBurn"
	echo -e "[\033[1;36mSlackware\033[1;0m] Installing BashBurn into ${BBINSTALL_PATH}"
	sleep 1s
	install_files
	exit 0
	fi
	# End Slackware

		echo	
		echo "Where do you want to install BashBurn?"
		echo " (Default is /usr/local/BashBurn)"
		echo "Just press enter to use the default location."
		echo -n "Path: "
		read BBINSTALL_PATH
			if [[ "${BBINSTALL_PATH}" = "" ]]; then
			BBINSTALL_PATH="/usr/local/BashBurn"
			echo "Installing BashBurn into ${BBINSTALL_PATH}"
			sleep 1s
			install_files
			else
			install_files
			fi
else
	echo "You're not root, go away."
fi


syntax highlighted by Code2HTML, v. 0.9.1