#!/bin/sh

# Build script for icecat-l10n-ko
# Author: Ejis <ejis@mailoo.org>

# Homepage: http://gnuzilla.gnu.org/download/langpacks


set -e


LANG=ko
SOFTNAME=icecat-l10n-$LANG
VERSION=3.6.15
ZENVER=70
BUILD=1

CATEGORY=extra/locale

MARCH=noarch
FLAGS="-O2 -march=$MARCH -mtune=i686"

DOTNEW=1

CWD=$(pwd)

PKG=$SOFTNAME-$VERSION-$MARCH-$ZENVER.$BUILD

SOFTDIR=$CWD/$SOFTNAME-$VERSION
PKGDIR=$CWD/$PKG

PKGFILE=$PKGDIR.txz
MD5=$PKGDIR.md5
SRC=$PKGDIR.src
DEP=$PKGDIR.dep

SRCFILES=(
	$LANG.xpi
)

HOSTURL=http://zenwalk.pinguix.com/user-accounts/ejis/$CATEGORY/$SOFTNAME/$ZENVER
SRCURLS=(
	http://gnuzilla.gnu.org/download/langpacks/$VERSION
)

SRCLINKS=(
	${SRCURLS[0]}/${SRCFILES[0]}
)

DOC=(
)

EXTRADEPS=(
	icecat
)

LESSDEPS=(
)


# Check for source files and download them if necessary

if [ ! -f ${SRCFILES[0]} ]; then
	wget -c -t 0 -T 50 -w 0 ${SRCLINKS[0]}
fi


# Erase the previous build files

rm -rf $PKGFILE $SOFTDIR $PKGDIR $MD5 $SRC $DEP
mkdir -p $PKG


# Build

echo ""
echo "-----Building-----"
echo ""

mkdir -pv $PKGDIR/usr/lib/icecat/chrome

cd $PKGDIR/usr/lib/icecat/chrome
unzip $CWD/${SRCFILES[0]}

mv chrome/$LANG.jar .
sed -i "s:chrome/::g" chrome.manifest
mv -v chrome.manifest $LANG.manifest

rm -rvf chrome locale install.rdf


# Packaging

echo ""
echo "-----Packaging-----"
echo ""

cd $PKGDIR

mkdir -p $PKGDIR/install
mkdir -p $PKGDIR/usr/src/$SOFTNAME-$VERSION
mkdir -p $PKGDIR/usr/doc/$SOFTNAME-$VERSION


	       #|-----handy-ruler------------------------------------------------------|
cat > $PKGDIR/install/slack-desc <<EOF
$SOFTNAME: $SOFTNAME - IceCat Korean language
$SOFTNAME:
$SOFTNAME: Korean language package for IceCat.
$SOFTNAME: 
$SOFTNAME: 
$SOFTNAME: 
$SOFTNAME: 
$SOFTNAME: 
$SOFTNAME: 
$SOFTNAME: 
$SOFTNAME: 
EOF


# Redundent

cp -v $CWD/build-$SOFTNAME.sh \
	$PKGDIR/usr/src/$SOFTNAME-$VERSION/


# fix permissions and ownership
fakeroot chown -R root:root .
find . -perm 777 -exec fakeroot chmod 755 {} \;
find . -perm 775 -exec fakeroot chmod 755 {} \;
find . -perm 666 -exec fakeroot chmod 644 {} \;
find . -perm 664 -exec fakeroot chmod 644 {} \;
find . -perm 600 -exec fakeroot chmod 644 {} \;
find . -perm 444 -exec fakeroot chmod 644 {} \;
find . -perm 400 -exec fakeroot chmod 644 {} \;
find . -perm 440 -exec fakeroot chmod 644 {} \;
find . -perm 511 -exec fakeroot chmod 755 {} \;
find . -perm 711 -exec fakeroot chmod 755 {} \;
find . -perm 555 -exec fakeroot chmod 755 {} \;


fakeroot /sbin/makepkg -l y -c n $PKGFILE


cd $CWD

echo 'Creating .md5 file'
md5sum $PKG.txz > $MD5

echo 'Creating .src file'
echo "$HOSTURL/build-$SOFTNAME.sh" > $SRC
for link in ${SRCLINKS[@]}; do
	echo $link >> $SRC
done

echo 'Checking for dependencies'
deps=$(depfinder $PKGFILE)

for dep in ${EXTRADEPS[@]}; do
	deps="$deps,$dep"
done

for dep in ${LESSDEPS[@]}; do
	deps=${deps/$dep/}
done

order=$(echo ${deps//,/ } | sort -d)
echo ${order// /,} > $DEP

echo "Removing build directories"
rm -rf $SOFTDIR $PKG
