#!/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
#
NAME=umbrello

PACKAGENAME32=mingw32-$NAME
ROOT32=windows\:mingw\:win32
SRCROOT32=${ROOT32}
ARCHOUT32=i686-w64-mingw32

PACKAGENAME64=mingw64-$NAME
ROOT64=windows\:mingw\:win64
SRCROOT64=${ROOT64}
ARCHOUT64=x86_64-w64-mingw32

REPO=openSUSE_Leap_42.3
SRCREPO=$REPO

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

# abort on errors
set -e

echo2() { printf "%s\n" "$*" >&2; }

which 7z >/dev/null 2>&1
if test $? -ne 0; then
    echo "7z not found, run 'zypper install p7zip'"
    exit 1
fi

self=$(realpath $0)

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

echo2 "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 $PACKAGENAME32:$PACKAGENAME32-installer $REPO x86_64
        if test -n "$ROOT64"; then
            osc $oscoptions getbinaries $ROOT64 $PACKAGENAME64:$PACKAGENAME64-installer $REPO x86_64
        fi
        cd ..
        $self downloadsrc
        $self fetchversion
        touch work/$1.finished
        ;;

fetchversion)
        cd work
        VERSION=$(find binaries/ -name "*$PACKAGENAME32-installer*" | sed "s,^.*$PACKAGENAME32-installer-,,g;s,-.*$,,g")
        echo $VERSION > VERSION
        touch $1.finished
        ;;

downloadsrc) ## download source
        cd work
        # fetch source package
        src32pkg=$(osc $oscoptions ls -b -r $SRCREPO -a x86_64 $SRCROOT32 $PACKAGENAME32 | grep src)
        osc $oscoptions getbinaries --sources $SRCROOT32 $PACKAGENAME32 $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 $PACKAGENAME32 | grep debug)
        osc $oscoptions getbinaries $SRCROOT32 $PACKAGENAME32 $SRCREPO x86_64 $debug32pkg
        if test -n "$ROOT64"; then
            debug64pkg=$(osc $oscoptions ls -b -r $SRCREPO -a x86_64 $SRCROOT64 $PACKAGENAME64 | grep debug)
            osc $oscoptions getbinaries $SRCROOT64 $PACKAGENAME64 $SRCREPO x86_64 $debug64pkg
        fi
        touch $1.finished
        ;;

unpack) ## unpack rpm files
        if ! test -f work/VERSION; then
            echo "no version found"
            exit 1;
        fi
        VERSION=$(cat work/VERSION)
        cd work
        files=$(cd binaries; find -name '*installer*' -o -name '*portable*' -o -name '*src*' -o -name '*debugpackage*' | grep "$VERSION" | sed 's,^.,binaries,g')
        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
        touch $1.finished
        ;;

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 \;
        touch $1.finished
        ;;

repacksource) ## repackage source tar ball to 7z
        # repackage source package
        srcfile=$(find work/tmp -name "$NAME-[0-9]*.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)
        touch work/$1.finished
        ;;

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

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
        touch work/$1.finished
        ;;

createdescription) ## create ticket description
        if ! test -f work/VERSION; then
            echo "no version found"
            exit 1;
        fi
        VERSION=$(cat work/VERSION)
        description="Please move the $NAME related files which has been uploaded to upload.kde.org/incoming to download mirror 'stable/$NAME/$VERSION' location and please update the symbolic link 'stable/$NAME/latest' to 'stable/$NAME/$VERSION'"
        sums=$(cat work/out/$NAME.sha256sum | gawk 'BEGIN { print "dir   shasum                                                            file"}  $2 ~ /i686/ { print "win32 " $0 } $2 ~ /x86_64/ { print "win64 " $0 } $2 ~ /[a-z]+-[0-9]/ { print "src   " $0 }')
        echo -e "$description\n\n$sums"
        touch work/$1.finished
        ;;

ticket) ## submit phabricator ticket
        description=$($0 createdescription)
        if ! test -f work/createdescription.finished; then
            echo "no description found"
            exit 1;
        fi
        VERSION=$(cat work/VERSION)
        curl $PHABURL/api/maniphest.createtask \
        -d api.token=$apitoken \
        -d "title=tarball move request for stable/$NAME/$VERSION" \
        -d "description=$description" \
        -d "$options"
        touch work/$1.finished
        ;;

sf) ## run all required targets for releasing on sourceforge
        $self clean
        $self download
        $self unpack
        $self movepackage
        $self repacksource
        $self createsha
        echo "All release related files are located in work/out"
        ls work/out
        touch work/$1.finished
        ;;

kde) ## run all required targets for releasing on download.kde.org
        $self clean
        $self download
        $self unpack
        $self movepackage
        $self repacksource
        $self createsha
        $self upload
        echo2 "Content for ticket creating:"
        $self createdescription
        echo2 run "$self ticket" to submit ticket
        touch work/$1.finished
        ;;

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

exit 0
