#!/bin/sh
#
# netbeans.SlackBuild
# 2024-05-20
#
# Develop desktop, mobile and web applications with Java, PHP, C/C++ and more.

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

HOMEPAGE="https://netbeans.apache.org/"
SOURCE="netbeans-${VERSION}-bin.zip"
DOWNLOAD="https://dlcdn.apache.org/netbeans/netbeans/${VERSION}/${SOURCE}"
MD5SUM="5c4def09c0166b16a32ec6142252a494"

REQUIRES="openjdk"

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

DESC="NetBeans IDE

Develop desktop, mobile and web applications with
Java, PHP, C/C++ and more.

${HOMEPAGE}"

DOCS="$(pwd)/$0 DEPENDENCIES LICENSE NOTICE README.html netbeans.css"

# 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

# check for correct source file
if [ "$(md5sum $CWD/$SOURCE | cut -f 1 -d ' ')" != "$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 .

# remove empty files
find . -name '.lastModified' -exec rm {} \;

# install everything into the package directory
cd $TMP
rm -rf $PKG
mkdir -p $PKG/install
mkdir -p $PKG/opt
mv $PRGNAM $PKG/opt
cd $PKG/opt/$PRGNAM

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

mkdir -p $PKG/usr/share/pixmaps
cp nb/netbeans.png $PKG/usr/share/pixmaps/

mkdir -p $PKG/usr/share/applications
cat > $PKG/usr/share/applications/$PRGNAM.desktop << EOF
[Desktop Entry]
Type=Application
Name=NetBeans
GenericName=Integrated Development Environment
Exec=netbeans
Icon=netbeans
Categories=Development;
EOF

# add documentation
if [ "$DOCS" != "" ]; then
  mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
  cp -a $DOCS $PKG/usr/doc/$PRGNAM-$VERSION
  find $PKG/usr/doc/$PRGNAM-$VERSION -type f -exec chmod 644 {} \;
fi

# 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
