#!/bin/busybox ash
# 2017 Jason Williams

. /etc/init.d/tc-functions
checknotroot

if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
	echo " "
	echo "${YELLOW}dCore-seamonkey-installer - Create a seamonkey.sce extension."
	echo " "
	echo "If >1 web browser installed select from list, opens Seamonkey download"
	echo "site, manually download file (ex. seamonkey-\$VERSION.tar.gz) to home"
	echo "(eg. /home/tc/) or /tmp directory, then prompted to create the SCE."
	echo " "
	echo "Seamonkeydownload site:"
	echo "https://www.seamonkey-project.org/releases/#official"
	echo " "
	echo "Usage:"
	echo "${YELLOW}"dCore-seamonkey-installer"${NORMAL}     Create a Seamonkey SCE."
	echo "${YELLOW}"dCore-seamonkey-installer -b"${NORMAL}  Create a Seamonkey SCE, add SCE to sceboot.lst."
	echo "${YELLOW}"dCore-seamonkey-installer -e"${NORMAL}  Create a Seamonkey SCE, search home and /tmp"
	echo "        directory for an already downloaded seamonkey-\$VERSION.tar.gz file."
	echo "${YELLOW}"dCore-seamonkey-installer -r"${NORMAL}  Create a Seamonkey SCE, create SCE in RAM."
	echo " "
exit 1
fi

exit_red() 
{
   echo "${RED}Error: $1  Exiting.${NORMAL}"
   echo " "
   sleep 5
   exit 1
}

echo " "

while getopts rbe OPTION
do
	case ${OPTION} in
		r) RAM=TRUE
		echo "Using the -r option."
		sleep 1
		;;
		b) ONBOOT=TRUE
		echo "Using the -b option."
		sleep 1
		;;
		e) EXISTING=TRUE
		echo "Using the -e option."
		sleep 1
		;;
		*) exit 1 ;;
	esac
done
shift `expr $OPTIND - 1`

NICE=`grep "^NICE=" /etc/sysconfig/sceconfig | cut -f2 -d=`
if [ -n "$NICE" ]; then
	if [ "$NICE" -gt 19 ]; then
		NICE=19
		echo "Using nice level 19, 19 is the highest possible nice value."
	elif [ 0 -gt "$NICE" ]; then
		NICE=0
		echo "Using nice level "$NICE", only root can use negative nice values."
	else
		echo "Using nice level "$NICE"."
	fi
	sleep 1
	/bb/renice -n "$NICE" -p $$
fi

echo " "
echo "${YELLOW}dCore-seamonkey-installer:${NORMAL}" && sleep 3
echo " "
echo "*  This script imports the Seamonkey Web Browser." && sleep 3
if [ "$EXISTING" != TRUE ]; then
	echo "*  Ensure Seamonkey, Firefox, Iceweasel, Nautilus, Midori or Chromium are loaded,"
	echo "   otherwise see dCore-seamonkey-installer --help regarding the -e option." && sleep 3
fi
echo " "
echo -n "Press Enter to proceed: "
read ANS
echo " "

if [ "$EXISTING" != "TRUE" ]; then
	{ which firefox-latest | grep -v ondemand ; which firefox | grep -v ondemand ; which nautilus | grep -v ondemand ; which chromium | grep -v ondemand ; which iceweasel | grep -v ondemand ; which midori | grep -v ondemand; which seamonkey | grep -v ondemand; } > /tmp/.dCore-seamonkey

	if [ ! -s /tmp/.dCore-seamonkey	]; then
		exit_red "No browsers are installed."
	fi
	
	{ which firefox-latest | grep -v ondemand ; which firefox | grep -v ondemand ; which nautilus | grep -v ondemand ; which chromium | grep -v ondemand ; which iceweasel | grep -v ondemand ; which midori | grep -v ondemand; which seamonkey | grep -v ondemand; } | sort | uniq | select "Select a web browser to download Seamonkey Web Browser:" "-" 
	
	read BROWSER < /tmp/select.ans
	rm /tmp/select.ans

	if [ "$BROWSER" == "q" ]; then
		echo " "
		echo "Load Seamonkey, Firefox, Iceweasel, Nautilus, Midori or Chromium and re-run script,"
		echo "otherwise download the "seamonkey-\$VERSION.tar.gz" version to home or /tmp with another browser"
		echo "and run 'dCore-seamonkey-installer -e' (see dCore-seamonkey-installer --help)."
		echo " "
		exit 1
	fi

	echo " "
	echo "*  Opening Seamonkey Web Browser download page with $BROWSER." && sleep 3
	"$BROWSER" https://www.seamonkey-project.org/releases/#official > /dev/null 2>&1 &
	echo "*  Manually download the "seamonkey-\$VERSION.tar.gz" version to home (eg. /home/tc) or /tmp." && sleep 3
	echo " "
	read -p 'When downloaded press Enter to import Seamonkey Web Browser: '
	echo " "
fi

cd /tmp

SCE=seamonkey
TCEDIR="/etc/sysconfig/tcedir"
SCEDIR="$TCEDIR/sce"
EXTDIR="$TCEDIR/import/$SCE"
IMPORTDIR="/etc/sysconfig/tcedir/import"
TCUSER=`cat /etc/sysconfig/tcuser`

sudo find ~ /tmp -name "seamonkey*.tar.*" | select "Select the desired Seamonkey Web Browser file to use:" "-"
	read FILE < /tmp/select.ans


if [ -z "$FILE" ]; then
	exit 0
fi

rm /tmp/select.ans

echo " "
echo "Is the below file the correct one?:"
echo " "
echo "${YELLOW}$FILE${NORMAL} "
echo " "
read -p "Press Enter to proceed, ctrl-c to exit: "

[ -d "$TCEDIR"/import/"$SCE" ] && sudo rm -r "$TCEDIR"/import/"$SCE"
if sudo busybox mount | grep "/tmp/$SCE" > /dev/null 2>&1; then
     sudo busybox umount /tmp/"$SCE"
fi
sudo mkdir -p "$EXTDIR"/usr/lib/

sudo mkdir -p "$EXTDIR"/usr/share/applications
sudo mkdir -p "$EXTDIR"/usr/share/pixmaps
sudo mkdir -p "$EXTDIR"/usr/bin


echo "${BLUE}Copying files and setting permissions...${NORMAL}"

sudo tar xvf "$FILE" -C "$EXTDIR"/usr/lib/ || exit_red "Unpacking source."
cd "$EXTDIR"/usr/bin
sudo ln -sf /usr/lib/seamonkey/seamonkey seamonkey
sudo cp -a /tmp/tcloop/dCore-seamonkey-installer/usr/local/share/dCore-seamonkey-installer/seamonkey.png "$EXTDIR"/usr/share/pixmaps/
sudo cp -a /tmp/tcloop/dCore-seamonkey-installer/usr/local/share/dCore-seamonkey-installer/seamonkey.desktop "$EXTDIR"/usr/share/applications/


cd "$IMPORTDIR"
if [ -f "$TCEDIR"/sce/"$SCE".sce ] && busybox mount | grep " /tmp/tcloop/"$SCE" " > /dev/null 2>&1; then
	echo "${BLUE}Creating "$SCE".sce extension in "$SCEDIR"/update...${NORMAL}"
	sudo mksquashfs "$SCE" "$TCEDIR"/sce/update/"$SCE".sce -noappend > /dev/null 2>&1 || exit_red "Creating extension."
	cd "$SCEDIR"/update/
	echo "dCore-seamonkey-installer" > "$SCE".sce.dep
	sudo md5sum "$SCE".sce > "$SCE".sce.md5.txt
	sudo chown "$TCUSER":staff "$SCE".sce*
	cd "$IMPORTDIR"
	echo "${GREEN}Done.${NORMAL}"
else
	echo "${BLUE}Creating "$SCE".sce extension in "$SCEDIR"...${NORMAL}"
	sudo mksquashfs "$SCE" "$TCEDIR"/sce/"$SCE".sce -noappend > /dev/null 2>&1 || exit_red "Creating extension."
	cd "$SCEDIR"
	echo "dCore-seamonkey-installer" > "$SCE".sce.dep
	sudo md5sum "$SCE".sce > "$SCE".sce.md5.txt
	sudo chown "$TCUSER":staff "$SCE".sce*
	cd "$IMPORTDIR"
	echo "${GREEN}Done.${NORMAL}"
fi

if [ "$ONBOOT" == "TRUE" ]; then
	if ! grep -wq "^$SCE$" "$TCEDIR"/sceboot.lst; then
		echo "$SCE" >> "$TCEDIR"/sceboot.lst
		#echo "$SCE entered in $TCEDIR/sceboot.lst."
	fi
fi

cd /tmp

if sudo busybox mount | grep "/tmp/$SCE" > /dev/null 2>&1; then
     sudo busybox umount /tmp/"$SCE"
fi

[ -d "$TCEDIR"/import/"$SCE" ] && sudo rm -r "$TCEDIR"/import/"$SCE"
sudo rm /tmp/.dCore-seamonkey
