#!/bin/bash

FULLPATH=`realpath -s $0`
TOPDIR=`dirname "$FULLPATH"`

VER='24.08'
SUBVER='.2'
OLDVER='17.08'
OLDSUBVER='.1'

build_a_package()
{
    N=$1
    cd "$TOPDIR"
    AD=`ls -d $N-*`
    FW=`echo $AD|sed 's/^'$N'\-//'`
    cd $AD
    #wget -c  http://download.kde.org/stable/plasma/${VER}/${FW}-${VER}${SUBVER}.tar.xz
    apt-get source ${FW}

    #cd ${FW}-${OLDVER}${OLDSUBVER}
    cd ${FW}-*/
    uscan
    uupdate ../*_${VER}${SUBVER}.orig.tar.xz
    cd ../${FW}-${VER}${SUBVER}
    #rm -rf ../*${OLDVER}${OLDSUBVER}*
#while dquilt push; do dquilt refresh; done

    sed -i 's/'${OLDVER}'/'${VER}'/g' debian/control
    sed -i 's/kf5/kf6/' debian/control
    sed -i 's/'${OLDVER}'/'${VER}'/g' debian/rules
    #dch -b --newversion=${VER}${SUBVER}-0.1
    sbuild -d sid-amd64-sbuild -A
    #dpkg-buildpackage -j6 --build=full 2>&1|tee ../log
    echo =====================================
    #pwd
    #echo ======== Check missing files ========
    #dh_install --fail-missing || exit 1
    #echo ======= Checked missing files =======
    cd ..
#    rm -rf *${OLDVER}${OLDSUBVER}*
    cd /home/wares/soft/deb-repo
    ./update.sh
    #sudo apt-get update
    #sudo apt-get upgrade
}

if [ $# -gt 0 ]
then
    NN=$@;
else
    NN=`seq -w 32 44`
fi

for N in $NN;
do
    build_a_package $N
done
