#!/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-skype-installer - Create a skypeforlinux.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.skype.com/en/get-skype/"
	echo " "
	echo "Usage:"
	echo "${YELLOW}"dCore-skype-installer"${NORMAL}     Create a skypeforlinux.sce."
	echo "${YELLOW}"dCore-skype-installer -b"${NORMAL}  Create a skypeforlinux.sce, add SCE to sceboot.lst."
	echo "${YELLOW}"dCore-skype-installer -e"${NORMAL}  Create a skypeforlinux.sce, search home and /tmp"
	echo "        directory for an already downloaded skype*64.deb file."
	echo "${YELLOW}"dCore-skype-installer -r"${NORMAL}  Create a skypeforlinux.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-skype-installer:${NORMAL}" && sleep 3
echo " "
echo "*  This script imports Skype." && 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-skype

	if [ ! -s /tmp/.dCore-skype ]; 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 Skype:" "-" 
	
	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 "skype*.deb" version to home or /tmp with another browser"
		echo "and run 'dCore-skype-installer -e' (see dCore-chrome-installer --help)."
		echo " "
		exit 1
	fi

	echo " "
	echo "*  Opening Skype download page with $BROWSER." && sleep 3
	"$BROWSER" https://www.skype.com/en/get-skype/ > /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
[ -d "$TCEDIR"/import/"$SCE" ] && sudo rm -r "$TCEDIR"/import/"$SCE"
SCE=skypeforlinux
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 "skype*.deb" | select "Select the desired Skype file to use:" "-"
	read FILE < /tmp/select.ans
	rm /tmp/select.ans

if [ ! -f "$FILE" ]; then
	echo " "
	exit_red "skype*.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





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/"$SCE" ] || sudo mkdir -p "$TCEDIR"/import/"$SCE"
	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."
sudo sed -i '/OnlyShowIn/d' usr/share/applications/skypeforlinux.desktop
sudo sed -i '/Actions/,$d' usr/share/applications/skypeforlinux.desktop

cd ..
sudo chown root:root "$SCE"
sudo chmod 775 "$SCE"


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-skype-installer" > "$SCE".sce.dep
	sudo md5sum "$SCE".sce > "$SCE".sce.md5.txt
	sudo chown "$TCUSER":staff "$SCE".sce*
	cd "$IMPORTDIR"
	#echo "${GREEN}Done.${NORMAL}"
	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-skype-installer" > "$SCE".sce.dep
	sudo md5sum "$SCE".sce > "$SCE".sce.md5.txt
	sudo chown "$TCUSER":staff "$SCE".sce*
	cd "$IMPORTDIR"
	#echo "${GREEN}Done.${NORMAL}"
	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/"$SCE" ] && sudo rm -r "$TCEDIR"/import/"$SCE"
[ -d /tmp/"$SCE" ] &&  sudo rm -r /tmp/"$SCE"
