#!/bin/sh

# Heavily based on the Slackware 13.1 SlackBuild
# Written by Tommaso D'Anna aka tasodan ( www.slacky.eu )
# Last build from Tommaso D'Anna aka tasodan ( www.slacky.eu )

# Slackware build script for Vim LaTeX Suite
# Official Site: http://vim-latex.sourceforge.net

# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

set -e

# Catching variables
PKGNAME=${PKGNAME:-vim-latex}
SUFFIX=${SUFFIX:-suite}
VERSION=${VERSION:-1.8.23}
REVISION=${REVISION:-20100129-r1104}
VIMVER=${VIMVER:-vim72}
BUILD=${BUILD:-1}
TAG=${TAG:-sl}
ARCH=${ARCH:-noarch}
SOURCE="http://downloads.sourceforge.net/$PKGNAME/$PKGNAME-$VERSION-$REVISION.tar.gz"

CWD=${CWD:-`pwd`}
TMP=${TMP:-/tmp/buildpkgs/$PKGNAME-$SUFFIX}
PKG=${PKG:-$TMP/package-$PKGNAME-$SUFFIX}
OUTPUT=${OUTPUT:-$CWD}

# Source file avaiability
if [ ! -e $CWD/$PKGNAME-$VERSION-$REVISION.tar.gz ] ; then
  wget $SOURCE
fi

# Create working directories:
rm -rf $TMP
mkdir -p $TMP $PKG $OUTPUT

# Package building
cd $TMP
tar xvf $CWD/$PKGNAME-$VERSION-$REVISION.tar.gz
cd $PKGNAME-$VERSION-$REVISION

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

mkdir -p $PKG/usr/share/vim/$VIMVER/{compiler,doc,ftplugin,indent,plugin}
cp -a compiler/* $PKG/usr/share/vim/$VIMVER/compiler
cp -a doc/*.txt $PKG/usr/share/vim/$VIMVER/doc
cp -a ftplugin/* $PKG/usr/share/vim/$VIMVER/ftplugin
cp -a indent/* $PKG/usr/share/vim/$VIMVER/indent
cp -a plugin/* $PKG/usr/share/vim/$VIMVER/plugin

# Adding documentation
mkdir -p $PKG/usr/doc/$PKGNAME-$SUFFIX-$VERSION
cp -a doc/{README,README.new} $PKG/usr/doc/$PKGNAME-$SUFFIX-$VERSION
cat $CWD/$PKGNAME-$SUFFIX.SlackBuild > $PKG/usr/doc/$PKGNAME-$SUFFIX-$VERSION/$PKGNAME-$SUFFIX.SlackBuild
cat $CWD/slack-desc > $PKG/usr/doc/$PKGNAME-$SUFFIX-$VERSION/slack-desc
cat $CWD/README.Slackware > $PKG/usr/doc/$PKGNAME-$SUFFIX-$VERSION/README.Slackware

# Adding slack-desc and doinst.sh
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
chown -R root:root $PKG

# Make the package
/sbin/makepkg -l y -c n $OUTPUT/$PKGNAME-$SUFFIX-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-txz}

# Clean up the extra stuff
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP $PKG
fi
