
source ./inits

#edb(0=also ocomp),ex(exec),win,old(for32),onlycomp

OFLAGS+=" nul_res_pref 1"

exp=.expected/

copy () {
	cp ${1} ${exp}${1}
}
base () {
	base64 ${1} > ${exp}${1}.txt
}

comp () {
	n=./${1}.oc
	if [ "${edb}" = "0" ]; then
		edb --run ${OCOMP} ${n} ${OFLAGS} || exit 1
	else
		#LD_LIBRARY_PATH=${LD_LIBRARY_PATH} this only if was at source ./inits
		${OCOMP} ${n} ${OFLAGS} || exit 1
	fi
	if [ -n "${write_to_expected}" ]; then
		copy ${n}.log
		base ${n}.x
		basenm=${1%.[^.]*}  #this is ok for a -> a , a.b -> a , a.b.c -> a.b
		ext=${1:${#basenm} + 1}
		if [ "${ext}" = "out" ]; then
			base ${1}
		else
			base ${1}.o  #${n:0:-3}.o
		fi
	fi
}
if [ -z "${ex}" ]; then
	if [ -z "${OLINK}" ]; then OLINK=../ounused/ounused; fi

	if [ -n "${win}" ]; then
		OFLAGS+=" inplace_reloc 0"
		if [ -z "${old}" ]; then OFLAGS+=" conv_64 2"; fi
	#else underscore_pref 1
	fi

	logs=
	objs=
	for f in ${@}; do
		comp ${f}
		logs+=" ${f}.oc.log"
		objs+=" ${f}.o"
	done
	if [ -z "${onlycomp}" ]; then
		${launcher} ${OLINK} ${logs} || exit 1
		echo
		if [ -z "${win}" ]; then
			if [ -z "${old}" ]; then
				if [ -z "${ldprefer}" ]; then
					cc ${objs}
				else
					${LD} ${objs} --dynamic-linker=/lib64/ld-linux-x86-64.so.2 ${L} -lc --entry main -o a.out
				fi
			else
				if [ -z "${LD}" ]; then LD=ld; fi
				${LD} ${objs} --entry main --dynamic-linker=/lib/ld-linux.so.2 -m elf_i386 -o a.out -lc
			fi
			if [ -z "${edb}" ]; then
				if [ -n "${ldprefer}" ]; then  #for box64 case
					./a.out
				else
					${launcher} ./a.out
				fi
			else edb --run a.out
			fi
		else
			if [ -z "${old}" ]; then
				x86_64-w64-mingw32-gcc ${objs} -o a.out
				if [ -z "${edb}" ]; then wine `pwd`/a.out
				else wine /home/bc/Desktop/x64/release/x64/x64dbg.exe `pwd`/a.out
				fi
			else
				i686-w64-mingw32-ld ${objs} -entry main -lmsvcrt -o ./a.out
				if [ -z "${edb}" ]; then wine `pwd`/a.out
				else wine /home/bc/Desktop/x64/release/x32/x32dbg.exe `pwd`/a.out
				fi
			fi
		fi
	fi
else
	comp ${1}
	if [ -z "${onlycomp}" ]; then
		if [ -z "${win}" ]; then
			mv ${1} `pwd`/a.out
			if [ -z "${edb}" ]; then /home/bc/Desktop/out/squashfs-root/lib32/ld-linux.so.2 ./a.out
			else edb --run /home/bc/Desktop/out/squashfs-root/lib32/ld-linux.so.2 ./a.out
			fi
		else
			mv ${1}.exe `pwd`/a.out
			if [ -z "${edb}" ]; then wine `pwd`/a.out
			else wine /home/bc/Desktop/x64/release/x32/x32dbg.exe `pwd`/a.out
			fi
		fi
	fi
fi || {
	echo $?
	exit 1
}

#wine regedit wine_debugger
