#!/bin/bash

_gui_build(){

#init
	oc_size=${#_oscamconfdir_custom}
	[ "$oc_size" -gt "4" ] && _oscamconfdir_default="$_oscamconfdir_custom"

	if [ "$_toolchainname" == "native" ]
	then
		log_name="$(date +%F).$(date +%X).$(hostname).log"
	else
		log_name="$(date +%F).$(date +%X).$_toolchainname.log"
	fi

	ologo >"$ldir/$log_name"
	[ -f "$configdir/max_cpus" ] && cpus="$(cat "$configdir/max_cpus")" || cpus="$(CPUS)"

	CROSS="$tcdir/$_toolchainname/bin/$_compiler"
	SYSROOT="$(realpath -sm $tcdir/$_toolchainname/$_sysroot)"
	_make="make"

	[ "$_stagingdir" == "1" ] && export STAGING_DIR="$tcdir/$_toolchainname"
	[ "$_androidndkdir" == "1" ] && export ANDROID_NDK="$tcdir/$_toolchainname"
	[ -f "$configdir/compiler_option" ] && co=$(cat "$configdir/compiler_option") || co="-O2"

#make clean
	cd "${repodir}"
	make distclean > /dev/null 2>&1
	USESTRING_=''
	EXTRA_USE=''
	libcount=0;
	statcount=0;

#fix streamrelay case 
	check_streamrelay

#do use_vars
	for e2 in $USESTRING
	do
		[ "USE_$e2" == "USE_PATCH" ] && _apply_menupatch
		is_blocked=0

		for e1 in $_block
		do

			if [ "$e1" == "USE_$e2" ]
			then
				is_blocked=1
				[ "USE_$e2" == "USE_LIBUSB" ] && silent=$(./config.sh -D CARDREADER_SMARGO)
			fi

		done
		[ "$is_blocked" == "0" ] && _us="USE_$e2 $_us"
	done

	for e in $_us
	do

		if [ ! "$e" == "USE_TARGZ" ] && [ ! "$e" == "USE_COMPRESS" ] && [ ! "$e" == "USE_PATCH" ]  && [ ! "$e" == "USE_EXTRA" ] && [ ! "$e" == "USE_STATIC" ]
		then
			USESTRING_="$e=1 $USESTRING_"
		fi

	done

	if [ "${USE_vars[USE_LIBCRYPTO]}" == "USE_LIBCRYPTO=1" ]
	then
		((libcount++))
	fi
	
	if [ "${USE_vars[USE_SSL]}" == "USE_SSL=1" ]
	then
		((libcount++))
	fi

	if [ "${USE_vars[USE_LIBUSB]}" == "USE_LIBUSB=1" ]
	then
		((libcount++))
		_usb="-libusb"
	else
		_usb=
	fi

	if [ "${USE_vars[USE_PCSC]}" == "USE_PCSC=1" ]
	then
		((libcount++))
		_pcsc="-pcsc"
	else
		_pcsc=
	fi
	
	if [ "${USE_vars[USE_LIBDVBCSA]}" == "USE_LIBDVBCSA=1" ]
	then
		((libcount++))
		_dvbcsa="-libdvbcsa"
	else
		_dvbcsa=
	fi

#IF SVN is Patched
	if [ -f "$workdir/${REPO^^}-IS-PATCHED" ]
	then
		patch_webif
	fi

#prepare extra_* variables
	if [ ! "${USE_vars[USE_EXTRA]}" == "USE_EXTRA=1" ]
	then
		unset extra_use extra_cc extra_ld extra_c
	fi

#dynamic, static, mixed build
	[ "$_toolchainname" == "native" ] && SEARCHDIR="$(ldconfig -v 2>/dev/null | grep -v ^$'\t' | awk -F':' '{print $1}')" || SEARCHDIR="$SYSROOT"
	[ "${USE_vars[USE_LIBCRYPTO]}" == "USE_LIBCRYPTO=1" ] && [ "${USE_vars[USE_STATIC]}" == "USE_STATIC=1" -o "${USE_vars[STATIC_LIBCRYPTO]}" == "STATIC_LIBCRYPTO=1" ]	&& LIBCRYPTO_STATIC="LIBCRYPTO_LIB=$(find $SEARCHDIR -name "libcrypto.a" -type f -print -quit)"	&& ((statcount++))
	[ "${USE_vars[USE_SSL]}" == "USE_SSL=1" ]             && [ "${USE_vars[USE_STATIC]}" == "USE_STATIC=1" -o "${USE_vars[STATIC_SSL]}" == "STATIC_SSL=1" ]				&& SSL_STATIC="SSL_LIB=$(find $SEARCHDIR -name "libssl.a" -type f -print -quit)"					&& ((statcount++))
	[ "${USE_vars[USE_LIBUSB]}" == "USE_LIBUSB=1" ]       && [ "${USE_vars[USE_STATIC]}" == "USE_STATIC=1" -o "${USE_vars[STATIC_LIBUSB]}" == "STATIC_LIBUSB=1" ]		&& LIBUSB_STATIC="LIBUSB_LIB=$(find $SEARCHDIR -name "libusb-1.0.a" -type f -print -quit)"		&& ((statcount++))
	[ "${USE_vars[USE_PCSC]}" == "USE_PCSC=1" ]           && [ "${USE_vars[USE_STATIC]}" == "USE_STATIC=1" -o "${USE_vars[STATIC_PCSC]}" == "STATIC_PCSC=1" ]			&& PCSC_STATIC="PCSC_LIB=$(find $SEARCHDIR -name "libpcsclite.a" -type f -print -quit)"			&& ((statcount++))
	[ "${USE_vars[USE_LIBDVBCSA]}" == "USE_LIBDVBCSA=1" ] && [ "${USE_vars[USE_STATIC]}" == "USE_STATIC=1" -o "${USE_vars[STATIC_LIBDVBCSA]}" == "STATIC_LIBDVBCSA=1" ]	&& DVBCSA_STATIC="LIBDVBCSA_LIB=$(find $SEARCHDIR -name "libdvbcsa.a" -type f -print -quit)"		&& ((statcount++))

	if [ $statcount -ge 1 -a $statcount -lt $libcount ]
	then
		buildtype="-mixed"
	elif [ "${USE_vars[USE_STATIC]}" == "USE_STATIC=1" -o $statcount -ge 1 -a $libcount -ge 1 -a $statcount -eq $libcount ]
	then
		_make="make static"
		buildtype="-static"
	else
		buildtype=""
	fi

	USESTRING_=${USE_vars[@]}
	EXTRA_USE=$extra_use

#todo add switch for verbose
	_generate_oscam_name "$_toolchainname"
	oscam_name="$oscam_name$buildtype"
	targztmp="$(mktemp)"

	if [ ! "${USE_vars[USE_TARGZ]}" == "USE_TARGZ=1" ]
	then
		s3cfg_vars[USE_TARGZ]=0
	fi
	check_smargo

#build
	_sz
	(ologo; _nl
	timer_start
	$_make -j"$cpus" \
		"CONF_DIR=$_oscamconfdir_default" "OSCAM_BIN=$bdir/$oscam_name" "CC_OPTS=$co $cc_opts $extra_cc" "CC_WARN=$cc_warn" "EXTRA_LDFLAGS=$extra_ld" "EXTRA_CFLAGS=$extra_c" $EXTRA_USE "CROSS=$CROSS" $stapivar $USESTRING_ $LIBCRYPTO_STATIC $SSL_STATIC $LIBUSB_STATIC $PCSC_STATIC $DVBCSA_STATIC 2>&1 \
		|tee "$ldir/$log_name" \
		|grep --line-buffered -v 'BFD\|^/' \
		|grep --line-buffered '^CC\|^GEN\|UseFlags\|  CONF_DIR =\|Binary\|LINK\|STRIP\|BUILD\|Addons\|Protocols\|Readers\|CardRdrs\|^/' \
		|sed -u "s/^|/ |/g;s/^LINK/ |  LINK  >/g;s/^STRIP/ |  STRIP >/g;s/^CC\|^BUILD/ |  BUILD >/g;s/^GEN/ |  GEN   >/g;s/WEBIF_//g;s/WITH_//g;s/MODULE_//g;s/CS_//g;s/HAVE_//g;s/_CHARSETS//g;s/CW_CYCLE_CHECK/CWCC/g;s/SUPPORT//g;s/= /: /g;"
	timer_stop
	timer_calc

#save list_smargo
	cd "${repodir}/Distribution"
	lsmn="$(ls list_smargo* 2> /dev/null)"

	if [ "${s3cfg_vars[SAVE_LISTSMARGO]}" == "1" ] && [ -f "${repodir}/Distribution/$lsmn" ]
	then

		if [ "$_toolchainname" == "native" ]
		then
			printf "SAVE\t$lsmn $txt_as oscam-${REPO}$(REVISION)$($(USEGIT) && printf "@$(COMMIT)" || printf "")-$(hostname)-list_smargo"| tee -a "$ldir/$log_name"
			mv -f "$lsmn" "$bdir/oscam-${REPO}$(REVISION)$($(USEGIT) && printf "@$(COMMIT)" || printf "")-$(hostname)-list_smargo"
			printf "oscam-${REPO}$(REVISION)$($(USEGIT) && printf "@$(COMMIT)" || printf "")-$(hostname)-list_smargo" >"$targztmp"
		else
			printf "SAVE\t$lsmn $txt_as oscam-${REPO}$(REVISION)$($(USEGIT) && printf "@$(COMMIT)" || printf "")-$_toolchainname-list_smargo"|tee -a "$ldir/$log_name"
			mv -f "$lsmn" "$bdir/oscam-${REPO}$(REVISION)$($(USEGIT) && printf "@$(COMMIT)" || printf "")-$_toolchainname-list_smargo"
			printf "oscam-${REPO}$(REVISION)$($(USEGIT) && printf "@$(COMMIT)" || printf "")-$_toolchainname-list_smargo" >"$targztmp\n"
		fi

	fi

#show buildtime
	printf "\n |  TIME  >\t[ $txt_buildtime $((Tcalc / 60)) min(s) $((Tcalc % 60)) secs ]\n"| tee -a "$ldir/$log_name"
	sleep 1

#remove debug binary
	if [ "${s3cfg_vars[delete_oscamdebugbinary]}" == "1" ] && [ -f "$bdir/$oscam_name.debug" ]
	then
		rm "$bdir/$oscam_name.debug"
		printf "\n $txt_delete $oscam_name.debug\n"|tee -a "$ldir/$log_name"
	fi;) | "$gui" "$st_" --colors --title " -[ Build ]- " "$pb_" "$_lines" "$_cols"
	sleep 2

#COMPRESS
	if [ ! "$stapi_allowed" == "1" ]
	then

		if [ "${s3cfg_vars[COMPRESS]}" == "1" ] || [ "${USE_vars[USE_COMPRESS]}" == "USE_COMPRESS=1" ]
		then
			(compress_cam_gui "$oscam_name") |tee -a "$ldir/$log_name"| "$gui" "$st_" --title " -[ COMPRESS ]- " "$pb_" 7 50
			sleep 2
		fi

	fi

#tar
	if [ "${USE_vars[USE_TARGZ]}" == "USE_TARGZ=1" ]
	then
		(tar_cam_gui "$oscam_name" "$(cat "$targztmp")") |tee -a "$ldir/$log_name"| "$gui" "$st_" --title " -[ TAR Binary ]- " "$pb_" 10 70
		sleep 2
	fi

#link log
	ln -frs "$ldir/$log_name" "$workdir/lastbuild.log"
	[ -f "$targztmp" ] && rm -rf "$targztmp"
}
