#!/bin/bash

# Needs bash because of ${x/exe/sha} expression.

date=$(date +%Y%m%d)
year=$(date +%Y)

WEBDIR=/var/www/html

targets="i686-w64-mingw32 x86_64-w64-mingw32"

if false; then
(
  echo Building i686-w64-mingw32...
  mkdir -p /home/stefan/src/qemu/repo.or.cz/qemu/ar7/bin/ndebug/i686-w64-mingw32
  cd /home/stefan/src/qemu/repo.or.cz/qemu/ar7/bin/ndebug/i686-w64-mingw32 && rm -rf *
  ../../../configure --cross-prefix=i686-w64-mingw32- --disable-guest-agent-msi --disable-werror --enable-strip --extra-cflags="-I/mingw32/include" --extra-ldflags="-L/mingw32/lib"
  make -O -j4
  make -C /home/stefan/src/qemu/repo.or.cz/qemu/ar7/bin/ndebug/i686-w64-mingw32 installer SIGNCODE=signcode
  mv /home/stefan/src/qemu/repo.or.cz/qemu/ar7/bin/ndebug/i686-w64-mingw32/qemu-setup-*.exe $WEBDIR/w32/qemu-w32-setup-$date.exe
)
fi

if grep -q exe /mingw64/lib/pkgconfig/gio-2.0.pc; then
  echo Please patch /mingw64/lib/pkgconfig/gio-2.0.pc
  exit 1
fi

(
  target=x86_64-w64-mingw32

  export PKG_CONFIG_PATH=/mingw64/lib/pkgconfig:/mingw64/share/pkgconfig

  echo Building $target...
  rm -rf /home/stefan/src/qemu/repo.or.cz/qemu/ar7/bin/ndebug/$target
  mkdir -p /home/stefan/src/qemu/repo.or.cz/qemu/ar7/bin/ndebug/$target
  cd /home/stefan/src/qemu/repo.or.cz/qemu/ar7/bin/ndebug/$target
  # -DNO_OLDNAMES geht nicht.
  # -D_MSC_VER geht nicht.
  ../../../configure --cross-prefix=${target}- --disable-guest-agent-msi --disable-werror --enable-strip --extra-cflags="-D_FORTIFY_SOURCE=2 -D__pragma(x)= -idirafter /mingw64/include" --extra-ldflags="-L/mingw64/lib"
  make -O -j4
  make -C /home/stefan/src/qemu/repo.or.cz/qemu/ar7/bin/ndebug/$target installer SIGNCODE=signcode
  instdir=$WEBDIR/w64/$year
  mkdir -p $instdir
  mv -v /home/stefan/src/qemu/repo.or.cz/qemu/ar7/bin/ndebug/$target/qemu-setup-*.exe $instdir/qemu-w64-setup-$date.exe

  echo Calculate SHA-512 checksums...
  cd $instdir
  sha512sum qemu-w64-setup-$date.exe >qemu-w64-setup-$date.sha512
  cd ..
  ln -sv $year/qemu-w64-setup-$date.* .
)

echo Update documentation...
cp -auv /home/stefan/src/qemu/repo.or.cz/qemu/ar7/bin/ndebug/x86_64-w64-mingw32/docs/manual/* /var/www/html/doc/latest/

echo Get installer for WoA...
instdir=$WEBDIR/aarch64/$year
mkdir -p $instdir
cd $instdir
wget https://v22024089067283036.luckysrv.de/~stefan/qemu-arm-setup-$date.exe
wget https://v22024089067283036.luckysrv.de/~stefan/qemu-arm-setup-$date.sha512
cd ..
ln -sv $year/qemu-arm-setup-$date.* .

echo Done.
