#!/bin/sh
#
# unpack windows rpm's from opensuse download server, upload files to kde.org and file a related release ticket
#
# Author: Ralf Habacker <ralf.habacker@freenet.de>
#
# requirements:
#
#  osc       - opensuse command line client
#
# syntax: release-windows-packages <mode>
#
# where mode is:
#
# clean      - clean working area
# fetch      - fetch file lists from opensuse download server
# download   - download rpm packages
# unpack     - unpack rpm files
# repack     - repackage source tar ball to 7z
# upload     - upload files to ftp:/upload.kde.org
# ticket     - file a tar ball move request
# all        - performs all above mentioned steps
#
REPO=openSUSE_42.2
VERSION=2.20.90

PHABURL=https://phabricator.kde.org
oscoptions="-A https://api.opensuse.org"
apitoken=cli-uxo23l4q5qrzoyscbz5kp4zcngqp
options='projectPHIDs[]=PHID-PROJ-3qa4tomwgrmcmp4ym2ow'

if ! test -d "work"; then
    mkdir work
fi

case $1 in
clean)
        rm -rf work/*
        ;;

download)
        cd work
        rm -rf binaries
        osc $oscoptions getbinaries windows\:mingw:\win32 mingw32-umbrello-installer $REPO x86_64
        osc $oscoptions getbinaries windows\:mingw:\win64 mingw64-umbrello-installer $REPO x86_64
        # fetch source package
        src32pkg=$(osc $oscoptions ls -b -r $REPO -a x86_64 windows:mingw:win32 mingw32-umbrello | grep src)
        osc $oscoptions getbinaries --sources windows\:mingw:\win32 mingw32-umbrello $REPO x86_64 $src32pkg
        # we only need once source package
        #src64pkg=$(osc $oscoptions ls -b -r $REPO -a x86_64 windows:mingw:win64 mingw64-umbrello | grep src)
        #osc $oscoptions getbinaries --sources windows\:mingw:\win32 mingw64-umbrello $REPO x86_64 $src64pkg
        # fetch debug packages
        #debug32pkg=$(osc $oscoptions ls -b -r $REPO -a x86_64 windows:mingw:win32 mingw32-umbrello | grep debug)
        #osc $oscoptions getbinaries windows\:mingw:\win32 mingw32-umbrello $REPO x86_64 $debug32pkg
        #debug64pkg=$(osc $oscoptions ls -b -r $REPO -a x86_64 windows:mingw:win64 mingw64-umbrello | grep debug)
        #osc $oscoptions getbinaries windows\:mingw:\win64 mingw64-umbrello $REPO x86_64 $debug64pkg
        ;;

unpack)
        cd work
        files=$(find binaries -name *installer* -o -name *portable* -o -name *src* | grep "$VERSION")
        if test -d tmp; then
                rm -rf tmp
        fi
        mkdir -p tmp
        for i in $(echo $files); do
                (cd tmp; rpm2cpio ../$i | cpio -idmv)
        done
        # move binary packages
        rm -rf out
        mkdir -p out
        find tmp/ -name '*.exe' -exec mv {} out \;
        find tmp/ -name '*.7z' -exec mv {} out \;
        ;;

repack)
        # repackage source package
        srcfile=$(find work/tmp -name '*.xz')
        outfile=$(basename $srcfile | sed 's,\.tar\.xz,\.7z,g')
        (mkdir -p work/srctmp; cd work/srctmp; tar -xJf ../../$srcfile; 7za a ../out/$outfile *; cd ..; rm -rf srctmp)
        # create sha256sums
        (cd work/out; sha256sum  *.7z *.exe > umbrello.sha256sum)
        ;;

upload)
        # upload
        for i in $(find work/out -name '*.7z' -o -name '*.exe'); do
                curl -T $i  ftp://upload.kde.org/incoming/
        done
        ;;

ticket)
        description="Please move the umbrello related files which has been uploaded to upload.kde.org/incoming to download mirror 'stable/umbrello/$VERSION' location and please update the symbolic link 'stable/umbrello/latest' to 'stable/umbrello/$VERSION'"
        sums=$(cat work/out/umbrello.sha256sum | sort -n | gawk 'BEGIN { print "dir   shasum                                                            file"}  $2 ~ /i686/ { print "win32 " $0 } $2 ~ /x86_64/ { print "win64 " $0 } $2 ~ /umbrello-[0-9]/ { print "src   " $0 }')
        description=$(echo -e "$description\n\n$sums")
        curl $PHABURL/api/maniphest.createtask \
        -d api.token=$apitoken \
        -d "title=tarball move request for stable/umbrello/$VERSION" \
        -d "description=$description" \
        -d "$options"
        ;;
all)
        $0 clean
        $0 download
        $0 unpack
        $0 repack
        $0 upload
        $0 ticket
        ;;

*)
        echo "Make sure to setup VERSION inside $0 and run"
        echo "$0 all"
        ;;
esac

exit 0
