#!/usr/bin/env bash

_wixdir="/c/Program Files (x86)/WiX Toolset v3.8"
_thisdir="$(dirname $0)"
test "${_thisdir}" = "." && _thisdir=${PWD}
_installer_root="${_thisdir}"/installer
_arch=$(uname -m)
_date=$(date +'%Y%m%d')
_dateqif=$(date +'%Y-%m-%d')
_version=@VERSION@
_filename=gedit-${_arch}-${_version}.msi
_log=/tmp/installer.log
if [ "${_arch}" = "x86_64" ]; then
  _bitness=64
else
  _bitness=32
fi

declare -a undo_commands

_exitcode=5

usage() {
  echo "Usage: $0 stage#"
  exit 1
}

if [ "$#" != "1" ]; then
  usage
fi

_stage="$1"
case "${_stage}" in
  stage1 | stage2)
    ;;
  *)
    usage
    ;;
esac

exit_with_undo() {
  for _cmd in ${undo_commands[@]}; do
    eval "$_cmd"
  done
  exit ${_exitcode}
}

exit_cleanly() {
  _exitcode=$1; shift;
  local _message=$1; shift;
  echo "${_message}"
  exit_with_undo
}

do_seds() {
  find "${_installer_root}" \( -name "defines.wxi" \) -exec sed -i "s|@VERSION@|${_version}|g" "{}" \;
  undo_commands+=("undo_seds")
}

undo_seds() {
  find "${_installer_root}" \( -name "defines.wxi" \) -exec sed -i "s|ProductVersion = \"${_version}\"|ProductVersion = \"@VERSION@\"|g" "{}" \;
}

_newgedit=/tmp/gedit

remove_useless_stuff() {
  # remove .a files not needed for the installer
  find installer/SourceDir -name "*.a" -exec rm -f {} \;
  # remove unneeded binaries
  find installer/SourceDir -not -name "g*.exe" -name "*.exe" -exec rm -f {} \;
  rm -rf installer/SourceDir/bin/gtk3-demo*.exe
  rm -rf installer/SourceDir/bin/gdbm*.exe
  rm -rf installer/SourceDir/bin/py*
  rm -rf installer/SourceDir/bin/*-config
  # remove other useless folders
  rm -rf installer/SourceDir/var
  rm -rf installer/SourceDir/ssl
  rm -rf installer/SourceDir/include
  rm -rf installer/SourceDir/share/man
  rm -rf installer/SourceDir/share/readline
  rm -rf installer/SourceDir/share/info
  rm -rf installer/SourceDir/share/aclocal
  rm -rf installer/SourceDir/share/gnome-common
  rm -rf installer/SourceDir/share/glade
  rm -rf installer/SourceDir/share/gettext
  rm -rf installer/SourceDir/share/terminfo
  rm -rf installer/SourceDir/share/tabset
  rm -rf installer/SourceDir/share/pkgconfig
  rm -rf installer/SourceDir/share/bash-completion
  rm -rf installer/SourceDir/share/appdata
  rm -rf installer/SourceDir/share/gdb
  # on windows we show the online help
  rm -rf installer/SourceDir/share/help
  rm -rf installer/SourceDir/share/gtk-doc
  rm -rf installer/SourceDir/share/doc
  # remove on the lib folder
  rm -rf installer/SourceDir/lib/terminfo
  rm -rf installer/SourceDir/lib/python2*
  rm -rf installer/SourceDir/lib/pkgconfig
  rm -rf installer/SourceDir/lib/peas-demo

  # strip the binaries to reduce the size
  find installer/SourceDir -name *.dll | xargs strip
  find installer/SourceDir -name *.exe | xargs strip

  # remove some translation which seem to add a lot of size
  find installer/SourceDir/share/locale/ -type f | grep -v atk10.mo | grep -v libpeas.mo | grep -v gsettings-desktop-schemas.mo | grep -v json-glib-1.0.mo | grep -v glib20.mo | grep -v gedit.mo | grep -v gedit-plugins.mo | grep -v gdk-pixbuf.mo | grep -v gtk30.mo | grep -v gtk30-properties.mo | grep -v gtksourceview-3.0.mo | grep -v iso_*.mo | xargs rm
  find installer/SourceDir/share/locale -type d | xargs rmdir -p --ignore-fail-on-non-empty
}

setup_source_dir() {
  cp -R "${_newgedit}/mingw${_bitness}" "installer/SourceDir"
  remove_useless_stuff
}

# Add -v to get more information.
make_installer() {
  setup_source_dir

  _platform="x86"
  if [ "${_arch}" = "x86_64" ]; then
    _platform="x64"
  fi

  pushd "installer" > /dev/null
  "${_wixdir}/bin/heat.exe" dir SourceDir -gg -dr INSTALLDIR -cg binaries -sfrag -sreg -srd -suid -template fragment -out binaries.wxs
  "${_wixdir}/bin/candle.exe" -arch ${_platform} gedit.wxs binaries.wxs
  "${_wixdir}/bin/light.exe" -ext WixUtilExtension -ext WixUIExtension gedit.wixobj binaries.wixobj -o "/tmp/${_filename}"
  popd
}

trap exit_with_undo 1 2 15

create_chroot_system() {
  [ -d ${_newgedit} ] && rm -rf ${_newgedit}
  mkdir -p "${_newgedit}"
  pushd "${_newgedit}" > /dev/null

  mkdir -p var/lib/pacman
  mkdir -p var/log
  mkdir -p tmp

  pacman -Syu --root "${_newgedit}"
  pacman -S filesystem bash pacman --noconfirm --root "${_newgedit}"
  _result=$?
  if [ "$_result" -ne "0" ]; then
    exit_cleanly "1" "failed to create base data via command 'pacman -S filesystem bash pacman --noconfirm --root ${_newgedit}'"
  fi
  popd > /dev/null
}

install_gedit_packages() {
  pacman -S mingw-w64-${_arch}-librsvg mingw-w64-${_arch}-gedit mingw-w64-${_arch}-gedit-plugins mingw-w64-${_arch}-gspell mingw-w64-${_arch}-adwaita-icon-theme --noconfirm --root "${_newgedit}"
  _result=$?
  if [ "$_result" -ne "0" ]; then
    exit_cleanly "1" "failed to create newgedit via command 'pacman -S gedit --noconfirm --root ${_newgedit}'"
  fi

  # some packages are pulled by the deps but we do not need them like python2
  pacman -Rdd mingw-w64-${_arch}-python2 --noconfirm --root "${_newgedit}"
  pacman -Rdd mingw-w64-${_arch}-gtk2 --noconfirm --root "${_newgedit}"
}

if [ "${_stage}" = "stage1" ]; then
  echo "Creating gedit chroot system ${_newgedit}"
  create_chroot_system
  exit 0
fi

echo "Installing gedit packages into ${_newgedit}"
install_gedit_packages

echo "Creating gedit installer /tmp/$_filename"
[ -f /tmp/$_filename ] && rm -f /tmp/$_filename

do_seds
make_installer
exit_cleanly "0" "All done, see ${_filename}"
