#!/bin/bash

myscript=$(basename $0)
if [ ! -f "$myscript" ] ; then
	echo "Please run $myscript whilst standing in the same directory" 1>&2
	exit 1
fi

server="$1"
client="$2"
user="$3"
pass="$4"
thirtytwobit="$5"

if [ -z "$server" ] \
  || [ -z "$client" ] \
  || [ -z "$user" ] \
  || [ -z "$pass" ] ; then
	echo "Usage: $myscript [server address] [client address] [client user] [client pass]"
	echo "By default, the 64bit client is tested."
	echo "Add a fifth argument to test the  32bit client"
	exit 1
fi

path="$PWD"
build="$path/build"
target="$path/target"

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

	# Create a target directory, compile burp and install it into the
	# target.
	makedir "$target"
	cdir "$build"
	make clean
	./configure || fail "configure failed"
	make || fail "make failed"
	# For some reason, make is not returning an error code.
	# Look for important binaries before carrying on.
	[ -f src/burp ] || fail "make failed"
	[ -f src/bedup ] || fail "make failed"
	make install DESTDIR="$target" || fail "make install failed"
	cdir -

	# Now build the Windows installer.
	cdir "$build/src/win32"
	if [ -n "$thirtytwobit" ] ; then
		make || fail
		installer=$(find -name burp-win32-installer*.exe)
	else
		make WIN64=yes || fail
		installer=$(find -name burp-win64-installer*.exe)
	fi

	# Now copy the Windows installer to the client
	[ -z "$client" ] && fail
	"$path/scp_expect" "$pass" "$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 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
"$path/scp_expect" "$pass" build.tar.bz2 "$user@$client:" || fail


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

rm -f windowsscript
cat >> windowsscript << EOF
#!/bin/bash
rm -rf "/cygdrive/c/Program Files/Burp" || exit 1
./$installer /S || exit 1
tar -xjf build.tar.bz2 -C "$clientburpdir" || exit 1
EOF

"$path/scp_expect" "$pass" windowsscript "$user@$client:" || fail
"$path/ssh_expect" "$pass" "$user@$client" chmod 755 windowsscript || fail
"$path/ssh_expect" "$pass" "$user@$client" chmod 755 "$installer" || fail
"$path/ssh_expect" "$pass" "$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)
echo "Running with split_vss=0 and strip_vss=0"
./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" \
	"$path/ssh_expect $pass" \
	"$path/scp_expect $pass" \
	"$user@$client" || exit 1

# Go again with SPLIT_VSS=1
echo "Running with split_vss=1 and strip_vss=0"
rm -rf "$target/var/spool/burp/testclient"
NO_CA_GEN=1 SPLIT_VSS=1 ./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" \
	"$path/ssh_expect $pass" \
	"$path/scp_expect $pass" \
	"$user@$client" || exit 1

# Go again with STRIP_VSS=1
echo "Running with split_vss=0 and strip_vss=1"
rm -rf "$target/var/spool/burp/testclient"
NO_CA_GEN=1 STRIP_VSS=1 ./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" \
	"$path/ssh_expect $pass" \
	"$path/scp_expect $pass" \
	"$user@$client" || exit 1

exit 0
