#!/bin/bash
set -ex

#FIXME we may be able to do this with a Makefile
VCC="/cygdrive/c/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.40.33807/bin/Hostx64/x64/cl.exe"
RC="/cygdrive/c/Program Files (x86)/Windows Kits/10/bin/10.0.22621.0/x64/rc.exe"

# H: is a mount of home directory
SRC=/cygdrive/h/aldercone/hello/hello
HL=/cygdrive/h/software/hashlink-1.14.0-win/
BUILD=/cygdrive/c/Users/Alder/build/hello-build

VER=$(cat $SRC/build/stamp)
IVER=$(echo $VER | tr '.' ',')",0"

echo "Building Hello $VER ($IVER)"

# copy generated source and hashlink api
rm -r "$BUILD"-old || /bin/true
mv "$BUILD" "$BUILD"-old || /bin/true
cp -R "$SRC/build/" "$BUILD"
cp "$SRC/windows-build/"* "$BUILD"
cp -R "$HL" "$BUILD/hashlink"

# build using VCC
. /cygdrive/h/aldercone/WINDOWS-CMD-ENVIRONMENT-x64.sh

pushd "$BUILD"

sed -e "s;%IVER%;$IVER;g" -e "s;%VER%;$VER;g" <hello.rc>hello-done.rc

"$RC" hello-icon.rc
"$RC" hello-done.rc


"$VCC" -I. '-Ihashlink\include' hello.c 'hashlink\*.lib' /link /subsystem:windows hello-icon.res hello-done.res
mkdir -p "$SRC/out/windows" || /bin/true
cp hello.exe "$SRC/out/windows"
popd
