#!/bin/bash

checkout(){
	$(USEGIT) && gitcheckout $@ || svncheckout $@
}

reporestore(){
	clear
	echo -en "$c_l"
	ologo

	if [ "$1" == "list" ]
	then
		cd "$brepo"
		bfiles="$(find . -type f |sed 's|./||' |sed 's|.tar.gz||')"
		bcount=$(echo "$bfiles" |wc -w)

		if [ "$bcount" -gt "0" ]
		then
			echo -e "\n$c_l    $bcount$w_l ${REPO} backups found\n    ____________________$re_\n"
			for b in $bfiles
			do
				echo -e "$c_l    --> $w_l$b"
			done
			_nl
		else
			echo -e "\n$r_l    $bcount$w_l ${REPO} backups found\n    ____________________$re_\n"
		fi

		exit
	fi

	if [ -d "${repodir}" ]
	then
		rm -rf "${repodir}"
		printf "$p_l\n  $txt_delete $txt_existing oscam-${REPO} $re_\n"
	else
		printf "$p_l\n  $txt_no oscam-${REPO} $txt_found\n$re_"
	fi
	_nl
	untar_repo $1
	printf "\e[1A\t\t\t\t$y_l""restored\n\n$re_"
	[ -L "$workdir/lastbuild.log" ]&& rm "$workdir/lastbuild.log";
	[ -L "$workdir/lastpatch.log" ]&& rm "$workdir/lastpatch.log";
	[ -f "$workdir/${REPO^^}-IS-PATCHED" ] && rm -f "$workdir/${REPO^^}-IS-PATCHED";
	[ -f "$workdir/EMU_ON" ] && rm -f "$workdir/EMU_ON";
}

repoup(){
	$(USEGIT) && gitup $@ || svnup $@
}

quickreporestore(){
	printf "$1 \n"
	untar_repo $1
}

repopatch(){
	if [ ! -f "$workdir/${REPO^^}-IS-PATCHED" ]
	then
		clear
		printf $C
		ologo
		unset patchlist
		cd "$pdir"
		patchlist=(*.patch)
		patchlog="$(mktemp)"

		if [ "$(ls -A "$pdir"/*.patch 2>/dev/null)" ]
		then
			for p in "${patchlist[@]}"
			do
				echo -e $WH"\n    -> $p <-"$W; echo -e "\n$p" >> "$patchlog"
				if _streamboard_patch "$pdir/$p"; then
					SKIPPATHS=-p0
				else
					SKIPPATHS=-p1
				fi
				sleep .5
				if grep '${REPO^^} binary patch' "$pdir/$p" &>/dev/null; then
					cd "${repodir}"
					echo -e $Y"    binary patch detected, using git apply <-"$W
					git apply --verbose $SKIPPATHS < "$pdir/$p" | tee -a "$patchlog"
				else
					patch -d${repodir}/ $SKIPPATHS < "$pdir/$p" | tee -a "$patchlog" | sed -e "s/^patching file/$G patching > $WH/g;s/^Hunk/$P Hunk     > $Y/g;s/FAILED/$R FAILED/g; ;s/-- saving/$Y -- saving/g;"
				fi
			done

			cat $patchlog >"$ldir/$e.log"
			ln -frs "$ldir/$e.log" "$workdir/lastpatch.log"
			rm -rf "$patchlog"

			mark_patched
		else
			printf $WH"    $txt_no patch $txt_found"
		fi

	else
		clear
		printf $C
		ologo
		printf $WH"    already patched"
	fi
	echo -e $W
}

_dialog_checkout(){
	upc="$(mktemp)"
	(printf " $txt_verify_syscheck "
	[ "$sanity" == "1" ] && printf "ok\n" && sleep 1;

	if [ "${s3cfg_vars[NO_REPO_AUTOUPDATE]}" == "0" ]
	then
		printf  " $txt_verify_svn "
		if [ -f "${repodir}/config.sh" ]
		then
			if check_url "$trunkurl" >/dev/null
			then
				printf "ok\n"
				sleep 1
				printf " $txt_update_svn $txt_wait\n"
				cd "${repodir}"
				$(USEGIT) && git pull -q || svn -q update
				echo 1 >"$upc"
			else
				printf "not ok\n"
			fi
			sleep 1
		fi
	fi;) | "$gui" "$st_" "$bt_" "$title_" "$pb_" 5 52

	if [ "${s3cfg_vars[NO_REPO_AUTOUPDATE]}" == "0" ]
	then
		upc1=$(cat "$upc" 2>/dev/null)
		rm -f "$upc"
		[ ! "$upc1" == "1" ] && check_url "$trunkurl" && _dialog_checkout1

		if [ -f "${repodir}/config.sh" ]
		then
			reset_="$("${repodir}/config.sh" -R)"
		fi
		_get_config_menu
	fi

	if [ -n "$1" ]
	then
		if [ -f "$tccfgdir/$1" ] && [ -f "${repodir}/config.sh" ]
		then
			source "$tccfgdir/$1"
			if [ ! -f "$tcdir/$_toolchainname/bin/$_compiler""gcc" ]
			then
				first="$1"
				_toolchain_gui_install
			fi
			loadprofile="yes"
			_toolchain_build_menu "$1"
		else
			_select_menu
		fi
	fi
	_select_menu
}

_dialog_checkout1(){
	$(USEGIT) && _dialog_checkout1_git $@ || _dialog_checkout1_svn $@
}

REVISION(){
	if [ -d "${repodir}" ]; then
		if cd "${repodir}"; then
			./config.sh --oscam-revision
		fi
	fi
};

COMMIT(){
	if [ -d "${repodir}" ]; then
		if cd "${repodir}"; then
			$(USEGIT) && git log 2>/dev/null | sed -n 1p | cut -d ' ' -f2 | cut -c1-8 || printf ''
		fi
	fi
};

BRANCH(){
	if [ -d "${repodir}" ]; then
		if cd "${repodir}"; then
			$(USEGIT) && git name-rev --name-only "$(COMMIT)" 2>/dev/null | awk -F'/' '{ print $NF }' | awk -F'~' '{ print $1 }' || printf "$trunkurl" | awk -F'/' '{ print $NF }'
		fi
	fi
};

USEGIT(){
	echo $trunkurl | grep -qe '^git@\|.git$';
}

REPOTYPE(){
	$(USEGIT) && printf "git" || printf "svn"
}
