#!/usr/bin/bash
# This script assumes it will be launched within "/NAME/VERSION/src" dir.
# With all sources in "src" Your Vector Linux .tlz package, slack-desc,
# and slack-required will be found in "VERSION" dir. The extraction and
# build will be in a temp dir created in "NAME" dir, and then removed on exit.
# Comment out second to last line to keep this dir intact. (pkg placement
# altered due to nature of these pkgs: 1 src dir, many pkgs)
# This Template was compiled from the contributions of many users of the Vector
# Linux forum at http://forum.vectorlinux.com and from tidbits collected 
# from all over the internet. 
#
# Generated by sbbuilder-0.4.12, written by Rodrigo Bistolfi 
# (rbistolfi) and Raimon Grau Cuscó (Kidd) for VectorLinux.
#
# Please put your name below if you add some original scripting lines.
# AUTHORS = incognu

NAME="seamonkey_langpack"            #Enter package Name! but redefine it when have $LANG variable
VERSION=${VERSION:-"2.0.11"}      #Enter package Version!
VER=$(echo $VERSION|sed 's/-/_/') #this fixes - in version
VL_PACKAGER=${VL_PACKAGER:-"toothandnail"}   #Enter your Name!
LINK=${LINK:-"ftp://ftp.mozilla.org/pub/mozilla.org/seamonkey/releases/$VERSION/langpack/"}  #Enter URL for package here!


#SYSTEM VARIABLES
#----------------------------------------------------------------------------
BUILDNUM=${BUILDNUM:-"1"}
VL_VERSION=${VL_VERSION:-"$(ls /var/log/packages/|grep vlconfig2|cut -d "-" -f4|cut -c 2-5)"}
BUILD=${BUILD:-"$BUILDNUM""$VL_VERSION"}
ARCH=${ARCH:-"$(uname -m)"}
CONFIG_OPTIONS=${CONFIG_OPTIONS:-""}
LDFLAG_OPTIONS=${LDFLAG_OPTIONS:-""}
ADDRB=${ADDRB:-""} #Add deps that need to be added to the slack-required file here
EXRB=${EXRB:-""} #Add deps that need to be excluded from the slack-required file here
#----------------------------------------------------------------------------


#SETUP PACKAGING ENVIRONMENT
#--------------------------------------------
CWD=$(pwd)
cd ../
RELEASEDIR=$(pwd)
cd $CWD
mkdir -p $RELEASEDIR/tmp
TMP=$RELEASEDIR/tmp
PKG=$TMP/package-$NAME # but redefine this using variable for individual package below
#--------------------------------------------

if [ $UID != 0 ]; then
   echo "You are not authorized to run this script. Please login as root"
   exit 1
fi

if [ ! -x /usr/bin/requiredbuilder ]; then
   echo "Requiredbuilder not installed, or not executable."
   exit 1
fi

if [ $VL_PACKAGER = "YOURNAME" ]; then
   echo 'Who are you?
   Please edit VL_PACKAGER=${VL_PACKAGER:-YOURNAME} in this script.
   Change the word "YOURNAME" to your VectorLinux packager name.
   You may also export VL_PACKAGER, or call this script with
   VL_PACKAGER="YOUR NAME HERE"'
   exit 1
fi


#CFLAGS SETUP
#--------------------------------------------
if [[ "$ARCH" = i?86 ]]; then
  ARCH=noarch
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  CONFIGURE_TRIPLET="i486-slackware-linux"
elif [ "$ARCH" = "x86_64" ]; then
  ARCH=noarch
  SLKCFLAGS="-O2 -fpic"
  CONFIGURE_TRIPLET="x86_64-vlocity-linux"
elif [ "$ARCH" = "powerpc" ]; then
  ARCH=noarch
  SLKCFLAGS="-O2"
  CONFIGURE_TRIPLET="powerpc-vlocity-linux"
fi

export CFLAGS="$SLKCFLAGS $CFLAG_OPTIONS"
export CXXFLAGS=$CFLAGS
export LDFLAGS="$LDFLAGS $LDFLAG_OPTIONS"
#--------------------------------------------

#GET THE SOURCE
#--------------------------------------------
# we're just going to assume that, if you have one *.xpi you have 'em all, as there is no one tarball and there are likely to be new ones each time you build this. So, either start with all recently downloaded, or with none. Or, of course, if you just want to build one, download the src you want and place it in this directory.
if [ "$(ls ./* | grep xpi > /dev/null)$?" != "0" ] ; then
wget $LINK/*
fi
#--------------------------------------------

mkdir -p $TMP

# now package 'em:
cd $CWD
for i in *.xpi
do

LANGPACKEXTDIRNAME="$(unzip -q -c $i install.rdf | grep "langpack" | cut -d '"' -f 2)"
LANGPK="$(echo $i | sed "s/$VERSION//" | cut -d . -f 2 | sed 's/-/_/')"
NAME="seamonkey_langpack_$LANGPK"
if [ $(unzip -q -c $i  install.rdf | grep "em:name" | cut -d '"' -f 2 | cut -d ')' -f 1 | grep "(" > /dev/null)$? = "0" ] ; then
DISPLAYLANG="$(unzip -q -c $i  install.rdf | grep "em:name" | cut -d '"' -f 2 | cut -d ')' -f 1 | sed 's/$/\)/')"
else 
DISPLAYLANG="$(unzip -q -c $i  install.rdf | grep "em:name" | cut -d '"' -f 2 | cut -d ' ' -f 1)"
fi
PKG=$TMP/package-$NAME
mkdir -p $RELEASEDIR/packages/$NAME
mkdir -p $PKG/usr/lib/seamonkey-$VERSION/extensions/
unzip -d $PKG/usr/lib/seamonkey-$VERSION/extensions/$LANGPACKEXTDIRNAME $i
### some searchplugins are useless duplicates which clog up the menulists; eliminate those but keep any others.
rm $PKG/usr/lib/seamonkey-$VERSION/extensions/$LANGPACKEXTDIRNAME/searchplugins/{google.*,dmoz.*,jeeves.*}
# and clean up empty searchplugins directories: quite a few supply nothing except duplicates of the defaults!
(
cd $PKG || exit 1
find . -type d -empty -exec rmdir {} \;
)
# and this should be enough to set perms:
# fix ownership and permissions, just in case
(
cd $PKG || exit 1
chown -R root:root .
find . -type f -exec chmod 644 {} \;
find . -type d -exec chmod 0755 {} \;
)
#----------------------------------------------------------------------
mkdir -p $PKG/usr/doc/$NAME-$VERSION
cat $CWD/seamonkey_langpacks.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/seamonkey_langpacks.SlackBuild

mkdir -p $PKG/install
# This creates the white space in front of "handy-ruler" in slack-desc below.
LENGTH=$(expr length "$NAME")
SPACES=0
SHIM=""
until [ "$SPACES" = "$LENGTH" ]; do
SHIM="$SHIM "
let SPACES=$SPACES+1
done

# Fill in the package summary between the () below.
# Then package the description, License, Author and Website.
# There may be no more then 11 $NAME: lines in a valid slack-desc.

cat > $RELEASEDIR/packages/$NAME/slack-desc << EOF
# HOW TO EDIT THIS FILE:
# The "handy ruler" below makes it easier to edit a package description.  Line
# up the first '|' above the ':' following the base package name, and the '|'
# on the right side marks the last column you can put a character in.  You must
# make exactly 11 lines for the formatting to be correct.  It's also
# customary to leave one space after the ':'.

$SHIM|-----handy-ruler------------------------------------------------------|
$NAME: $NAME ($DISPLAYLANG language pack for SeaMonkey)
$NAME:
$NAME: This is the $LANGPK ($DISPLAYLANG) language pack
$NAME: for SeaMonkey, to provide the $DISPLAYLANG language
$NAME: for the user interface. It also includes some other localisations.
$NAME:
$NAME: License: MPL 1.1/GPL 2.0/LGPL 2.1
$NAME: Authors: The SeaMonkey Project and contributors
$NAME: Website: http://www.seamonkey-project.org/

EOF

cat >> $RELEASEDIR/packages/$NAME/slack-desc << EOF

#----------------------------------------
BUILDDATE: $(date)
PACKAGER:  $VL_PACKAGER
HOST:      $(uname -srm)
DISTRO:    $(cat /etc/vector-version)
CFLAGS:    $CFLAGS
LDFLAGS:   $LDFLAGS
CONFIGURE: $(awk "/\.\/configure\ /" $TMP/$NAME-$VERSION/config.log)

EOF

cat $RELEASEDIR/packages/$NAME/slack-desc > $PKG/install/slack-desc
cat >> $PKG/install/slack-required  << EOF
seamonkey >= $VERSION-i586-1

EOF
#FINISH PACKAGE
#--------------------------------------------------------------
cd $PKG
echo "NOT Finding dependencies ... doing that manually"
# ADD="$ADDRB" EXCLUDE="$EXRB" requiredbuilder -v -y -s $RELEASEDIR $PKG
# nope, that's not necessary; just use SeaMonkey $VERSION as the sole dependency. After all, we're just talking SM lang packs here ...
cat > $RELEASEDIR/packages/$NAME/slack-required  << EOF
seamonkey >= $VERSION-i586-1

EOF
echo "Creating package $NAME-$VERSION-$ARCH-$BUILD.tlz"
makepkg -l y -c n $RELEASEDIR/packages/$NAME/$NAME-$VERSION-$ARCH-$BUILD.txz

cd $CWD
#echo "Cleaning up temp files..." && rm -rf $TMP
echo "Package Complete"
done


#--------------------------------------------------------------

# vim: set tabstop=4 shiftwidth=4 foldmethod=marker : ##