#!/bin/sh

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

# Slackware build script for Mozilla Firefox Language Packs
# Official Site: http://www.mozilla.com

# 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:-mozilla-firefox-l10n-it}
VERSION=${VERSION:-3.6.16}
BUILD=${BUILD:-1}
TAG=${TAG:-sl}
ARCH=${ARCH:-i686}
SOURCE="http://releases.mozilla.org/pub/mozilla.org/firefox/releases/$VERSION/linux-i686/xpi/it.xpi"

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

CHOST="i486"
if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
  CHOST="x86_64"
fi

# Source file avaiability
if [ ! -e $CWD/it.xpi ];then
  wget $SOURCE
fi

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

# Package building
cp -a it.xpi $TMP
cd $TMP
unzip it.xpi

mkdir -p $PKG/usr/lib$LIBDIRSUFFIX/firefox-$VERSION/extensions/langpack-it@firefox.mozilla.org
cp -a chrome chrome.manifest install.rdf \
  $PKG/usr/lib$LIBDIRSUFFIX/firefox-$VERSION/extensions/langpack-it@firefox.mozilla.org/
mkdir -p $PKG/usr/lib$LIBDIRSUFFIX/firefox-$VERSION/defaults/pref
cp $CWD/langpacks.js $PKG/usr/lib$LIBDIRSUFFIX/firefox-$VERSION/defaults/pref/

# Adding documentation
mkdir -p $PKG/usr/doc/$PKGNAME-$VERSION
cat $CWD/$PKGNAME.SlackBuild > $PKG/usr/doc/$PKGNAME-$VERSION/$PKGNAME.SlackBuild
cat $CWD/slack-desc > $PKG/usr/doc/$PKGNAME-$VERSION/slack-desc

# Adding slack-desc
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# Change permissions
cd $PKG
chown -R root:root .

find . \( -perm 777 -o -perm 770 -o -perm 711 \) -exec chmod 755 {} \;
find . \( -perm 700 -o -perm 555 -o -perm 511 \) -exec chmod 755 {} \;
find . \( -perm 666 -o -perm 664 -o -perm 600 \) -exec chmod 644 {} \;
find . \( -perm 444 -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

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

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