#!/bin/bash

if ! [ -e /usr/include/uniquecheck.h ]
then
echo "libuniquecheck not installed in system"
./compile_libuniquecheck.sh && echo "Run installdeb_libuniquecheck.sh as root and rerun this make process"
exit 1
fi

mkdir -p ./units/
fpc -O4 -XX -CX -Cg ver.pas
./obtainmse.sh
fpc -O4 -XX -CX -Cg msepatch.pas
./msepatch
     if [ "$?" != "0" ]
     then
          echo 'MSE patch error - update FAILED :('
          exit
     fi
find ./ \( -name "*_mfm.pas" \)  -delete
./ver
./compile_msetools.sh

fpc -B -Xs- -Ow -FWo.opt -OWAll -Cg -k-pie -k-znow -CfSSE64 -Fu./ -FU./units/ -Fu./ -Fu./mseide-msegui/lib/common/* -Fu./mseide-msegui/lib/common/kernel/linux -CX -XX -O2 -Mobjfpc gorg64.pas
mold -run clang -fbasic-block-address-map -funique-internal-linkage-names -flto=full -ffast-math -fforce-addr -funroll-loops -O3 -msse4a -fPIE gorg64_spktone.c  -o gorg64_spktone -lm || fpc -Xs- -Ow -FWo.opt -OWAll -Cg -k-pie -k-znow -CfSSE64 -CX -XX -O2 -Mobjfpc gorg64_spktone.pas

mold -run clang -fbasic-block-address-map -funique-internal-linkage-names -flto=full -ffast-math -fforce-addr -funroll-loops -O3 -msse4a -fPIE chext.c -o chext || fpc -Xs- -Ow -FWo.opt -OWAll -Cg -k-pie -k-znow -CfSSE64 -CX -XX -O3 -Mobjfpc chext.pas
fpc -Xs- -Ow -FWo.opt -OWAll -Cg -k-pie -k-znow -CfSSE64 -CX -XX -O3 -Mobjfpc untar.pas
fpc -Xs- -Ow -FWo.opt -OWAll -Cg -k-pie -k-znow -CfSSE64 -CX -XX -O3 -Mobjfpc endprocessmonitor.pas
mold -run clang -fbasic-block-address-map -funique-internal-linkage-names -flto=full -ffast-math -fforce-addr -funroll-loops -O3 -msse4a -fPIE ./wavrecc/wavrec.c -o wavrec -lasound -lm -pthread || fpc -Xs- -Ow -FWo.opt -OWAll -Cg -k-pie -k-znow -CfSSE64 -CX -XX -O3 wavrec.pas
mold -run clang -fbasic-block-address-map -funique-internal-linkage-names -flto=full -ffast-math -fforce-addr -funroll-loops -O3 -msse4a -fPIE ./wavnormc/wavnorm.c -o wavnorm -lm || fpc -Xs- -Ow -FWo.opt -OWAll -Cg -k-pie -k-znow -CfSSE64 -CX -XX -O3 -Mobjfpc wavnorm.pas
mold -run clang -fbasic-block-address-map -funique-internal-linkage-names -flto=full -ffast-math -fforce-addr -funroll-loops -O3 -msse4a -fPIE xsendenter.c -o xsendenter -lX11 -lXtst || fpc -Xs- -Ow -FWo.opt -OWAll -Cg -k-pie -k-znow -CfSSE64 -CX -XX -O3 -Mobjfpc xsendenter.pas
mold -run clang -fbasic-block-address-map -funique-internal-linkage-names -flto=full -ffast-math -fforce-addr -funroll-loops -O3 -msse4a -fPIE renice_tree.c -o renice_tree || fpc -Xs- -Ow -FWo.opt -OWAll -Cg -k-pie -k-znow -CfSSE64 -CX -XX -O3 -Mobjfpc renice_tree.pas
mold -run clang -fbasic-block-address-map -funique-internal-linkage-names -flto=full -ffast-math -fforce-addr -funroll-loops -O3 -msse4a -fPIE voicestarter.c -o voicestarter -lasound -lm || fpc -Xs- -Ow -FWo.opt -OWAll -Cg -k-pie -k-znow -CfSSE64 -CX -XX -O3 -Mobjfpc voicestarter.pas
mold -run clang -fbasic-block-address-map -funique-internal-linkage-names -flto=full -ffast-math -fforce-addr -funroll-loops -O3 -msse4a -fPIE vc2.c -o vc2 -lm || fpc -Xs- -Ow -FWo.opt -OWAll -Cg -k-pie -k-znow -CfSSE64 -CX -XX -O3 -Mobjfpc vc2.pas

mold -run clang -fbasic-block-address-map -funique-internal-linkage-names -flto=full -ffast-math -fforce-addr -funroll-loops -O3 -msse4a -fPIE gorg64_runner.c  -o gorg64_runner -lm || fpc -B -Xs- -Ow -FWo.opt -OWAll -Cg -k-pie -k-znow -CfSSE64 -CX -XX -O2 -Mobjfpc gorg64_runner.pas

as _true.S -o _true.o
as _false.S -o _false.o
as _yes.S -o _yes.o
as _sync.S -o _sync.o
as _arch.S -o _arch.o
as gorg64_spkplay.S -o gorg64_spkplay.o
as gorg64_spkon.S -o gorg64_spkon.o
as gorg64_spkoff.S -o gorg64_spkoff.o

ld.lld -L /usr/lib/x86_64-linux-gnu -lc --no-dynamic-linker --pie _true.o -o _true
ld.lld -L /usr/lib/x86_64-linux-gnu -lc --no-dynamic-linker --pie _false.o -o _false
ld.lld -L /usr/lib/x86_64-linux-gnu -lc --no-dynamic-linker --pie _yes.o -o _yes
ld.lld -L /usr/lib/x86_64-linux-gnu -lc --no-dynamic-linker --pie _sync.o -o _sync
ld.lld -L /usr/lib/x86_64-linux-gnu -lc --no-dynamic-linker --pie _arch.o -o _arch
ld.lld -L /usr/lib/x86_64-linux-gnu -lc --no-dynamic-linker --pie gorg64_spkon.o -o gorg64_spkon
ld.lld -L /usr/lib/x86_64-linux-gnu -lc --no-dynamic-linker --pie gorg64_spkoff.o -o gorg64_spkoff
# ld.lld -L /usr/lib/x86_64-linux-gnu -lc --no-dynamic-linker --pie gorg64_spkplay.o -o gorg64_spkplay ||
mold -run clang -fbasic-block-address-map -funique-internal-linkage-names -flto=full -ffast-math -fforce-addr -funroll-loops -O3 -msse4a -fPIE gorg64_spkplay.c -o gorg64_spkplay -lm -lasound -luniquecheck || fpc -Xs- -Ow -FWo.opt -OWAll -Cg -k-pie -k-znow -CfSSE64 -CX -XX -O2 -Mobjfpc gorg64_spkplay.pas

rm gorg64_spkon.o || fpc -B -Cg -k-pie -k-znow -O2 -CX -XX gorg64_spkon.pas
rm gorg64_spkoff.o || fpc -B -Cg -k-pie -k-znow -O2 -CX -XX gorg64_spkoff.pas

mold -run clang -fbasic-block-address-map -funique-internal-linkage-names -flto=full -ffast-math -fforce-addr -funroll-loops -O3 -msse4a -fPIE ./sortucs4c/sortucs4.c -o sortucs4 -lm
if ! [ -e ./sortucs4 ]
then
pushd sortucs4-src
make clean
make
mv sortucs4 ../
make clean
popd
fi

pushd todo-src
make clean
make
mv todo ../
make clean
popd

pushd dclockspec4
make clean
make
mv dclockpanel termometeropenweathermap termometerwttr ../
make clean
popd
