#!/bin/bash

enable_emu(){
	clear
	s3logo
	if [ -d "$svndir" ]
	then
		printf "$w_l  quicksvnrestore $G$txt_wait\n"
		quicksvnrestore
	else
		printf "$w_l  checkout $G$txt_wait\n"
		checkout
	fi

	pname="oscam-emu.patch"
	source "$configdir/urls"
	emulocal="$dldir/emu_github"

	if [ -f "$emulocal/oscam-emu.patch" ]
	then
		printf "$w_l  Emu Revision  : $y_l"
		grep '\+#define EMU_VERSION' "$emulocal/oscam-emu.patch" |cut -d " " -f 3
		if [ -f "$emulocal/$pname" ]
		then
			cp -rf "$emulocal/$pname" "$svndir"
		fi
	fi

	_nl
	printf $re_
	cd $svndir
	printf $(date)>"$workdir/patch.log"
	patch -p0 < $pname	|tee -a "$workdir/patch.log" \
						|grep --line-buffered -v '^[0-9]\|^/' \
						|sed -e "s@^patching file@$Y  patching file $WH----->$C@g;" \
						|sed -e "s@^Hunk@$P  Hunk@g;" \
						|sed -e "s@FAILED@"$R"FAILED@g;" \
						|sed -e "s@succeeded@"$WH"succeeded@g;"

	fcount=0
	fcount=$(grep -o 'FAILED at' -i "$workdir/patch.log" | wc -l)
	hcount=0;
	hcount=$(grep -o 'Hunk #' -i "$workdir/patch.log" | wc -l)
	if [ ! "$fcount" == "0" ] || [ ! "$hcount" == "0" ]
	then
		printf "\n$WH  Patch Result\n  ------------\n  Hunk Count : $hcount\n"
		printf "  Fail Count : $fcount\n\n"
		# todo ask for restore svn yes / no / show log
else
	printf "\n$WH  Patch Status :$G CLEAN \n"
	touch "$workdir/EMU_ON"
fi
	
	printf $re_
	_nl
}
