#!/bin/sh
#
# ArtOfIllusion.SlackBuild
# 2024-05-16
#

PRGNAM=ArtOfIllusion
VERSION=3.2.1
ARCH=noarch
BUILD=${BUILD:-1}
TAG=${TAG:-scr}
PKGTYPE=${PKGTYPE:-txz}

HOMEPAGE="http://www.artofillusion.org/"
SOURCE="ArtOfIllusion_${VERSION}_no-install.zip"
DOWNLOAD="https://downloads.sourceforge.net/project/aoi/ArtOfIllusion/${VERSION}/Art%20of%20Illusion%20${VERSION}%20no-install.zip"
MD5SUM="f62988d73caf24a3b5b7e0aafdfa6c17"

MANUAL="ArtOfIllusion_${VERSION}_manual.zip"
MANUAL_DOWNLOAD="https://downloads.sourceforge.net/project/aoi/ArtOfIllusion-manual/${VERSION}/Art%20of%20Illusion%20${VERSION}%20manual.zip"
MANUAL_MD5SUM="96b7692ca5edbe6ea4da1c2056141069"

REQUIRES="openjdk"

MAINTAINER="Gernot Walzl"
EMAIL="gernot.walzl@gmx.at"

DESC="Art of Illusion

Art of Illusion is a free, open source 3D modelling and
rendering studio. Many of its capabilities rival those
found in commercial programs. Highlights include subdivision
surface based modelling tools, skeleton based animation,
and a graphical language for designing procedural textures
and materials.

${HOMEPAGE}"

DOCS="$(pwd)/$0 License.txt"

# EOF info


# set initial variables
CWD=$(pwd)
TMP=${TMP:-/tmp}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# exit on error
set -e

# download the source if needed
if [ ! -f $CWD/$SOURCE ]; then
  wget -O $CWD/$SOURCE $DOWNLOAD || exit 1
fi
if [ ! -f $CWD/$MANUAL ]; then
  wget --no-check-certificate -O $CWD/$MANUAL $MANUAL_DOWNLOAD || exit 1
fi

# check for correct source file
if [ "$(md5sum $CWD/$SOURCE | cut -f 1 -d ' ')" != "$MD5SUM" ]; then
  exit 1
fi
if [ "$(md5sum $CWD/$MANUAL | cut -f 1 -d ' ')" != "$MANUAL_MD5SUM" ]; then
  exit 1
fi

# extract the application source
cd $TMP || exit 1
unzip $CWD/$SOURCE || exit 1

# change to the application source directory
cd $PRGNAM || exit 1

# fix permissions
chown -R root:root .
chmod -R u+w,go+r-w,a-s .

# install everything into the package directory
rm -rf $PKG
mkdir -p $PKG/install
cd ..
mkdir -p $PKG/opt
rm -f "${PRGNAM}/Art of Illusion.exe"
mv $PRGNAM $PKG/opt/$PRGNAM
cd $PKG/opt/$PRGNAM

# add to /usr/bin
mkdir -p $PKG/usr/bin
cat > $PKG/usr/bin/$PRGNAM << EOF
#!/bin/sh
java -jar /opt/ArtOfIllusion/ArtOfIllusion.jar "\$@"
EOF
chmod +x $PKG/usr/bin/$PRGNAM

# add documentation
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a $DOCS $PKG/usr/doc/$PRGNAM-$VERSION
cd $PKG/usr/doc/$PRGNAM-$VERSION
unzip $CWD/$MANUAL
find $PKG/usr/doc/$PRGNAM-$VERSION -type f -exec chmod 644 {} \;
cd $PKG/opt/$PRGNAM

# add some description to the package
if [ "$DESC" != "" ]; then
  echo "$DESC" | sed 's/^/'$PRGNAM': /g' > $PKG/install/slack-desc
fi

# append dependency information to the package
if [ "$REQUIRES" != "" ]; then
  for REQ in $REQUIRES; do
    echo "$REQ" >> $PKG/install/slack-required
  done
fi

# build the package
cd $PKG
PKGVERSION=$(echo $VERSION | sed 's/-/./g')
makepkg -l y -c n \
  $OUTPUT/$PRGNAM-$PKGVERSION-$ARCH-$BUILD$TAG.$PKGTYPE \
  || exit 1

# make everything clean
cd $CWD
rm -rf $PKG
