#!/bin/busybox ash
# 2019 Jason Williams

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

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

	if [ ! -s /tmp/.dCore-tor-browser ]; 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 Firefox 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 "tor-\$VERSION.tar.*" version to home or /tmp with another browser"
		echo "and run 'dCore-tor-browser-installer -e' (see dCore-tor-browser-installer --help)."
		echo " "
		exit 1
	fi

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

cd /tmp

SCE=tor-browser
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 "tor*.tar.*" | select "Select the desired Tor Browser file to use:" "-"
	read FILE < /tmp/select.ans
	rm /tmp/select.ans

if [ ! -f "$FILE" ]; then
	echo " "
	exit_red "tor*.tar.* 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
sudo mkdir -p "$EXTDIR"/usr/lib/
sudo mkdir -p "$EXTDIR"/usr/bin
sudo mkdir -p "$EXTDIR"/usr/local/share/applications
sudo mkdir -p "$EXTDIR"/usr/local/share/pixmaps
sudo mkdir -p "$EXTDIR"/usr/local/tce.installed



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

sudo tar xvf "$FILE" -C "$EXTDIR"/usr/lib/ || exit_red "Unpacking source."
DIRNAME=`ls "$EXTDIR"/usr/lib/`
sudo mv "$EXTDIR"/usr/lib/"$DIRNAME" "$EXTDIR"/usr/lib/tor-browser
cd "$EXTDIR"/usr/bin
sudo ln -sf /usr/lib/tor-browser/Browser/start-tor-browser "$SCE"
sudo chown -R "$TCUSER":staff "$EXTDIR"/usr/lib/tor-browser
sudo chmod -R a+x "$EXTDIR"/usr/lib/tor-browser
sudo chmod -R g+r "$EXTDIR"/usr/lib/tor-browser
sudo cp -a /tmp/tcloop/dCore-tor-browser-installer/usr/local/share/dCore-tor-browser-installer/tor-browser.png "$EXTDIR"/usr/local/share/pixmaps/
sudo cp -a /tmp/tcloop/dCore-tor-browser-installer/usr/local/share/dCore-tor-browser-installer/tor-browser.desktop "$EXTDIR"/usr/local/share/applications/
sudo cp -a /tmp/tcloop/dCore-tor-browser-installer/usr/local/share/dCore-tor-browser-installer/tor-browser "$EXTDIR"/usr/local/tce.installed/
sudo chown -R root:staff "$EXTDIR"/usr/local/tce.installed/
sudo chmod -R 775 "$EXTDIR"/usr/local/tce.installed/

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-tor-browser-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-tor-browser-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

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