#!/bin/bash
#
# Script for creating a self-contained Wings package for Windows.
#

#
# The pre-built Erlang/OTP R15B for Windows requires this version
# of the VC100.CRT. It must be updated if future release of Erlang
# requires a later version.
#
WINGS_VCREDIST_VERSION=10.00.40219.1
WINGS_VCREDIST64_VERSION=10.00.30319.1

OS=`uname -o 2> /dev/null`
if [ X$OS != XMsys ]; then
    OS=`uname -s`
    if [ X$OS != XMINGW32_NT-6.1 ]; then
	echo "This script is supposed to be run in the" \
	    "msys environment on windows only." >&2
	exit 1
    fi
fi

# Configurable stuff.
TEMPDIR=$TMP
INSTALL=/usr/bin/install
NSIS=`which makensis 2>/dev/null`
if [ -z "$NSIS" ]; then
    NSIS=c:/program/nsis/makensis
fi

DEST_ROOT="build"

ESDL_PATH=`erl -noshell -eval "io:put_chars(code:lib_dir(esdl))" -run erlang halt`

ESDL_NAME=`basename $ESDL_PATH`
if [ X$ESDL_NAME == Xesdl ]; then
    echo $ESDL_NAME without version
    source $ESDL_PATH/vsn.mk
    ESDL_NAME=$ESDL_VSN
    echo Install esdl to $ESDL_NAME
fi

ERLANG_PATH=`which erl`
ERLANG_PATH=`dirname "$ERLANG_PATH"`
ERLANG_PATH=`dirname "$ERLANG_PATH"`

WIN_X86=`erl -noshell -eval "io:format([126,115,126,110],[erlang:system_info(wordsize)==4])" -s erlang halt`

if [ ! -f "$WINGS_VCREDIST" ]; then
    echo "The environment variable WINGS_VCREDIST must point out vcredist_x86.exe. " \
	 "If you have Visual Studio 2005 installed, try 'find "/c/Program Files" -name vcredist_x86.exe'." \
	 "Otherwise, you can download it from Microsoft (try googling 'vcredist_x86.exe')." >&2
    exit 1
fi

if [ X$WIN_X86 == Xfalse ]; then
    WINGS_VCREDIST=`dirname "$WINGS_VCREDIST"`
    WINGS_VCREDIST="$WINGS_VCREDIST"/vcredist_x64.exe
    WINGS_VCREDIST_VERSION=$WINGS_VCREDIST64_VERSION
    NSI_FILE=wings64.nsi
else
    NSI_FILE=wings.nsi
fi

# Copy the Wings application.

WINGS_DIR="`pwd`"
WINGS_VSN=`sed -n "/WINGS_VSN/s/^WINGS_VSN[^0-9]*//p" vsn.mk`
WINGS_APP="wings-$WINGS_VSN"

dest="$DEST_ROOT"

$INSTALL -d "$dest"
$INSTALL -c -m 644 "$WINGS_VCREDIST" "$dest"
$INSTALL -c -m 644 README AUTHORS license.terms vsn.mk "$dest"
$INSTALL -c -m 644 win32/$NSI_FILE "$dest"
$INSTALL -c -m 644 win32/Wings3D.exe "$dest"
$INSTALL -c -m 644 win32/wings.ico "$dest"
$INSTALL -c -m 644 win32/install.ico "$dest"
$INSTALL -c -m 644 win32/uninstall.ico "$dest"

dest="$DEST_ROOT/lib/$WINGS_APP"

$INSTALL -d "$dest" "$dest/ebin" "$dest/patches"
$INSTALL -c -m 644 ebin/* "$dest/ebin"
$INSTALL -d  "$dest/fonts"

$INSTALL -d "$dest/textures"
$INSTALL -d "$dest/shaders"
$INSTALL -c -m 644 textures/* "$dest/textures"
$INSTALL -c -m 644 shaders/* "$dest/shaders"

$INSTALL -d "$dest/plugins/default" "$dest/plugins/accel" \
 "$dest/plugins/import_export" "$dest/plugins/commands" \
 "$dest/plugins/primitives" "$dest/plugins/win32_file" \
 "$dest/plugins/autouv" "$dest/plugins/jpeg"

$INSTALL -c -m 644 fonts/* "$dest/fonts"

$INSTALL -c -m 644 plugins/default/wp9_dialogs* "$dest/plugins/default"
$INSTALL -c -m 644 plugins/accel/* "$dest/plugins/accel"
$INSTALL -c -m 644 plugins/import_export/* "$dest/plugins/import_export"
$INSTALL -c -m 644 plugins/commands/* "$dest/plugins/commands"
$INSTALL -c -m 644 plugins/primitives/* "$dest/plugins/primitives"
$INSTALL -c -m 644 plugins/win32_file/* "$dest/plugins/win32_file"
$INSTALL -c -m 644 plugins/autouv/* "$dest/plugins/autouv"
$INSTALL -c -m 644 plugins/jpeg/* "$dest/plugins/jpeg"

# Copy the relevant parts of Erlang/OTP.

src="$ERLANG_PATH"
dest="$DEST_ROOT"
erts_src=`echo "$src"/erts-*`
stdlib=`basename "$src"/lib/stdlib-*`
kernel=`basename "$src"/lib/kernel-*`
xmerl=`basename "$src"/lib/xmerl-*`

$INSTALL -d "$dest/bin" \
    "$dest/lib/$stdlib/ebin" \
    "$dest/lib/$kernel/ebin" \
    "$dest/lib/$xmerl/ebin"

$INSTALL -c -m 644 "$src"/lib/kernel-*/ebin/* "$dest/lib/$kernel/ebin"
$INSTALL -c -m 644 "$src"/lib/stdlib-*/ebin/* "$dest/lib/$stdlib/ebin"
$INSTALL -c -m 644 "$src"/lib/xmerl-*/ebin/* "$dest/lib/$xmerl/ebin"

$INSTALL -c "$erts_src/bin/erl.exe" "$dest/bin"
$INSTALL -c "$erts_src/bin/werl.exe" "$dest/bin"
$INSTALL -c "$erts_src/bin/erlexec.dll" "$dest/bin"
$INSTALL -c "$erts_src/bin/beam.smp.dll" "$dest/bin"
$INSTALL -c -m 644 "$src/bin/start.boot" "$dest/bin"
$INSTALL -c "$erts_src/bin/inet_gethost.exe" "$dest/bin/inet_gethost.exe"

# Copy the relevant parts of ESDL.
dest="$DEST_ROOT/lib/$ESDL_NAME"
$INSTALL -d "$dest/ebin" "$dest/priv"
$INSTALL -c -m 644  "$ESDL_PATH"/ebin/* "$dest/ebin"
$INSTALL -c -m 644  "$ESDL_PATH"/priv/sdl_driver.dll "$dest/priv"
$INSTALL -c -m 644  "$ESDL_PATH"/priv/SDL.dll "$dest/priv"
# May be built with microsofts tools don't strip
#strip --strip-debug --strip-unneeded "$dest/priv/sdl_driver.dll"
rm -rf "$dest/ebin/sdl_audio.beam" "$dest/ebin/sdl_joystick.beam"

# Copy the relevant parts of wx (we use opengl from it)
WX_PATH=`erl -noshell -eval "io:format([126,115,126,110],[code:lib_dir(wx)])" -s erlang halt`
WX_NAME=`basename "$WX_PATH"`
dest="$DEST_ROOT/lib/$WX_NAME"
$INSTALL -d "$dest/ebin" "$dest/priv"
$INSTALL -c -m 644  "$WX_PATH"/ebin/gl.beam "$dest/ebin"
$INSTALL -c -m 644  "$WX_PATH"/ebin/glu.beam "$dest/ebin"
$INSTALL -c -m 644  "$WX_PATH"/priv/erl_gl.dll "$dest/priv"
# Built with microsofts tools don't strip
#strip --strip-debug --strip-unneeded "$dest/priv/erl_gl.dll"

# OpenCL (currently requires the dev branch, which contains win64 fixes)
CL_PATH=`erl -noshell -eval "io:format([126,115,126,110],[code:lib_dir(cl)])" -s erlang halt`
CL_NAME=`basename $CL_PATH`
if [ X$CL_NAME == Xcl ]; then
    echo $CL_NAME without version
    CL_VSN=`sed -n "/CL_VSN/s/^CL_VSN[^0-9]*//p" $CL_PATH/vsn.mk`
    CL_NAME="cl-$CL_VSN"
    echo Install cl to $CL_NAME
fi
dest="$DEST_ROOT/lib/$CL_NAME"
$INSTALL -d "$dest/ebin" "$dest/priv"
$INSTALL -c -m 644  "$CL_PATH"/ebin/cl.beam "$dest/ebin"
$INSTALL -c -m 644  "$CL_PATH"/ebin/cl1?.beam "$dest/ebin"
$INSTALL -c -m 644  "$CL_PATH"/ebin/clu.beam "$dest/ebin"
$INSTALL -c -m 644  "$CL_PATH"/priv/cl_nif.dll "$dest/priv"
# May be built with microsofts tools don't strip
#strip --strip-debug --strip-unneeded "$dest/priv/cl_nif.dll"

#
# Remove files from OTP not needed.
#
$WINGS_DIR/tools/wings_reduce_otp "$DEST_ROOT"

# Strip debug information from all beam files.
cd "$latest_wings"
erlc -o "$TEMPDIR" tools/strip.erl
cd "$DEST_ROOT"
erl -noshell -pa $TEMPDIR -run strip strip

# Run NSIS
"$NSIS" //DREDIST_DLL_VERSION=$WINGS_VCREDIST_VERSION //DWINGS_VERSION=$WINGS_VSN $NSI_FILE

cd "$WINGS_DIR"
cp wings*.exe ..
