#!/bin/sh
#
# eclipse.SlackBuild
# 2014-03-30
#
# Eclipse is a multi-language software development environment comprising an
# integrated development environment (IDE) and an extensible plug-in system.
# It is written mostly in Java and can be used to develop applications in Java
# and other programming languages.

PRGNAM=eclipse
VERSION=4.3.2

# automatically determine the architecture
if [ -z "$ARCH" ]; then
  case "$(uname -m)" in
    i?86) ARCH=i586 ;;
    x86_64) ARCH=x86_64 ;;
       *) exit 1 ;;
  esac
fi

BUILD=${BUILD:-1}
TAG=${TAG:-scr}

HOMEPAGE="http://www.eclipse.org/"
if [ "$ARCH" = "i586" ]; then
  SOURCE="eclipse-standard-kepler-SR2-linux-gtk.tar.gz"
  DOWNLOAD="http://ftp-stud.fht-esslingen.de/pub/Mirrors/eclipse/technology/epp/downloads/release/kepler/SR2/${SOURCE}"
  MD5SUM="3fbb62eeef189dc0a45ff4c020588d6f"
elif [ "$ARCH" = "x86_64" ]; then
  SOURCE="eclipse-standard-kepler-SR2-linux-gtk-x86_64.tar.gz"
  DOWNLOAD="http://ftp-stud.fht-esslingen.de/pub/Mirrors/eclipse/technology/epp/downloads/release/kepler/SR2/${SOURCE}"
  MD5SUM="b72d5a6caae4e2fd86be2f2f134fc8cb"
fi

REQUIRES="jdk"

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

DESC="Eclipse

Eclipse is a multi-language software development environment
comprising an integrated development environment (IDE) and
an extensible plug-in system. It is written mostly in Java
and can be used to develop applications in Java and
other programming languages.

${HOMEPAGE}"

DOCS=""

# 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
tar xvf $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
cd ..
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/eclipse/eclipse "\$@"
EOF
chmod +x $PKG/usr/bin/$PRGNAM

mkdir -p $PKG/usr/share/pixmaps
cp icon.xpm $PKG/usr/share/pixmaps/$PRGNAM.xpm

mkdir -p $PKG/usr/share/applications
cat > $PKG/usr/share/applications/$PRGNAM.desktop << EOF
[Desktop Entry]
Type=Application
Name=Eclipse
GenericName=Integrated Development Environment
Exec=eclipse
Icon=eclipse
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:-tgz} \
  || exit 1

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