#! /bin/sh

while [ $# -gt 0 ]; do
	case "$1" in
	-v)
		set -x
		;;
	*)
		break
		;;
	esac
	shift
done

env USETOOLS=no MAKEOBJDIRPREFIX=/usr/obj/i386 sh -c 'cd ../lib && \
	make cleandir ; \
	su root -c "make includes"; \
	make ; \
	su root -c "make install"'
env USETOOLS=no MAKEOBJDIRPREFIX=/usr/obj/i386 sh -c 'cd ../netpgp && \
	make cleandir ; \
	make ; \
	su root -c "make install"'
env USETOOLS=no MAKEOBJDIRPREFIX=/usr/obj/i386 sh -c 'cd ../netpgpkeys && \
	make cleandir ; \
	make ; \
	su root -c "make install"'
env USETOOLS=no MAKEOBJDIRPREFIX=/usr/obj/i386 sh -c 'cd ../netpgpverify && \
	make cleandir ; \
	make ; \
	su root -c "make install"'

passed=0
total=27
echo "======> sign/verify 180938 file"
cp configure a
/usr/bin/netpgp --sign a
/usr/bin/netpgp --verify a.gpg && passed=$(expr $passed + 1)
echo "======> attempt to verify an unsigned file"
/usr/bin/netpgp --verify a || passed=$(expr $passed + 1)
echo "======> encrypt/decrypt 10809 file"
cp src/netpgp/netpgp.1 b
/usr/bin/netpgp --encrypt b
/usr/bin/netpgp --decrypt b.gpg
diff src/netpgp/netpgp.1 b && passed=$(expr $passed + 1)
echo "======> encrypt/decrypt 180938 file"
cp configure c
/usr/bin/netpgp --encrypt c
/usr/bin/netpgp --decrypt c.gpg
diff configure c && passed=$(expr $passed + 1)
echo "======> encrypt/decrypt bigass file"
cat configure configure configure configure configure configure > d
ls -l d
cp d e
/usr/bin/netpgp --encrypt d
/usr/bin/netpgp --decrypt d.gpg
diff e d && passed=$(expr $passed + 1)
echo "======> sign/verify detached signature file"
cat configure configure configure configure configure configure > f
/usr/bin/netpgp --sign --detached f
ls -l f f.sig
/usr/bin/netpgp --verify f.sig && passed=$(expr $passed + 1)
echo "======> cat signature - verified cat command"
/usr/bin/netpgp --cat a.gpg > a2
diff a a2 && passed=$(expr $passed + 1)
echo "======> another cat signature - verified cat command"
/usr/bin/netpgp --cat --output=a3 a.gpg
diff a a3 && passed=$(expr $passed + 1)
echo "======> netpgp list-packets test"
/usr/bin/netpgp --list-packets || passed=$(expr $passed + 1)
echo "======> version information"
/usr/bin/netpgp --version
echo "======> netpgpverify file"
/usr/bin/netpgpverify a.gpg && passed=$(expr $passed + 1)
echo "======> attempt to verify an unsigned file"
/usr/bin/netpgpverify a || passed=$(expr $passed + 1)
echo "======> sign/verify detached signature file"
ls -l f f.sig
/usr/bin/netpgpverify f.sig && passed=$(expr $passed + 1)
echo "======> another verify signature - verified cat command"
/usr/bin/netpgpverify --output=a3 a.gpg
diff a a3 && passed=$(expr $passed + 1)
echo "======> list keys"
/usr/bin/netpgpkeys --list-keys && passed=$(expr $passed + 1)
echo "======> version information"
/usr/bin/netpgpverify --version
echo "======> find specific key information"
/usr/bin/netpgpkeys --get-key c0596823 agc@netbsd.org && passed=$(expr $passed + 1)
echo "======> ascii armoured signature"
cp Makefile.am g
/usr/bin/netpgp --sign --armor g && passed=$(expr $passed + 1)
echo "======> ascii armoured sig detection and verification"
/usr/bin/netpgp --verify g.asc && passed=$(expr $passed + 1)
echo "======> ascii armoured signature of large file"
cp Makefile.in g
/usr/bin/netpgp --sign --armor g && passed=$(expr $passed + 1)
echo "======> ascii armoured sig detection and verification of large file"
/usr/bin/netpgp --verify g.asc && passed=$(expr $passed + 1)
echo "======> verify memory by recognising ascii armour"
/usr/bin/netpgp --cat < g.asc > g2
diff g g2 && passed=$(expr $passed + 1)
echo "======> list ssh host RSA public key"
/usr/bin/netpgpkeys --ssh-keys --sshkeyfile=/etc/ssh/ssh_host_rsa_key.pub --list-keys && passed=$(expr $passed + 1)
echo "======> sign/verify file with ssh host keys"
cp configure a
sudo /usr/bin/netpgp --ssh-keys --sshkeyfile=/etc/ssh/ssh_host_rsa_key.pub --sign a
sudo chmod 644 a.gpg
/usr/bin/netpgp --verify --ssh-keys --sshkeyfile=/etc/ssh/ssh_host_rsa_key.pub a.gpg && passed=$(expr $passed + 1)
echo "======> pipeline and memory encrypt/decrypt"
/usr/bin/netpgp --encrypt < a | /usr/bin/netpgp --decrypt > a4
diff a a4 && passed=$(expr $passed + 1)
echo "======> pipeline and memory sign/verify"
/usr/bin/netpgp --sign < a | /usr/bin/netpgp --cat > a5
diff a a5 && passed=$(expr $passed + 1)
echo "======> verify within a duration"
cp Makefile.am h
/usr/bin/netpgp --sign --duration 6m --detached h
/usr/bin/netpgp --verify h.sig && passed=$(expr $passed + 1)
echo "======> invalid signature - expired"
rm -f h.sig
/usr/bin/netpgp --sign --duration 2 --detached h
sleep 3
/usr/bin/netpgp --verify h.sig || passed=$(expr $passed + 1)
echo "======> list signatures and subkey signatures"
/usr/bin/netpgpkeys --list-sigs && passed=$(expr $passed + 1)
rm -f a a.gpg b b.gpg c c.gpg d d.gpg e f f.sig g g.asc g2 a2 a3 a4 a5 h h.sig
echo "Passed ${passed}/${total} tests"
