#! /bin/sh

nr="0"

# for i in `ls -1 *.tar.bz2 | sed -e "s,.tar.bz2,,"` ; do bunzip2 -c $i.tar.bz2 > $i.tar; tar xf $i.tar; find $i -name *.png | xargs -n1 optipng -o4; tar cf $i-new.tar $i; xdelta delta $i.tar $i-new.tar $i.xdelta; rm -rf $i; rm $i.tar $i-new.tar; done

cp kde4-l10n.spec.in kde4-l10n.spec

for lang in `ls -1 kde-l10n*tar.bz2 | sed -n 's,kde-l10n-\([^-]*\)-.*,\1,p'`; do
  languages="$languages $lang"
done

# if the tar is ~ >10MB - do not automate to avoid ping pong between dot releases
splitlanguages="de es it ru fr sv da ml nl pl pt_BR"
sed="s,@LANGUAGES@,$languages,"
sed="$sed; s,@SPLITLANGS@,$splitlanguages,"
sed -i -e "$sed" kde4-l10n.spec

exec 4>"kde4-l10n.spec.packages"
setup_line="-D -T"
for lang in $languages; do

 sed="s,#@SOURCES@,Source$nr:       kde-l10n-$lang-%version.tar.bz2\n#@SOURCES@," 
 setup_line="$setup_line -b $nr"
 snr=$nr
 nr=$(( $nr + 1 ))
 if test -e kde-l10n-$lang-*.xdelta; then
   dnr=$nr
   nr=$(( $nr + 1 )) 
   sed="$sed;s,#@SOURCES@,Source$dnr: kde-l10n-$lang-%version.xdelta\n#@SOURCES@," 
   sed="$sed;s,#@EXTRACT@,bunzip2 -c %{S:$snr} > $lang.tar; md5sum $lang.tar; xdelta patch %{S:$dnr} $lang.tar $lang-new.tar; tar xf $lang-new.tar; rm $lang.tar $lang-new.tar\n#@EXTRACT@,"
 fi

 if test -e kdepim-l10n-$lang-*.bz2; then
   pnr=$nr
   nr=$(( $nr + 1 )) 
   sed="$sed;s,#@SOURCES@,Source$pnr: kdepim-l10n-$lang-%{kdepim_version}.tar.bz2\n#@SOURCES@," 
   setup_line="$setup_line -b $pnr"
 fi

 plang=$lang
 if test "$lang" = "sr@Latn"; then
	lang=srLatn
	plang=sr
 fi
 
 splitpackage=
 for sl in $splitlanguages; do
   if test "$lang" = "$sl"; then splitpackage=true; fi
 done

 echo "%package $lang" >&4
 echo "License:        GPL v2 or later" >&4
 echo "Summary:        summary" >&4
 echo "Group:          System/Localization" >&4
 echo "Provides:       kde-l10n-$lang locale(kdelibs4:$plang)" >&4
 echo "Supplements:    packageand(kdelibs4:kde3-i18n-$lang)" >&4
 if test "$lang" = "nn"; then
   echo "Provides: kde4-l10n-no_NY" >&4
   echo "Obsoletes: kde4-l10n-no_NY\n" >&4
 fi
 echo "%description $lang" >&4
 echo "descr" >&4
 echo "" >&4

 if test $splitpackage; then
        echo "%package $lang-doc" >&4
        echo "Summary:        summary" >&4
        echo "License:        GPL v2 or later" >&4
        echo "Group:          System/Localization" >&4
        echo "Provides:       locale(kdelibs4:$plang)" >&4
        echo "Supplements:    packageand(kdelibs4:kde3-i18n-$lang)" >&4
        echo "" >&4
        echo "%description $lang-doc" >&4
        echo "descr" >&4
        echo "" >&4
        echo "%package $lang-data" >&4
        echo "Summary:        summary" >&4
        echo "License:        GPL v2 or later" >&4
        echo "Group:          System/Localization" >&4
        echo "Provides:       locale(kdelibs4:$plang)" >&4
        echo "Supplements:    packageand(kdelibs4:kde3-i18n-$lang)" >&4
        echo "%description $lang-data" >&4
        echo "descr" >&4
        echo "" >&4
 fi

 if test $splitpackage; then
        sed="$sed;s,#@FILES@,%files $lang -f files.$lang\n%files $lang-doc -f files.doc.$lang\n%files $lang-data -f files.data.$lang\n#@FILES@,"
 else
         sed="$sed;s,#@FILES@,%files $lang -f files.all.$lang\n#@FILES@,"
 fi

 sed -i -e "$sed" kde4-l10n.spec 
done

sed -i -e "s,@SETUP_LIST@,$setup_line," kde4-l10n.spec
exec 4<&-

sed -n -e '1,/PACKAGES/p' kde4-l10n.spec > spec.$$
cat kde4-l10n.spec.packages >> spec.$$
sed -n -e '/PACKAGES/,$p' kde4-l10n.spec >> spec.$$
perl update_spec.pl spec.$$ texts > kde4-l10n.spec
rm spec.$$
rm kde4-l10n.spec.packages
