#!/bin/sh
#
# argouml.SlackBuild
# 2012-02-16
#

PRGNAM=argouml
VERSION=0.34
ARCH=noarch
BUILD=${BUILD:-1}
TAG=${TAG:-scr}

HOMEPAGE="http://argouml.tigris.org/"
SOURCE="ArgoUML-${VERSION}.tar.gz"
DOWNLOAD="http://argouml-downloads.tigris.org/nonav/argouml-${VERSION}/${SOURCE}"
MD5SUM="440efea6096a00b8c19daab1ecddaf7c"

REQUIRES="jdk"

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

DESC="ArgoUML

ArgoUML is the leading open source UML modeling tool and
includes support for all standard UML 1.4 diagrams.
It runs on any Java platform and is available in ten languages.

${HOMEPAGE}"

DOCS="$(pwd)/$0 README.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

# 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-$VERSION || exit 1

# fix permissions
chown -R root:root .
chmod -R u+w,go+r-w,a-s .
find . -type f ! -name '*.sh' -exec chmod -x {} \;

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

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

# 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
