#!/bin/bash
if [ -z "$1" ]; then
    echo "Syntax: $0 <targetdir> [OFF]   (OFF = Qt5, default Qt6)" 1>&2
    exit 1
fi

TARGET=$1

QT6=1
if [[ "$2" == "OFF" ]]; then
   QT6=0
fi

mkdir -p "$TARGET"

rm -rfv "$TARGET"/*

mkdir -p "$TARGET/bin"

cp ../COPYING "$TARGET/bin"

rsync -a -m -v --include '*/' --include '*.wav' --exclude '*' ../data/sounds "$TARGET/bin"

rsync -a -m -v --include '*/' --include '*.png' --include '*.csv' --exclude '*' ../data/icons "$TARGET/bin"

if [[ "$QT6" == 1 ]]
then
rsync -a -m -v --include '*/' --include 'qt*cs.qm' --exclude '*' ${MINGW_PREFIX}/share/qt6/translations "$TARGET/bin"
rsync -a -m -v --include '*/' --include 'qt*sk.qm' --exclude '*' ${MINGW_PREFIX}/share/qt6/translations "$TARGET/bin"
rsync -a -m -v --include '*/' --include 'qwin*.dll' --exclude '*' ${MINGW_PREFIX}/share/qt6/plugins/platforms "$TARGET/bin"
else
rsync -a -m -v --include '*/' --include 'qt*cs.qm' --exclude '*' ${MINGW_PREFIX}/share/qt5/translations "$TARGET/bin"
rsync -a -m -v --include '*/' --include 'qt*sk.qm' --exclude '*' ${MINGW_PREFIX}/share/qt5/translations "$TARGET/bin"
rsync -a -m -v --include '*/' --include 'qwin*.dll' --exclude '*' ${MINGW_PREFIX}/share/qt5/plugins/platforms "$TARGET/bin"
fi

mkdir -p "$TARGET/bin/locale/cs/LC_MESSAGES"
cp ../build/data/translations/cs.gmo "$TARGET/bin/locale/cs/LC_MESSAGES/dxirc.mo"

mkdir -p "$TARGET/bin/locale/sk/LC_MESSAGES"
cp ../build/data/translations/sk.gmo "$TARGET/bin/locale/sk/LC_MESSAGES/dxirc.mo"

installapp ../build/fox "$TARGET/bin"
installapp ../build/qt "$TARGET/bin"

# Qt plugins (tls for SSL, multimedia for sounds, styles, imageformats), translations are copied above
if [[ "$QT6" == 1 ]]
then
# no multimedia plugin: sounds use QSoundEffect, the ffmpeg plugin would pull FFmpeg with all codecs
windeployqt6 --no-translations --skip-plugin-types multimedia "$TARGET/bin/dxirc-qt.exe"
else
windeployqt --no-translations "$TARGET/bin/dxirc-qt.exe"
fi

mkdir -p "$TARGET/lib"
rsync -a -m -v --include '*/' --include '*.dll' --include '*.cache' --exclude '*' ${MINGW_PREFIX}/lib/enchant-2 "$TARGET/lib"

mkdir -p "$TARGET/share/enchant"
rsync -a -m -v --include '*/' --include '*.aff' --include '*.dic' --include '*.txt' --exclude '*' ${MINGW_PREFIX}/share/enchant-2/hunspell "$TARGET/share/enchant"

DLLSOURCE=${MINGW_PREFIX}/bin/
copydll() {
  b="$(basename $1)"
  echo "Copy dll for '$b'"
  strings "$1" | grep '\.dll' | while read dll
  do
    DLL=`find "$DLLSOURCE" -maxdepth 1 -name "$dll" -print -quit`
    if [ ! -z "$DLL" ]; then
      FILE="$TARGET/bin/$(basename $DLL)"
      if [ ! -f "$FILE" ]; then
         cp -a "$DLL" "$TARGET/bin/"
         copydll "$DLL"
      fi
    fi
  done
}

pacman -Ql ${MINGW_PACKAGE_PREFIX}-openssl | grep '\.dll$' | while read dll
do
  path=${dll#* }
  copydll "$path"
done

pacman -Ql ${MINGW_PACKAGE_PREFIX}-enchant | grep '\.dll$' | while read dll
do
  path=${dll#* }
  copydll "$path"
done

# dependencies of the Qt plugins (e.g. FFmpeg for the multimedia plugin), windeployqt copies only Qt libraries
find "$TARGET/bin" -mindepth 2 -name '*.dll' | while read dll
do
  copydll "$dll"
done
