#!/usr/bin/env bash

DIR=$(dirname "$0")
cd "$DIR" || exit 1

myscript=$(basename $0)

server="$1"
client="$2"
user="$3"

if [ -z "$server" ] \
  || [ -z "$client" ] \
  || [ -z "$user" ] ; then
	echo "Usage: $myscript [server address] [client address] [client user]"
	exit 1
fi

path="$PWD"
build="$path/build"
target="$path/target"
ssh_opts="-o StrictHostKeyChecking=no"

fail()
{
	echo
	echo "Test setup failed: $@"
	echo
	exit 1
}

makedir()
{
	rm -rf "$1"
	mkdir -p "$1" || fail "could not mkdir $1"
}

cdir()
{
	cd "$1" || fail "could not cd to $1"
}

build_and_install()
{
	# Create a build directory, and fill it with the source.
	makedir "$build"
	makedir "$build/test"
	ls ../ | while read f ; do
		[ "$f" = "test" ] && continue
		[ "$f" = ".git" ] && continue
		cp -ar ../"$f" "$build" || fail "could not copy ../$f to $build"
	done || exit 1
	# Add some wide-character directories/files.
	cp -ar china-data "$build"

	# Create a target directory, compile burp and install it into the
	# target.
	makedir "$target"
	cdir "$build"
	make clean
	./configure --prefix=/usr --sysconfdir=/etc/burp --localstatedir=/var || fail "configure failed"
	make || fail "make failed"
	# For some reason, make is not returning an error code.
	# Look for important binaries before carrying on.
	[ -x burp ] || fail "make failed to build binaries"
	make install-all DESTDIR="$target" || fail "make install failed"
	cdir -

	# Now build the Windows installer.
	cdir "$build/src/win32"
	make WIN64=yes || fail

	# Now copy the Windows installer to the client
	installer=$(find -name burp-win64-installer*.exe)
	[ -z "$client" ] && fail
	scp $ssh_opts "$installer" "$user@$client:" || fail

	cdir -
}

build_and_install

# Copy the build directory to the client, to give it something to backup.
cdir "$path" || fail
# The Windows build symlinks cock things up for 'diff -ur' on the restore
# later, so delete them first.
rm -f build/burp-depkgs
rm -f build/burp-cross-tools
tar -cjf build.tar.bz2 build || fail
scp $ssh_opts build.tar.bz2 "$user@$client:" || fail


installer=$(basename $installer)
clientburpdir="/cygdrive/c/Program Files/Burp"

rm -f windowsscript
cat >> windowsscript << EOF
#!/usr/bin/env bash
set -e

clientburpdir="$clientburpdir"

function check_conf()
{
	local conf="\$clientburpdir/burp.conf"
	grep "\$1" "\$conf"
}

echo "Test command line switches"
rm -rf "\$clientburpdir"
./$installer /S /cname=customcname /server=1.2.3.4 /password=custompass
check_conf '^cname = customcname$'
check_conf '^server = 1.2.3.4$'
check_conf '^password = custompass$'

echo "Test command line without switches"
rm -rf "\$clientburpdir"
./$installer /S
check_conf "^cname = \$(hostname)$"
check_conf '^server = 10.0.0.1$'
check_conf '^password = abcdefgh$'

# Remove the cron job, or unexpected results can occur.
schtasks /DELETE /TN "burp cron" /F
tar -xjf build.tar.bz2 -C "$clientburpdir"
# Encrypt some directories and files to test EFS.
cipher.exe /E /S:"C:/Program Files/Burp/build/utest/builders"
# Run utest.exe.
cd "/cygdrive/c/Program Files/Burp/bin"
./utest.exe
EOF

scp $ssh_opts windowsscript "$user@$client:" || fail
ssh $ssh_opts "$user@$client" chmod 755 windowsscript || fail
ssh $ssh_opts "$user@$client" chmod 755 "$installer" || fail
ssh $ssh_opts "$user@$client" ./windowsscript || fail

# args:
# 1 - directory where build was installed
# 2 - location of client burp
# 3 - location of client conf (for editing)
# 4 - location of client conf (for giving as option to burp binary)
# 5 - directory to backup
# 6 - directory to restore to
# 7 - address of the server
# 8 - ssh command (leave unset for self test)
# 9 - scp command (leave unset for self test)
# 10 - address of the client (leave unset for self test)

do_run()
{
	rm -rf "$target/var/spool/burp/global"
	rm -rf "$target/var/spool/burp/testclient"
	./test_main \
		"$target" \
		"$clientburpdir/bin/burp.exe" \
		"$clientburpdir/burp.conf" \
		"C:\Program Files\Burp\burp.conf" \
		"C:/Program Files/Burp/build" \
		"C:/Program Files/Burp/restore" \
		"$server" \
		"ssh $ssh_opts" \
		"scp $ssh_opts" \
		"$user@$client" || exit 1
}

echo "Running with protocol=1, split_vss=0, strip_vss=0"
PROTOCOL=1 do_run

echo "Running with protocol=1, split_vss=1, strip_vss=0"
PROTOCOL=1 NO_CA_GEN=1 SPLIT_VSS=1 do_run

echo "Running with protocol=1, split_vss=0, strip_vss=1"
PROTOCOL=1 NO_CA_GEN=1 STRIP_VSS=1 do_run

# Protocol 2 does not yet support EFS, so redo the test files without EFS.
# Can delete this once protocol 2 does support EFS.
rm -f windowsscript
cat >> windowsscript << EOF
#!/usr/bin/env bash
rm -rf "$clientburpdir/build" || exit 1
tar -xjf build.tar.bz2 -C "$clientburpdir" || exit 1
EOF

scp $ssh_opts windowsscript "$user@$client:" || fail
ssh $ssh_opts "$user@$client" chmod 755 windowsscript || fail
ssh $ssh_opts "$user@$client" ./windowsscript || fail

echo "Running with protocol=2, split_vss=0, strip_vss=0"
PROTOCOL=2 NO_CA_GEN=1 do_run

exit 0
