mk/linux/build-appimage.sh
#!/bin/sh

# To test this script locally, from the source root directory,
# you can use:
#
# docker run -it --rm -e HOSTUID=$(id -u) -e \
# -v $PWD:/workspace -w /workspace andy5995/linuxdeploy:v2-focal bash
#
# Normally 'bash' can be replaced with the /path/to/script but starting
# the container from a shell means you can run the script manually in the
# container and then fix any problems, then re-run the script (hint: the
# dependencies will be installed the first time, and not have to be installed
# on subsequent attempts).
#
# Then: 'su - builder' (preserve some environmental variables with '-')
# 'bash'
# 'export VERSION=snapshot'
# 'cd /workspace'
# 'mk/linux/build-appimage.sh'

set -ev

if [ -z "$VERSION" ]; then
  echo "VERSION must be set"
  exit 1
fi

# Check if the workspace path is absolute
if [[ "$WORKSPACE" != /* ]]; then
  echo "The workspace path must be absolute"
  exit 1
fi
test -d "$WORKSPACE"

# Used by linuxdeploy when it sets the filename
export LINUXDEPLOY_OUTPUT_VERSION="$VERSION"

APPIMAGE_BUILD_DIR="$WORKSPACE/_build_appimage"
APPDIR="$APPIMAGE_BUILD_DIR/AppDir"
INST_PREFIX="usr"

#if [ -n "$DO_CLEAN_BUILD" ] && [ -d $APPIMAGE_BUILD_DIR ]; then
  #rm -rf $APPIMAGE_BUILD_DIR
#fi

if [ ! -d "$APPIMAGE_BUILD_DIR" ]; then
  mkdir -p "$APPIMAGE_BUILD_DIR"
fi

sudo DEBIAN_FRONTEND=noninteractive -i sh -c \
  "apt update &&
  apt upgrade -y &&
  apt install --no-install-recommends -y
    build-essential
    $CC
    libcurl4-gnutls-dev
    libsdl2-dev
    libopenal-dev
    liblua5.3-dev
    libjpeg-dev
    libpng-dev
    libfreetype6-dev
    libwxgtk3.0-gtk3-dev
    libcppunit-dev
    libfribidi-dev
    libftgl-dev
    libglew-dev
    libogg-dev
    libvorbis-dev
    libminiupnpc-dev
    libircclient-dev
    libxml2-dev
    libx11-dev
    libgl1-mesa-dev
    libglu1-mesa-dev
    librtmp-dev
    libkrb5-dev
    libldap2-dev
    libidn2-dev
    libpsl-dev
    libgnutls28-dev
    libnghttp2-dev
    libssh-dev
    libbrotli-dev
    p7zip-full
    imagemagick"

cd "$APPIMAGE_BUILD_DIR"
cmake $WORKSPACE \
  -DCMAKE_INSTALL_PREFIX=/$INST_PREFIX \
  -DBUILD_MEGAGLEST_MODEL_IMPORT_EXPORT_TOOLS=OFF
make DESTDIR=$APPDIR -j$(nproc) install
# This is done by linuxdeploy
# strip AppDir/$INST_PREFIX/local/bin/megaglest

GAME_DESKTOP_DEST="$APPDIR/$INST_PREFIX/share/applications"

if [ ! -d "$GAME_DESKTOP_DEST" ]; then
  mkdir -p "$GAME_DESKTOP_DEST"
fi

if [ ! -f "$GAME_DESKTOP_DEST/megaglest.desktop" ]; then
  cp "$WORKSPACE/data/glest_game/others/desktop/megaglest.desktop" "$GAME_DESKTOP_DEST"
fi

convert $APPDIR/$INST_PREFIX/share/megaglest/megaglest.ico megaglest.png
mv megaglest-2.png megaglest.png

MAP_EDITOR_DESKTOP_DEST="$APPDIR/$INST_PREFIX/share/applications"
if [ ! -d "$MAP_EDITOR_DESKTOP_DEST" ]; then
  mkdir -p "$MAP_EDITOR_DESKTOP_DEST"
fi

if [ ! -f "$MAP_EDITOR_DESKTOP_DEST/megaglest_editor.desktop" ]; then
  cp "$WORKSPACE/data/glest_game/others/desktop/megaglest_editor.desktop" "$MAP_EDITOR_DESKTOP_DEST"
  # Another stupid hack to fix icons.
  sed -i 's#Icon=megaglest#Icon=editor#' $MAP_EDITOR_DESKTOP_DEST/megaglest_editor.desktop
fi

#convert $APPDIR/$INST_PREFIX/share/megaglest/editor.ico editor.png
#mv editor-2.png editor.png

G3DVIEWER_DESKTOP_DEST="$APPDIR/$INST_PREFIX/share/applications"
if [ ! -d "$G3DVIEWER_DESKTOP_DEST" ]; then
  mkdir -p "$G3DVIEWER_DESKTOP_DEST"
fi

if [ ! -f "$G3DVIEWER_DESKTOP_DEST/megaglest_g3dviewer.desktop" ]; then
  cp "$WORKSPACE/data/glest_game/others/desktop/megaglest_g3dviewer.desktop" "$G3DVIEWER_DESKTOP_DEST"
  # Another stupid hack to fix icons.
  sed -i 's#Icon=megaglest#Icon=g3dviewer#' $G3DVIEWER_DESKTOP_DEST/megaglest_g3dviewer.desktop
fi

#convert $APPDIR/$INST_PREFIX/share/megaglest/g3dviewer.ico g3dviewer.png
#mv g3dviewer-2.png g3dviewer.png

linuxdeploy -d $GAME_DESKTOP_DEST/megaglest.desktop \
  --icon-file=megaglest.png \
  --icon-filename=megaglest \
  --custom-apprun="$WORKSPACE/mk/linux/AppRun" \
  --executable AppDir/$INST_PREFIX/bin/megaglest \
  --appdir AppDir \
  --plugin gtk \

ARCH=$(uname -m)
REPO="megaglest-source"
TAG="snapshot"
GITHUB_REPOSITORY_OWNER="${GITHUB_REPOSITORY_OWNER:-megaglest}"
UPINFO="gh-releases-zsync|$GITHUB_REPOSITORY_OWNER|$REPO|$TAG|*$ARCH.AppImage.zsync"
OUT_APPIMAGE="MegaGlest-snapshot-$ARCH.AppImage"

appimagetool --comp zstd \
  --mksquashfs-opt \
  -Xcompression-level \
  --mksquashfs-opt 20 \
  -u "$UPINFO" \
  "$APPDIR" "$OUT_APPIMAGE"

mv "$OUT_APPIMAGE" "$WORKSPACE"
mv "$OUT_APPIMAGE.zsync" "$WORKSPACE"

# Tools
#mkdir -p $APPIMAGE_BUILD_DIR/tools
#cd $APPIMAGE_BUILD_DIR/tools

#cmake $WORKSPACE -DBUILD_MEGAGLEST=OFF -DBUILD_MEGAGLEST_MODEL_VIEWER=OFF -DBUILD_MEGAGLEST_MAP_EDITOR=OFF -DBUILD_MEGAGLEST_MODEL_IMPORT_EXPORT_TOOLS=ON
#make -j$(nproc)

#strip source/tools/glexemel/g2xml source/tools/glexemel/xml2g
#mv source/tools/glexemel/g2xml $WORKSPACE
#mv source/tools/glexemel/xml2g $WORKSPACE
