#! /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 kde3-i18n.spec.in kde3-i18n.spec

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

sed="s,@LANGUAGES@,$languages,"

for lang in $languages; do

 sed="$sed;s,#@SOURCES@,Source$nr: kde-i18n-$lang-%version.tar.bz2\n#@SOURCES@," 
 snr=$nr
 nr=$(( $nr + 1 ))
 if test -e kde-i18n-$lang-*.xdelta; then
   dnr=$nr
   nr=$(( $nr + 1 )) 
   sed="$sed;s,#@SOURCES@,Source$dnr: kde-i18n-$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@,"
 else
   sed="$sed;s,#@EXTRACT@,tar xf %{S:$snr}\n#@EXTRACT@,"
 fi

 plang=$lang
 if test "$lang" = "sr@Latn"; then
	lang=srLatn
	plang=sr
 fi
 if test "$lang" = "uz@cyrillic"; then
	lang=uzcyrillic
	plang=uz
 fi

 splitpackage=

 if test "$lang" = "de"; then
        splitpackage=true
 fi

 text="%package $lang\nSummary:$lang\nGroup:System/GUI/KDE\nProvides:kde-i18n-$lang locale(kdelibs3:$plang)\n"
 if test "$lang" = "nn"; then
   text="$text""Provides:kde3-i18n-no_NY\nObsoletes:kde3-i18n-no_NY\n"
 fi
 text="$text""%description $lang\n$lang\n"

 if test $splitpackage; then
        text="$text\n%package $lang-doc\nSummary:$lang manuals\nGroup:System/GUI/KDE\nProvides:locale(kdelibs3:$plang)\n%description $lang-doc\n$lang\n"
        text="$text%package $lang-data\nSummary:$lang application data\nGroup:System/GUI/KDE\nProvides:locale(kdelibs3:$plang)\n%description $lang-data\n$lang\n"
 fi

 sed="$sed;s,#@PACKAGES@,$text\n#@PACKAGES@,"

 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

done

sed -e "$sed" kde3-i18n.spec > kde3-i18n.spec.tmp && mv kde3-i18n.spec.tmp kde3-i18n.spec

# prepare_spec kde3-i18n.spec > kde3-i18n.spec.temp && mv kde3-i18n.spec.temp kde3-i18n.spec
