# Given a standard OpenVPN tarball tree,
# build a Windows tree which is NSIS-ready.
#
# Requires:
#  MinGW
#  MSYS GNU shell environment
#  See other requirements below

# Top level directory.
H=/c/src

# Output NSIS-ready tree here (will be deleted
# if already exists).
OUT=$H/20

# Source distribution is here.  Can be the top
# level directory of exploded tarball.
IN=/y/openvpn/20/openvpn

# Already built OpenSSL tree.
SSL=$H/openssl-0.9.7i

# Already built LZO tree.
LZO=$H/lzo-1.08

# Already built dmalloc tree.
# Optional, but leave defined even if you are not using
# dmalloc.
DMALLOC=$H/dmalloc-5.4.2

# TAP binaries should be here: tap0801.sys and tapinstall.exe
# These must be built with MS DDK.
TAPBIN=$H/tapbin

# u2d.c should exist here.
SCRIPTS=$IN/install-win32

# Put service.[ch] here from MS Platform SDK.
SVC_TEMPLATE=$H/svc-template

# Misc files/directories which should be copied to OUT (optional)
MISC=$H/add

# Temporary directory
TMP=/tmp

# End of user-defined parameters.

U2D=$TMP/u2d

echo BUILD u2d
gcc -O2 $SCRIPTS/u2d.c -o $U2D

echo BUILD output dir from source
rm -rf $OUT
mkdir $OUT

cp $IN/*.[ch] $OUT
rm -f $OUT/config.h

if [ $MISC ]; then
  cp $MISC/*.* $OUT
fi

echo BUILD makefile

sed "s#^OPENSSL = .*\$#OPENSSL = $SSL#" <$IN/makefile.w32 | \
sed "s#^LZO = .*\$#LZO = $LZO#" | \
sed "s#^DMALLOC = .*\$#DMALLOC = $DMALLOC#" >$OUT/Makefile

echo BUILD bin

mkdir $OUT/bin
for f in libeay32.dll libssl32.dll openssl.exe ; do
  cp $SSL/$f $OUT/bin
  strip $OUT/bin/$f
done

echo BUILD install-win32

mkdir $OUT/install-win32

cp $IN/install-win32/openvpn.nsi $OUT/install-win32
cp $IN/install-win32/setpath.nsi $OUT/install-win32
cp $IN/images/install-whirl.bmp $OUT/install-win32
cp $IN/images/openvpn.ico $OUT/install-win32
cp $IN/INSTALL-win32.txt $OUT/install-win32
cat $IN/COPYING $IN/COPYRIGHT.GPL | $U2D >$OUT/install-win32/license.txt

$U2D <$IN/sample-config-files/client.conf >$OUT/install-win32/client.ovpn
$U2D <$IN/sample-config-files/server.conf >$OUT/install-win32/server.ovpn
cp $IN/install-win32/sample.ovpn $OUT/install-win32
$U2D <$IN/easy-rsa/openssl.cnf >$OUT/install-win32/openssl.cnf.sample

echo BUILD tap-win32

mkdir $OUT/tap-win32
cp $IN/tap-win32/*.[ch] $OUT/tap-win32
cp $IN/tap-win32/*.rc $OUT/tap-win32
cp $IN/tap-win32/MAKEFILE $OUT/tap-win32
cp $IN/tap-win32/SOURCES $OUT/tap-win32

mkdir $OUT/tap-win32/i386
cp $IN/tap-win32/i386/OemWin2k.inf $OUT/tap-win32/i386
cp $IN/tap-win32/i386/tap.cat $OUT/tap-win32/i386

cp $TAPBIN/tap0801.sys $OUT/tap-win32/i386

mkdir $OUT/bin/ti3790
cp $TAPBIN/tapinstall.exe $OUT/bin/ti3790

echo BUILD service-win32

mkdir $OUT/service-win32
cp $IN/service-win32/Makefile $OUT/service-win32
cp $IN/service-win32/mkpatch $OUT/service-win32
cp $IN/service-win32/openvpnserv.c $OUT/service-win32
cp $IN/service-win32/service.patch $OUT/service-win32

cp $SVC_TEMPLATE/service.[ch] $OUT/service-win32
cp $OUT/service-win32/service.h $OUT/service-win32/service.h.orig
cp $OUT/service-win32/service.c $OUT/service-win32/service.c.orig

pushd $OUT/service-win32
patch <service.patch
popd

echo BUILD easy-rsa
cp -a $IN/easy-rsa $OUT

echo BUILD sample-keys
cp -a $IN/sample-keys $OUT

echo COMPILE OpenVPN
pushd $OUT
make
strip openvpn.exe
popd

echo COMPILE OpenVPN Service
pushd $OUT/service-win32
make clean
make
strip openvpnserv.exe
popd
