#!/bin/sh
#
# phpmyadmin.SlackBuild
# 2013-11-26
#
# phpMyAdmin is a free software tool written in PHP intended to
# handle the administration of MySQL over the World Wide Web.

PRGNAM=phpmyadmin
VERSION=3.5.8.2
ARCH=${ARCH:-noarch}
BUILD=${BUILD:-1}
TAG=${TAG:-scr}

HOMEPAGE="http://www.phpmyadmin.net/"
SOURCE="phpMyAdmin-${VERSION}-all-languages.tar.xz"
DOWNLOAD="http://downloads.sourceforge.net/project/phpmyadmin/phpMyAdmin/${VERSION}/${SOURCE}"
MD5SUM="ea74a4a59588ccc15c528e214e7124f7"

REQUIRES="httpd php"

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

DESC="phpMyAdmin

phpMyAdmin is a free software tool written in PHP intended to
handle the administration of MySQL over the World Wide Web.

${HOMEPAGE}"

DOCS="$(pwd)/$0 ChangeLog Documentation.* LICENSE README* RELEASE-DATE-*"

# EOF info


DOCROOT=${DOCROOT:-var/www/htdocs}

# 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

# fix permissions
chown -R root:root ./phpMyAdmin-$VERSION-all-languages

# install everything into the package directory
rm -rf $PKG
mkdir -p $PKG/install
mkdir -p $PKG/$DOCROOT
mv ./phpMyAdmin-$VERSION-all-languages $PKG/$DOCROOT/phpMyAdmin || exit 1
cd $PKG/$DOCROOT/phpMyAdmin

# 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

# doinst.sh
cat > $PKG/install/doinst.sh <<EOF
#!/bin/sh
if [ ! -f $DOCROOT/phpMyAdmin/config.inc.php ]; then
  RAND=\$(head -c6 /dev/urandom | base64)
  sed "s/\['blowfish_secret'\] = '.*'/\['blowfish_secret'\] = '\$RAND'/" \\
    $DOCROOT/phpMyAdmin/config.sample.inc.php \\
    > $DOCROOT/phpMyAdmin/config.inc.php
fi
EOF

# build the package
cd $PKG
makepkg -l y -c n \
  $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz} \
  || exit 1

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