# Maintainer: Dominykas Svetikas <dominykas@svetikas.lt>
pkgname='magic-systems-of-systems-architect'
pkgver='2024.3' #2024x Refresh3
pkgrel='1'
pkgdesc='Powerful modeling tool based on the MagicDraw platform by No Magic'
url='https://docs.nomagic.com/display/MSOSA2024xR3'
arch=('any')
license=('custom')
depends=('java-runtime=11')
makedepends=('desktop-file-utils')
# Obtain from http://failai.isk.ktu.lt/
source=(
  "local://Magic_Systems_of_Systems_Architect_2024x_Refresh3_unix.sh"
  "installer.properties"
)
sha256sums=(
  'dcfeebb50a42c180ad743d82de391c4f46b80fca73a7fe9da52ea08fffae5075'
  'SKIP'
)

build() {
  # Fix for "Error: Could not find or load main class com.zerog.lax.LAX"
  export JAVA_TOOL_OPTIONS="-Djdk.util.zip.disableZip64ExtraFieldValidation=true"

  # Set the JAVA_HOME environment variable
  export PATH="/usr/lib/jvm/java-11-openjdk/bin/:$PATH"
  # Move every tempdir to the source directory, including the user.home
  export JAVA_TOOL_OPTIONS="$JAVA_TOOL_OPTIONS -Djava.io.tmpdir=$srcdir/tmp -Duser.dir=$srcdir/tmp/tmp2 -Duser.home=$srcdir"

  # Replace the $BUILD_PATH variable in the installer.properties file
  sed -e "s|\$BUILD_PATH|$PWD|" installer.properties > installer.properties.tmp

  # Specify the temporary directory for the installer
  # https://community.flexera.com/s/article/installanywhere-self-extraction-and-temp-space-requirements
  mkdir -p "$srcdir/tmp"
  export IATEMPDIR="$srcdir/tmp"

  # Create the required directories, otherwise warnings are generated and it fails
  mkdir -p "$srcdir/.local/share/applications"
  mkdir -p "$srcdir/magic-systems-of-systems-architect/userhome"

  chmod +x Magic_Systems_of_Systems_Architect_2024x_Refresh3_unix.sh
  ./Magic_Systems_of_Systems_Architect_2024x_Refresh3_unix.sh -f "$srcdir/installer.properties.tmp" -i console -l en

  # Update the desktop file with the correct path
  desktop-file-edit .local/share/applications/*.desktop --set-key=Exec --set-value="magic-systems-of-systems-architect"
  desktop-file-edit .local/share/applications/*.desktop --set-key=Icon --set-value="/usr/share/pixmaps/magic-systems-of-systems-architect.ico"
}

package() {
  cd "$srcdir/magic-systems-of-systems-architect" || exit 1

  # Install the application
  install -d $pkgdir/{opt/$pkgname,usr/bin}
  cp -a bin certs data help lib manual mdmain.ini modelLibraries openapi plugins profiles samples templates $pkgdir/opt/$pkgname
  ln -s /opt/$pkgname/bin/msosa $pkgdir/usr/bin/$pkgname

  # TODO: Change some permissions

  # Install the desktop file
  install -d "${pkgdir}/usr/share/pixmaps"
  install -Dm644 $srcdir/.local/share/applications/Magic_Systems_of_Systems_Architect_2024x_Refresh3.desktop $pkgdir/usr/share/applications/$pkgname.desktop
  ln -s /opt/$pkgname/bin/msosa.ico "${pkgdir}/usr/share/pixmaps/$pkgname.ico"
}
