#!/bin/bash

_cmd_build(){
	[ ! -d "${repodir}" ] || [ -f "$workdir/NEED-CHECKOUT" ] && checkout

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

	source "$tccfgdir/$_toolchainname"
	_reset_config
	printf $WH
	ologo >"$ldir/$log_name"

#set build defaults
	CROSS="$tcdir/$_toolchainname/bin/$_compiler"
	SYSROOT="$(realpath -sm $tcdir/$_toolchainname/$_sysroot)"
	[ "$_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"

#toolchain defaults
	for defa in $default_use
	do
		USE_vars[$defa]="$defa=1"
	done

#disable by cmd
	for e in ${USE_vars[*]}
	do

		for d in ${USE_vars_disable[*]}
		do
			[ "$e" == "$d" ] && USE_vars[${e:0: -2}]=;
		done

		if [ $e == "USE_DIAG=1" ]; then
			codecheck=$(command -v scan-build)
		fi
	done
	cd "${repodir}"

#make clean
	make distclean > /dev/null 2>&1;

#patching
	if [ "${USE_vars['USE_PATCH']}" == "USE_PATCH=1" ]
	then
		if [ -f "$workdir/${REPO^^}-IS-PATCHED" ]
		then
			printf "$y_l\n |  $r_l WARNING : patch upon patch"
			printf "$y_l\n |  $r_l UNCLEAN $y_l: restore SVN $G$txt_wait"
			quickreporestore $_toolchainname
			_apply_consolepatch
		else
			_apply_consolepatch
		fi
	fi

#do enable and disable modules
	for am in "${all_cc[@]}"
	do

		chose="false"
		if [ "${am:${#am} - 3}" == "_on" ]
		then
			e="${am:0: -3}"
			"${repodir}/config.sh" -E "$e" |awk '{printf "\033[1;37m"} {printf " |    %s : ", $1} {printf "\033[1;32m"} {printf "%s\n", $2}'
		fi

		if [ "${am:${#am} - 4}" == "_off" ]
		then
			d="${am:0: -4}"
			"${repodir}/config.sh" -D "$d" |awk '{printf "\033[1;37m"} {printf " |   %s : ", $1} {printf "\033[1;31m"} {printf "%s\n", $2}'
		fi

	done
	echo -en "\e[1A"

#fix smargo case
	if [ "$(./config.sh -e CARDREADER_SMARGO)" == "Y" ] || [ "${USE_vars[$e]}" == "USE_LIBUSB=1" ]
	then
		silent=$(./config.sh -E CARDREADER_SMARGO)
		check_smargo
	else
		silent=$(./config.sh -D CARDREADER_SMARGO)
		check_smargo
	fi

#fix streamrelay case 
	check_streamrelay

#fill use variables and set name addons
	USESTRING=;
	EXTRA_USE=;
	libcount=0;
	statcount=0;
	_usb=;
	_pcsc=;
	_dvbcsa=;
	_stapi=;
	_stapi5=;
	_make="make"
	for e in "${!USE_vars[@]}"
	do
		for e1 in $_block
		do
			USE_vars[$e1]=;
		done

		uv=${USE_vars[$e]}
		if [ ! "$e" == "USE_CONFDIR" ]
		then
			USESTRING="$uv $USESTRING"
			if [ "${#USE_vars[$e]}" -gt "5" ]
			then
				printf "\n$y_l |       set : ${USE_vars[$e]}"
			fi
		fi

		case "$uv" in
			"USE_LIBCRYPTO=1")
					((libcount++));;
			"USE_SSL=1")
					((libcount++));;
			"USE_LIBUSB=1")
					((libcount++));
					_usb="-libusb";;		# set libusb suffix to name
			"USE_PCSC=1")
					((libcount++));
					_pcsc_on
					_pcsc="-pcsc";;			# set pcsc suffix to name
			"USE_LIBDVBCSA=1")
					((libcount++));
					_dvbcsa="-libdvbcsa";;	# set libdvbcsa suffix to name
			"USE_TARGZ=1")
					s3cfg_vars[TARGZ]=1;;	# overwrite global
			"USE_COMPRESS=1")
					s3cfg_vars[COMPRESS]=1;;# overwrite global
			"USE_STATIC=1")
					_make="make static";;
			"USE_STAPI=1")
					_stapi="-stapi"
					[ -z "$stapi_lib_custom" ] && STAPI_LIB="STAPI_LIB=$sdir/stapi/liboscam_stapi.a" || STAPI_LIB="STAPI_LIB=$sdir/stapi/${stapi_lib_custom}"
					printf "$y_l\n |       LIB : ${c_l}$(basename "$STAPI_LIB")${w_l}";;
			"USE_STAPI5=1")
					_stapi5="-stapi5"
					[ "$OPENBOX" == "1" ] && STAPI_LIB="STAPI5_LIB=$sdir/stapi/liboscam_stapi5_OPENBOX.a" && printf "$y_l\n |       LIB : "$c_l"liboscam_stapi5_OPENBOX.a"$w_l
					[ "$UFS916003" == "1" ] && STAPI_LIB="STAPI5_LIB=$sdir/stapi/liboscam_stapi5_UFS916_0.03.a" && printf "$y_l\n |       LIB : "$c_l"liboscam_stapi5_UFS916_0.03.a"$w_l
					[ "$UFS916003" == "0" ] && [ "$OPENBOX" == "0" ] && STAPI_LIB="STAPI5_LIB=$sdir/stapi/liboscam_stapi5_UFS916.a" && printf "$y_l\n |       LIB : "$c_l"liboscam_stapi5_UFS916.a"$w_l;;
		esac

	done

#change default oscam CONF_DIR

CONFDIR="$_oscamconfdir_default"
cdtag="CONF_DIR=$CONFDIR"

if [ ! -z "$_oscamconfdir_custom" ];
then
	if [ ! "$_oscamconfdir_custom" == "not_set" ];
	then
		CONFDIR="$_oscamconfdir_custom"
		cdtag="${r_l}custom CONF_DIR=$_oscamconfdir_custom (via toolchain.cfg)"
	fi
fi

if [ ! "$CUSTOM_CONFDIR" == "not_set" ];
then
	CONFDIR="$CUSTOM_CONFDIR"
	cdtag="${r_l}custom CONF_DIR=$CUSTOM_CONFDIR (via CUSTOM_CONFDIR)"
fi

printf "$y_l\n |       set : ${cdtag}$w_l"

#if build with profile
	[ ! "$pf" == "empty" ] && printf "\n$y_l |   PROFILE : $pf_name"

#IF SVN is Patched
	if [ -f "$workdir/${REPO^^}-IS-PATCHED" ]
	then
		printf "$y_l\n | ISPATCHED :$P YES (integrate patch infomation into WebIf)"
		patch_webif
	fi

#prepare extra_* variables
	if [ "${USE_vars[USE_EXTRA]}" == "USE_EXTRA=1" ]
	then
		printf "$y_l\n | EXTRAFLAGS: enabled"
	else
		printf "$y_l\n | EXTRAFLAGS: disabled"
		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
		printf "$y_l\n | BUILDTYPE : mixed"
		buildtype="-mixed"
	elif [ "${USE_vars[USE_STATIC]}" == "USE_STATIC=1" -o $statcount -ge 1 -a $libcount -ge 1 -a $statcount -eq $libcount ]
	then
		printf "$y_l\n | BUILDTYPE : static"
		buildtype="-static"
	else
		printf "$y_l\n | BUILDTYPE : dynamic"
		buildtype=""
	fi

#max cpu usage
	if [ -f "$configdir/max_cpus" ]
	then
		cpus="$(< "$configdir/max_cpus")"
		[ ! "$cpus" -gt "1" ] && cpus="1"
		[ "$cpus" -gt "$(CPUS)" ] && cpus="$(CPUS)"
		printf "$y_l\n |  MAX_CPUS : $txt_use $cpus $txt_of $(CPUS) CPU(s)"
	else
		cpus="$(CPUS)"
	fi

	[ "${s3cfg_vars[USE_VERBOSE]}" == "1" ] && _verbose="V=1"

#build
if [ ${#USE_vars[USE_OSCAMNAME]} -gt 0 ]
then
	oscam_name=$(echo ${USE_vars[USE_OSCAMNAME]} |cut -d "=" -f2)
else
	_generate_oscam_name "$_toolchainname"
fi
	oscam_name="$oscam_name$buildtype"
	_nl
	USESTRING=${USE_vars[@]}
	EXTRA_USE=$extra_use
	timer_start
	$codecheck $_make -j"$cpus" $_verbose \
	"CONF_DIR=$CONFDIR" \
	"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" $STAPI_LIB $USESTRING $LIBCRYPTO_STATIC $SSL_STATIC $LIBUSB_STATIC $PCSC_STATIC $DVBCSA_STATIC 2>&1 \
	|tee -a "$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 -e "s/^|/"$Y" |/g;s/^LINK/"$P" LINK ------->$W/g;s/^STRIP/"$P" STRIP ------>$W/g;s/^CC\|^BUILD/"$G" BUILD ------>$W/g;s/^GEN/"$C" 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;"

#calc buildtime
	timer_stop
	timer_calc
	bt="[ $txt_buildtime $((Tcalc / 60)) min(s) $((Tcalc % 60)) secs ]"

#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 "$g_n"" SAVE -------> $w_l$lsmn$g_l $txt_as$y_l oscam-${REPO}$(REVISION)$($(USEGIT) && printf "@$(COMMIT)" || printf "")-$(hostname)-list_smargo"
			mv -f "$lsmn" "$bdir/oscam-${REPO}$(REVISION)$($(USEGIT) && printf "@$(COMMIT)" || printf "")-$(hostname)-list_smargo"
			tartmp="oscam-${REPO}$(REVISION)$($(USEGIT) && printf "@$(COMMIT)" || printf "")-$(hostname)-list_smargo"
		else
			printf "$g_n"" SAVE -------> $w_l$lsmn$g_l $txt_as$y_l oscam-${REPO}$(REVISION)$($(USEGIT) && printf "@$(COMMIT)" || printf "")-$_toolchainname-list_smargo"
			mv -f "$lsmn" "$bdir/oscam-${REPO}$(REVISION)$($(USEGIT) && printf "@$(COMMIT)" || printf "")-$_toolchainname-list_smargo"
			tartmp="oscam-${REPO}$(REVISION)$($(USEGIT) && printf "@$(COMMIT)" || printf "")-$_toolchainname-list_smargo"
		fi

	fi

#remove debug binary
	if [ "${s3cfg_vars[delete_oscamdebugbinary]}" == "1" ] && [ -f "$bdir/$oscam_name.debug" ]
	then
		printf "$r_l"" REMOVE ----->  $w_l$bdir/$oscam_name.debug"
		rm "$bdir/$oscam_name.debug"
	fi

#show build time
	printf "$g_n""\n TIME -------> $bt$re_\n\n"

#compress cam
	if [ "${s3cfg_vars[COMPRESS]}" == "1" ]
	then
		printf "$w_l"" ENABLE -----> COMPRESSION:$y_l $txt_wait"
		compress_cam "$oscam_name"
	fi;

	if [ "${s3cfg_vars[TARGZ]}" == "1" ]
	then
		printf "$w_l"" ENABLE -----> TARGZ:$y_l $txt_wait\n"
		tar_cam "$oscam_name" "$tartmp"
	fi

#link lastlog
	ln -frs "$ldir/$log_name" "$workdir/lastbuild.log"
	if [ "${USE_vars[USE_DIAG]}" == "USE_DIAG=1" ]; then
		grep -v "^CC\|^GEN\|^CONF\|^BUILD\|STRIP\|LINK\|^+\|^scan-build: R\|^scan-build: U\|HOSTCC\|^|" -i "$workdir/lastbuild.log" \
		|sed $'s/ generated./ generated.\\\n\\\n\\\n/g' >"$workdir/USE_DIAG.log"
	fi

#EXTRA_COPY_DIR
	if [ $EXTRA_COPY_DIR -eq 1 ]; then
		cd $bdir
		cp $oscam_name $here_
	fi

#build error message
	error_on_build=$(grep -c1 error "$ldir/$log_name")
	[ "$error_on_build" -gt "0" ] && printf "$r_l\terrors on build found$w_l"
	printf "$re_$w_l"
	exit
}
