#!/bin/busybox ash
# 2017 Jason Williams

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

if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
	echo " "
	echo "${YELLOW}dCore-realplayer11-installer - Create a realplayer11.sce extension."
	echo " "
	echo "Usage:"
	echo "${YELLOW}"dCore-realplayer11-installer"${NORMAL}     Create a realplayer11 SCE."
	echo "${YELLOW}"dCore-realplayer11-installer -b"${NORMAL}  Create a realplayer11 SCE, add SCE to sceboot.lst."
	echo "${YELLOW}"dCore-realplayer11-installer -e"${NORMAL}  Create a realplayer11 SCE, search home and /tmp"
	echo "        directory for an already downloaded RealPlayer11GOLD.deb file."
	echo " "
exit 1
fi

echo " "

while getopts be OPTION
do
	case ${OPTION} in

		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-realplayer11-installer:${NORMAL}" && sleep 3
echo " "
echo "*  This script imports realplayer11." && sleep 3
echo " "
echo -n "Press Enter to proceed: "
read ANS
echo " "

if [ "$EXISTING" != "TRUE" ]; then
	cd ~
	wget http://client-software.real.com/free/unix/RealPlayer11GOLD.deb
fi

cd /tmp

exit_red() 
{
   echo "${RED}Error: $1  Exiting.${NORMAL}"
	[ -d /etc/sysconfig/tcedir/import/realplayer11 ] && sudo rm -r /etc/sysconfig/tcedir/import/realplayer11
	[ -d /tmp/scerealplayer11 ] && sudo rm -r /tmp/scerealplayer11
   echo " "
   sleep 5
   exit 1
}

SCE=realplayer11
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 "RealPlayer11GOLD.deb" | select "Select the desired Real Player file to use:" "-"
	read FILE < /tmp/select.ans
	rm /tmp/select.ans

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


sudo mkdir -p /tmp/scerealplayer11

echo "${BLUE}Copying files and setting permissions...${NORMAL}"
cd /tmp/scerealplayer11
sudo ar x "$FILE" || exit_red "Unpacking source."
sudo mkdir pkg
sudo tar xf data.tar.gz -C pkg
sudo cp -a pkg/ "$EXTDIR"
cd "$EXTDIR"
sudo mkdir -p usr/lib/mozilla/plugins/
sudo ln -sf /opt/real/RealPlayer/mozilla/nphelix.so usr/lib/mozilla/plugins/
sudo mkdir -p usr/bin
sudo ln -sf /opt/real/RealPlayer/realplay usr/bin/realplay
sudo mkdir -p usr/share/applications
sudo mkdir -p usr/share/pixmaps
sudo ln -sf /opt/real/RealPlayer/share/icons/realplay_192x192.png usr/share/pixmaps/realplayer11.png
sudo cp /tmp/tcloop/dCore-realplayer11-installer/usr/local/share/dCore-realplayer11-installer/realplayer11.desktop 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}"
	cd "$IMPORTDIR"
	sudo mksquashfs "$SCE" "$TCEDIR"/sce/update/"$SCE".sce -noappend > /dev/null 2>&1 || exit_red "Creating extension."
	cd "$SCEDIR"/update/
	echo "dCore-realplayer11-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}"
	cd "$IMPORTDIR"
	sudo mksquashfs "$SCE" "$TCEDIR"/sce/"$SCE".sce -noappend > /dev/null 2>&1 || exit_red "Creating extension."
	cd "$SCEDIR"
	echo "dCore-realplayer11-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

[ -d /etc/sysconfig/tcedir/import/realplayer11 ] && sudo rm -r /etc/sysconfig/tcedir/import/realplayer11
[ -d /tmp/scerealplayer11 ] && sudo rm -r /tmp/scerealplayer11



