#!/bin/busybox ash
# 2017 Jason Williams

. /etc/init.d/tc-functions
checknotroot
sudo chown root:staff /tmp
sudo chmod 1777 /tmp

if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
	echo " "
	echo "${YELLOW}dCore-chrome-installer - Create a google-chrome-stable.sce extension."
	echo " "
	echo "If >1 web browser installed select from list, opens Chrome download"
	echo "site, manually download the .deb package to home"
	echo "(eg. /home/tc/) or /tmp directory, then prompted to create the SCE."
	echo " "
	echo "Chrome download site:"
	echo "https://www.google.com/chrome/browser/desktop/index.html"
	echo " "
	echo "Usage:"
	echo "${YELLOW}"dCore-chrome-installer"${NORMAL}     Create a google-chrome.sce."
	echo "${YELLOW}"dCore-chrome-installer -b"${NORMAL}  Create a google-chrome.sce, add SCE to sceboot.lst."
	echo "${YELLOW}"dCore-chrome-installer -e"${NORMAL}  Create a google-chrome.sce, search home and /tmp"
	echo "        directory for an already downloaded google-chrome*amd64.deb file."
	echo "${YELLOW}"dCore-chrome-installer -r"${NORMAL}  Create a google-chrome.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-chrome-installer:${NORMAL}" && sleep 3
echo " "
echo "*  This script imports the Google Chrome Web Browser." && sleep 3
if [ "$EXISTING" != TRUE ]; then
	echo "*  Ensure Seamonkey, Firefox, Iceweasel, Nautilus, Chrome, Midori or Chromium are loaded,"
	echo "   otherwise see dCore-chrome-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 google-chrome | 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-chrome

	if [ ! -s /tmp/.dCore-chrome ]; then
		exit_red "No browsers are installed."
	fi
	
	{ which firefox-latest | grep -v ondemand ; which google-chrome | 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 Google Chrome Web Browser:" "-" 
	
	read BROWSER < /tmp/select.ans
	rm /tmp/select.ans

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

	echo " "
	echo "*  Opening Google Chrome Web Browser download page with $BROWSER." && sleep 3
	"$BROWSER" https://www.google.com/chrome/browser/desktop/index.html > /dev/null 2>&1 &
	echo "*  Manually download the .deb version to home (eg. /home/tc) or /tmp." && sleep 3
	echo " "
	read -p 'When downloaded press Enter to import Google Chrome Web Browser: '
	echo " "
fi

cd /tmp

SCE=google-chrome
TCEDIR="/etc/sysconfig/tcedir"
SCEDIR="$TCEDIR/sce"
#WORK="$TCEDIR"/import/$SCEwork"
EXTDIR="$TCEDIR/import/$SCE"
IMPORTDIR="/etc/sysconfig/tcedir/import"
TCUSER=`cat /etc/sysconfig/tcuser`

sudo find ~ /tmp -name "google-chrome*.deb" | select "Select the desired Google Chrome Web Browser file to use:" "-"
	read FILE < /tmp/select.ans
	rm /tmp/select.ans

if [ ! -f "$FILE" ]; then
	echo " "
	exit_red "google-chrome*.deb file not found."
fi
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
[ -d /tmp/google-chrome ] && sudo rm -r /tmp/google-chrome




if [ RAM == "TRUE" ]; then
	EXTDIR="/tmp/$SCE"
	[ -d /tmp/"$SCE" ] || sudo mkdir -p /tmp/"$SCE"
	cd /tmp/"$SCE"
else
	EXTDIR="$TCEDIR/import/$SCE"
	[ -d "$TCEDIR"/import/google-chrome ] || sudo mkdir -p "$TCEDIR"/import/google-chrome
	cd "$TCEDIR/import/$SCE"
fi

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

sudo ar p "$FILE" data.tar.xz | sudo unxz | sudo tar x || exit_red "Unpacking source."


cd ..
sudo chown root:root "$SCE"
sudo chmod 775 "$SCE"
sudo mkdir -p "$SCE"/usr/local/tce.installed
cd "$EXTDIR"/usr/local/tce.installed
sudo cp -p /usr/share/dCore-chrome-stable-installer/google-chrome .
[ -d "$EXTDIR"/usr/share/pixmaps ] || sudo mkdir -p "$EXTDIR"/usr/share/pixmaps
sudo cp -p /usr/share/dCore-chrome-stable-installer/google-chrome.png "$EXTDIR"/usr/share/pixmaps/
echo "Icon=google-chrome" | sudo /bb/tee -a "$EXTDIR"/usr/share/applications/google-chrome.desktop > /dev/null 2>&1
cd ..
sudo chmod -R 775 tce.installed
sudo chown -R root:staff tce.installed
cd "$EXTDIR"/usr/bin
sudo ln -sf google-chrome-stable google-chrome


if [ RAM == "TRUE" ]; then
	cd /tmp/
else
	cd "$TCEDIR/import"
fi
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-chrome-stable-installer" > "$SCE".sce.dep
	sudo md5sum "$SCE".sce > "$SCE".sce.md5.txt
	sudo chown "$TCUSER":staff "$SCE".sce*
	cd "$IMPORTDIR"
	read -rsn1 -p"${GREEN}Done!  Press any key to exit.${NORMAL}";echo
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-chrome-stable-installer" > "$SCE".sce.dep
	sudo md5sum "$SCE".sce > "$SCE".sce.md5.txt
	sudo chown "$TCUSER":staff "$SCE".sce*
	cd "$IMPORTDIR"
	read -rsn1 -p"${GREEN}Done!  Press any key to exit.${NORMAL}";echo
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

sudo chown root:staff /tmp
sudo chmod 1777 /tmp

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