PATH := $(PATH):/usr/local/sbin:/usr/sbin:/sbin

gorg64:
	./compile_gorg64.sh

spkplay:
	fpc -CX -XX -Cg -k-pie -k-znow -O4 gorg64_spkplay.pas
	fpc -CX -XX -Cg -k-pie -k-znow -O4 gorg64_spkplay_syscalls.pas
	fpc -CX -XX -Cg -k-pie -k-znow -O4 gorg64_spktone.pas

suidroot :
	chown root:root gorg64_spkplay gorg64_spktone gorg64_spkon gorg64_spkoff
	chmod =04755 gorg64_spkplay gorg64_spktone gorg64_spkon gorg64_spkoff

drstatotxt:
	fpc drstatotxt.pas

obtainmse :
	./obtainmse.sh

compilemse:
	./compile_mse.sh

updatefpgui:
	git -C fpGUI pull
	echo "Run ./fpGUI/src/build.sh for build" 

source:
	dpkg-source -b .

archive:
	fpc -B -Cg -k-pie -k-znow -O2 -CX -XX ver.pas
	-rm -f ver.o
	-sstrip ver
	./ver ar

clean:
	-rm -f *.o *.a *.ppu *.bak gorg64_spkplay alarm gorg64 gorg64_spkoff link*.res *.rsj gorg64*.deb
	-rm -f gorg64_spkplay_syscalls gorg64_spkplay_evdev gorg64_spkplay_alsa core gorg64_spktone gorg64_spkon gorg64_runner wavrec wavnorm sortucs4
	-rm -f untar chext todo ver xsendenter fpcpiepatch *.bak1 mseide msepatch mseguiintf.pas form2pas data2pas bmp2pas ideU dclockpanel renice_tree voicestarter vc2 termometeropenweathermap termometerwttr endprocessmonitor
	-rm -f ttaenc wavfix o.opt acmp gcmp _true _false _yes _sync _arch libuniquecheck*amd64.deb libuniquecheck*amd64.buildinfo libuniquecheck*amd64.changes
	-rm -r -f ./units/
	-rm -r -f ./libttaR/build/
	-find ./ \( -name "o.opt" -o -name "link*.res" -o -name "*~" -o -name "*.ppu" -o -name "*.o" -o -name "*_mfm.pas" -o -name "*.a" -o -name "*.bak" -o -name "*.bak1" \)  -delete
	-rm -f ./mseide-msegui/apps/ide/mseide
	-mkdir ./units/
	-rm -f ./ttaenc-3.4.1-src/ttaenc ./ttaenc-3.4.1-src/ttaenc.o

install:
	dpkg -i gorg64_2.0.0-1_amd64.deb

deb:
	./compile_mse.sh
	./compile_tta.sh
	./compile_ttaR.sh
	./compile_wavfix.sh
	./obtainideU.sh
	./compile_ideu.sh
	./obtainupx.sh
	-rm -r -f usr
	-mkdir -m =00755 ./usr/ ./usr/bin/ ./usr/lib/ ./usr/include/ ./usr/share/ ./usr/share/doc/ ./usr/share/doc/gorg64/ ./usr/share/doc/gorg64/lang_s/ ./usr/share/applications/ ./usr/share/man/ ./usr/share/man/man1/ ./usr/share/man/man3/ ./usr/share/pixmaps/ ./usr/share/man/ru/ ./usr/share/man/ru/man1/
	cp gorg64 gorg64_spkplay gorg64_spktone gorg64_spkon gorg64_spkoff gorg64_runner wavrec wavnorm chext untar todo renice_tree ttaenc xsendenter sortucs4 ./libttaR/build/ttar mseide form2pas data2pas bmp2pas ideU dclockpanel wavfix _true _false _yes _sync _arch voicestarter vc2 termometeropenweathermap termometerwttr endprocessmonitor ./usr/bin/
	cp ./libttaR/build/libttar.so ./usr/lib/
	cp ./libttaR/build/libttar.h ./usr/include/
	./compile_leafpad.sh
	./compile_lazprog.sh
	./compile_upx.sh
	./compile_libuniquecheck.sh
	sstrip ./usr/bin/* ./usr/lib/*
	cp *.desktop ./usr/share/applications/
	cp *.png ./usr/share/pixmaps/
	cp copyright ./usr/share/doc/gorg64/
	cp ./lang_s/*.txt ./usr/share/doc/gorg64/lang_s/
	gzip -n -9 -c changelog > ./usr/share/doc/gorg64/changelog.gz
	tar -c --xz sound music script voicedigits > ./usr/share/doc/gorg64/files.tar.xz
	cp -r ./petrovich-ruby/ ./usr/share/doc/gorg64/
	find ./ -maxdepth 1 -name "*.1" -type f -exec gzip -f -n -9 -k '{}' \;
	find ./ -maxdepth 1 -name "*.3" -type f -exec gzip -f -n -9 -k '{}' \;
	mv *.1.gz ./usr/share/man/man1/
	mv *.3.gz ./usr/share/man/man3/
	gzip -n -9 -c ./manru/gorg64.1 > ./usr/share/man/ru/man1/gorg64.1.gz
	gzip -n -9 -c ./manru/gorg64_spkplay.1 > ./usr/share/man/ru/man1/gorg64_spkplay.1.gz
	gzip -n -9 -c ./manru/gorg64_spktone.1 > ./usr/share/man/ru/man1/gorg64_spktone.1.gz
	gzip -n -9 -c ./manru/gorg64_spkon.1 > ./usr/share/man/ru/man1/gorg64_spkon.1.gz
	gzip -n -9 -c ./manru/gorg64_spkoff.1 > ./usr/share/man/ru/man1/gorg64_spkoff.1.gz
	gzip -n -9 -c ./manru/wavrec.ru.1 > ./usr/share/man/ru/man1/wavrec.1.gz
	gzip -n -9 -c ./manru/wavnorm.ru.1 > ./usr/share/man/ru/man1/wavnorm.1.gz
	gzip -n -9 -c ./manru/petrovich.1 > ./usr/share/man/ru/man1/petrovich.1.gz
	find ./usr/ -type d -exec chmod =00755 '{}' \;
	find ./usr/ -type f -exec chmod =00644 '{}' \;
	find usr/ -type f -exec md5sum \{\} \; > md5sums
	chmod =00644 md5sums
	chmod =00755 usr/bin/*
	cd usr/lib/ && ln -s libttar.so libttar.so.1
	chmod =00644 usr/lib/*
	chmod =00755 usr/share/doc/gorg64/petrovich-ruby/ usr/share/doc/gorg64/petrovich-ruby/bin/petrovich usr/share/doc/gorg64/petrovich-ruby/rules/check.rb
	chmod =00644 control
	-rm -f control.tar.xz data.tar.zst data.tar.xz data.tar data.tar.bz2
	tar -c --owner=root --group=root -f control.tar postinst postrm control md5sums triggers shlibs
	xz -e control.tar
	tar -c --owner=root --group=root -f data.tar usr
	zstd --ultra data.tar
	-rm -r -f usr
	-rm -f gorg64_2.0.0-1_amd64.deb
	ar q gorg64_2.0.0-1_amd64.deb debian-binary control.tar.xz data.tar.zst
	-rm -f control.tar.xz data.tar.zst data.tar.xz data.tar data.tar.bz2 md5sums
	-lintian --tag-display-limit 0 gorg64_2.0.0-1_amd64.deb
.PHONY: clean