#!/usr/bin/make -f
%:
	dh $@


override_dh_auto_clean:
	cd .. ; tar xvpzf cryptlib_3.4.9.2.orig.tar.gz; cd cryptlib-3.4.9.2
	pwd
	/usr/bin/make clean
	/bin/rm -rf bindings/build bindings/cryptlib libcl.so*
	/bin/rm -rf bindings/GenPerl.pl bindings/blib bindings/PerlCryptLib.o bindings/MYMETA.json bindings/MYMETA.yml bindings/Makefile
	/bin/rm -rf bindings/PerlCryptLib.c bindings/const-c.inc bindings/const-xs.inc  bindings/PerlCryptLib.bs bindings/pm_to_blib
	/bin/rm -rf bindings/Perl*xsc
	/bin/rm -rf bindings/javadoc
	/bin/rm -rf jni.h jni_md.h
	/bin/rm -rf bindings/ppport.h bindings/typemap

override_dh_auto_build:
	
	/usr/bin/make clean
	ln -s libcl.so.3.4.9 libcl.so
	ln -s libcl.so.3.4.9 libcl.so.3
	sed -i 's/\/\* #define USE_JAVA \*\// #define USE_JAVA /' misc/config.h
	cp /usr/lib/jvm/java-??-openjdk-amd64/include/jni.h .
	cp /usr/lib/jvm/java-??-openjdk-amd64/include/linux/jni_md.h .
	chmod +x tools/mkhdr.sh; tools/mkhdr.sh
	/bin/bash tools/rename.sh
	# patches 
	#sed -i '53s/2021/2023/' test/certs.c
	#sed -i '55s/2021/2023/' test/certs.c
	#sed -i '900s/native/x86-64-v3/' tools/ccopts.sh
	#sed -i '3111s/^/#include <sched.h>/' kernel/thread.h
	#sed -i '3169s/pthread_yield/sched_yield/' kernel/thread.h
	/usr/bin/make shared
	/usr/bin/make stestlib
	cd ./bindings; /usr/bin/python3 setup.py build
	cd ./bindings;cp ../debian/perl/* .;  cp ../tools/GenPerl.pl .; export PERL_CRYPT_LIB_HEADER=./cryptlib-perl.h; sed -i '88s/\-lcl/\-L.. \-lcl/' Makefile.PL; perl Makefile.PL;  make
	cd ./bindings; /bin/mkdir javadoc;  cp function-comments.py3 javadoc; chmod +x ../tools/cryptlibConverter.py3; ../tools/cryptlibConverter.py3 javadoc.h javadoc java; cd javadoc; export LC_ALL=en_EN.utf8; jar -xf ../cryptlib.jar; javadoc cryptlib -encoding utf-8

override_dh_auto_install:
	/bin/mkdir -p debian/cryptlib/usr/bin
	/bin/mkdir -p debian/cryptlib/usr/lib/cryptlib
	/bin/mkdir -p debian/cryptlib/usr/lib64
	/bin/mkdir -p debian/cryptlib/usr/lib/cryptlib/javadoc/cryptlib
	/bin/mkdir -p debian/cryptlib/usr/include/cryptlib
	/bin/mkdir -p debian/cryptlib/usr/lib/python3/dist-packages
	/bin/mkdir -p debian/cryptlib/lib/x86_64-linux-gnu
	/bin/mkdir -p debian/cryptlib/usr/share/doc/cryptlib
	/bin/mkdir -p debian/cryptlib/usr/share/java
	/bin/mkdir -p debian/cryptlib/usr/share/man
	/bin/mkdir -p debian/cryptlib/usr/share/man/man1
	/bin/mkdir -p debian/cryptlib/usr/lib/perl5/auto/PerlCryptLib
	cp manual.pdf debian/cryptlib/usr/share/doc/cryptlib 
	cp manual.pdf debian/cryptlib/usr/lib/cryptlib 
	cp *.md debian/cryptlib/usr/lib/cryptlib 
	cp README debian/cryptlib/usr/lib/cryptlib 
	cd debian/cryptlib/lib/x86_64-linux-gnu; cp ../../../../libcl.so.3.4.9 .; ln -s libcl.so.3.4.9 libcl.so.3; ln -s libcl.so.3 libcl.so; ln -s libcl.so.3.4.9 libcl.so.3.4; cd -
	cd debian/cryptlib/usr/lib64/; cp ../../../../libcl.so.3.4.9 .; ln -s libcl.so.3.4.9 libcl.so.3; ln -s libcl.so.3 libcl.so; ln -s libcl.so.3.4.9 libcl.so.3.4; cd -
	# for U-20 and U-22
	###cp bindings/build/lib.linux-x86_64-3.*/cryptlib_py.cpython-*-x86_64-linux-gnu.so debian/cryptlib/usr/lib/python3/dist-packages/cryptlib_py.so
	###  and U-24  
	cp bindings/build/lib.linux-x86_64-cpython*/cryptlib_py.cpython-3*-x86_64-linux-gnu.so debian/cryptlib/usr/lib/python3/dist-packages/cryptlib_py.so
	cp crypt.h cryptlib.h cryptkrn.h debian/cryptlib/usr/include/cryptlib
	cp bindings/cryptlib.jar debian/cryptlib/usr/share/java
	cp -r bindings/javadoc debian/cryptlib/usr/lib/cryptlib
	cp -r bindings/cryptlib debian/cryptlib/usr/lib/cryptlib/javadoc
	cp stestlib debian/cryptlib/usr/lib/cryptlib
	cp -r test debian/cryptlib/usr/lib/cryptlib
	cp debian/cltools/clsha1  debian/cryptlib/usr/bin
	cp debian/cltools/clsha2  debian/cryptlib/usr/bin
	cp debian/cltools/claes   debian/cryptlib/usr/bin
	cp debian/cltools/clkeys  debian/cryptlib/usr/bin
	cp debian/cltools/clsmime  debian/cryptlib/usr/bin
	cp debian/man/clsha1.1.gz debian/cryptlib/usr/share/man/man1
	cp debian/man/clsha2.1.gz debian/cryptlib/usr/share/man/man1
	cp debian/man/claes.1.gz  debian/cryptlib/usr/share/man/man1
	cp debian/man/clkeys.1.gz debian/cryptlib/usr/share/man/man1
	cp debian/man/clsmime.1.gz debian/cryptlib/usr/share/man/man1
	cp -r debian/examples debian/cryptlib/usr/lib/cryptlib
	cp  bindings/blib/arch/auto/PerlCryptLib/PerlCryptLib.so debian/cryptlib/usr/lib/perl5/auto/PerlCryptLib
	cp -r bindings/blib/lib/* debian/cryptlib/usr/lib/perl5


override_dh_shlibdeps:
	dh_shlibdeps -l $$(pwd)/debian/cryptlib/lib/x86_64-linux-gnu

