#!/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 build service command line client
#
# syntax: release-windows-packages <mode>
#
# run ./release-windows-packages to see all modes
#

# abort on errors
set -e

self=$(realpath $0)
which 7z >/dev/null 2>&1
if test $? -ne 0; then
    echo "7z not found, run 'zypper install p7zip'"
    exit 1
fi
ROOT=
ROOT32=${ROOT}windows\:mingw\:win32
ROOT64=${ROOT}windows\:mingw\:win64
SRCROOT32=windows\:mingw\:win32
SRCROOT64=windows\:mingw\:win64
REPO=openSUSE_Leap_42.2
SRCREPO=openSUSE_Leap_42.2
VERSION=2.22.1

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

echo "running mode $1"

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

download) ## download rpm packages
        cd work
        rm -rf binaries
        osc $oscoptions getbinaries $ROOT32 mingw32-umbrello-installer $REPO x86_64
        osc $oscoptions getbinaries $ROOT64 mingw64-umbrello-installer $REPO x86_64
        cd ..
        $self downloadsrc
        ;;

downloadsrc) ## download source
        cd work
        # fetch source package
        src32pkg=$(osc $oscoptions ls -b -r $SRCREPO -a x86_64 $SRCROOT32 mingw32-umbrello | grep src)
        osc $oscoptions getbinaries --sources $SRCROOT32 mingw32-umbrello $SRCREPO x86_64 $src32pkg
        # we only need once source package
        #src64pkg=$(osc $oscoptions ls -b -r $SRCREPO -a x86_64 $SRCROOT64 mingw64-umbrello | grep src)
        #osc $oscoptions getbinaries --sources $SRCROOT64 mingw64-umbrello $SRCREPO x86_64 $src64pkg
        # fetch debug packages
        debug32pkg=$(osc $oscoptions ls -b -r $SRCREPO -a x86_64 $SRCROOT32 mingw32-umbrello | grep debug)
        osc $oscoptions getbinaries $SRCROOT32 mingw32-umbrello $SRCREPO x86_64 $debug32pkg
        debug64pkg=$(osc $oscoptions ls -b -r $SRCREPO -a x86_64 $SRCROOT64 mingw64-umbrello | grep debug)
        osc $oscoptions getbinaries $SRCROOT64 mingw64-umbrello $SRCREPO x86_64 $debug64pkg
        ;;

unpack) ## unpack rpm files
        cd work
        files=$(find binaries -name *installer* -o -name *portable* -o -name *src* -o -name *debug* | 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
        ;;

movepackage) ## move windows binary packages into upload folder
        cd work
        rm -rf out
        mkdir -p out
        find tmp/ -type f -name '*.exe' -exec cp {} out \;
        find tmp/ -type f -name '*.7z' -exec cp {} out \;
        ;;

packdebug) ## package debug package
        cd work
        rm -rf out/*debug*.7z
        arch=mingw32
        version=$(find binaries/ -name *$arch*debug* | sed 's,.*debug-,,g;s,\.noarch.*,,g')
        if test -z "$version"; then
                echo no version found
                exit 1;
        fi
        dir=tmp/usr/i686-w64-mingw32/sys-root/mingw
        outfile=$PWD/out/umbrello-i686-w64-mingw32-$version-debug.7z
        (cd $dir; 7z a -r -mx=9 $outfile *.debug *.sym)

        arch=mingw64
        version=$(find binaries/ -name *$arch*debug* | sed 's,.*debug-,,g;s,\.noarch.*,,g')
        dir=tmp/usr/x86_64-w64-mingw32/sys-root/mingw
        outfile=$PWD/out/umbrello-x86_64-w64-mingw32-$version-debug.7z
        (cd $dir; 7z a -r -mx=9 $outfile *.debug *.sym)
        ;;

repacksource) ## repackage source tar ball to 7z
        # 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)
        ;;

createsha) ## create sha256sums
        (cd work/out; find -type f -name '*.7z' -o -name '*.exe' | sed 's,\./,,g' | sort | xargs sha256sum > umbrello.sha256sum)
        ;;

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

createdescription) ## create ticket description
        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 | gawk 'BEGIN { print "dir   shasum                                                            file"}  $2 ~ /i686/ { print "win32 " $0 } $2 ~ /x86_64/ { print "win64 " $0 } $2 ~ /umbrello-[0-9]/ { print "src   " $0 }')
        echo -e "$description\n\n$sums"
        ;;

ticket) ## submit phabricator ticket
        description=$($0 createdescription)
        curl $PHABURL/api/maniphest.createtask \
        -d api.token=$apitoken \
        -d "title=tarball move request for stable/umbrello/$VERSION" \
        -d "description=$description" \
        -d "$options"
        ;;

all) ## run all required targets
        $self clean
        $self download
        $self unpack
        $self movepackage
        $self repacksource
        $self packdebug
        $self createsha
        $self upload
        echo "Content for ticket creating:"
        $self createdescription
        echo run "$self ticket" to submit ticket
        ;;

*)
        echo "Make sure to setup VERSION inside $0 and run"
        echo "$0 all"
        echo
        echo "or run single targets"
        echo
        gawk '$0 ~ /^[a-z].*) ##/ { sub(/) ##/,"",$0); a = $1; $1 = ""; printf("    %-20s  - %s\n",a, $0); }' $0
        ;;
esac

exit 0
