#!/bin/bash -e
#
# Instead of a quilt workflow, we use a git tree that contains
# all the commits on top of a stable tarball.
#
# When updating this package, just either update the git tree
# below (use rebase!) or change the tree path and use your own
#
# That way we can easily rebase against the next stable release
# when it comes.

GIT_TREE=https://gitlab.suse.de/virtualization/qemu.git
GIT_LOCAL_TREE=~/git/qemu
GIT_UPSTREAM_TREE=git://git.qemu-project.org/qemu.git
GIT_BRANCH=SLE11-SP1
GIT_UPSTREAM_TAG=v0.12.5-qemu-kvm
GIT_DIR=/dev/shm/qemu-sle11-git-dir
CMP_DIR=/dev/shm/qemu-sle11-cmp-dir

rm -rf $GIT_DIR
rm -rf $CMP_DIR

if [ -d "$GIT_LOCAL_TREE" ]; then
    echo "Processing $GIT_BRANCH branch of local git tree, using tag:" \
         "$GIT_UPSTREAM_TAG"
    if ! (cd $GIT_LOCAL_TREE && git show-branch $GIT_BRANCH &>/dev/null); then
        echo "Error: Branch $GIT_BRANCH not found - please create a remote" \
             "tracking branch of origin/$GIT_BRANCH"
        exit
    fi
    git clone -lsn $GIT_LOCAL_TREE $GIT_DIR -b $GIT_BRANCH
    if ! (cd $GIT_LOCAL_TREE && git remote show upstream &>/dev/null); then
        echo "Remote for upstream git tree not found. Next time add remote" \
             "named upstream for git://git.qemu.org/qemu.git and update"
        (cd $GIT_DIR && git remote add upstream $GIT_UPSTREAM_TREE)
    	(cd $GIT_DIR && git fetch -q upstream +$GIT_UPSTREAM_TAG)
        BASE=FETCH_HEAD
   else
        BASE=$GIT_UPSTREAM_TAG
   fi
else
    echo "Processing $GIT_BRANCH branch of remote git tree, using tag:" \
         "$GIT_UPSTREAM_TAG"
    echo "(For much faster processing, create a local git tree at $GIT_LOCAL_TREE)"
    git init -q $GIT_DIR
    (cd $GIT_DIR && git config --global core.compression 0)
    (cd $GIT_DIR && git remote add origin $GIT_TREE)
    (cd $GIT_DIR && git fetch -q origin $GIT_BRANCH)
    (cd $GIT_DIR && git remote add upstream $GIT_UPSTREAM_TREE)
    (cd $GIT_DIR && git fetch -q upstream +$GIT_UPSTREAM_TAG)
    BASE=FETCH_HEAD
fi
(cd $GIT_DIR && git format-patch -N --no-renames --start-number=1000 $BASE..origin/$GIT_BRANCH --suffix= -o $CMP_DIR >/dev/null)
QEMU_VERSION=`(cd $GIT_DIR && git show origin/$GIT_BRANCH:VERSION)`
echo "QEMU version: $QEMU_VERSION"

rm -rf $GIT_DIR

(
    CHANGED_COUNT=0
    UNCHANGED_COUNT=0
    DELETED_COUNT=0
    ADDED_COUNT=0

    shopt -s nullglob

# Process patches to eliminate useless differences: limit file names to 40 chars
# before extension and remove git signature. ('28' below gets us past dir prefix)
    for i in $CMP_DIR/*; do
        if grep -q "GIT binary patch" $i; then
            echo "WARNING: Removing binary patch ${i:28:40}.patch"
        else
            head -n -3 $i > $CMP_DIR/${i:28:40}.patch
        fi
        rm $i
    done

    for i in 1???-*.patch; do
        if [ -e $CMP_DIR/$i ]; then
            if cmp -s $CMP_DIR/$i $i; then
                rm $CMP_DIR/$i
                let UNCHANGED_COUNT+=1
            else
                mv $CMP_DIR/$i .
                let CHANGED_COUNT+=1
            fi
        else
            osc rm --force $i
            let DELETED_COUNT+=1
            echo "  ${i##*/}" >> kvm.changes.deleted
        fi
    done

    for i in $CMP_DIR/*; do
        mv $i .
        osc add ${i##*/}
        let ADDED_COUNT+=1
        echo "  ${i##*/}" >> kvm.changes.added
    done

    for package in kvm; do
        while IFS= read -r line; do
            if [ "$line" = "PATCH_FILES" ]; then
                for i in 1???-*.patch; do
                    NUM=${i%%-*}
                    echo -e "Patch$NUM:      $i"
                done
            elif [ "$line" = "PATCH_EXEC" ]; then
                for i in 1???-*.patch; do
                    NUM=${i%%-*}
                    echo "%patch$NUM -p1"
                done
            else
                echo "$line"
            fi
        done < $package.spec.in > $CMP_DIR/$package.spec
        if cmp -s $package.spec $CMP_DIR/$package.spec; then
            echo "$package.spec unchanged"
        else
            mv $CMP_DIR/$package.spec $package.spec
            echo "$package.spec regenerated"
        fi

        # All deleted and added patches are to be mentioned in change log
        if [ -e kvm.changes.deleted ] || [ -e kvm.changes.added ]; then
            echo "Patch queue updated from ${GIT_TREE} ${GIT_BRANCH}" > $package.changes.proposed
        fi
        if [ -e kvm.changes.deleted ]; then
            echo "* Patches dropped:" >> $package.changes.proposed
            cat kvm.changes.deleted  >> $package.changes.proposed
        fi
        if [ -e kvm.changes.added ]; then
            echo "* Patches added:" >> $package.changes.proposed
            cat kvm.changes.added  >> $package.changes.proposed
        fi
        if [ -e $package.changes.proposed ]; then
            osc vc --file=$package.changes.proposed $package
            rm -f $package.changes.proposed
        fi
    done
    if [ -e kvm.changes.deleted ]; then
        rm -f kvm.changes.deleted
    fi
    if [ -e kvm.changes.added ]; then
        rm -f kvm.changes.added
    fi
    echo "git patch summary"
    echo "  unchanged: $UNCHANGED_COUNT"
    echo "    changed: $CHANGED_COUNT"
    echo "    deleted: $DELETED_COUNT"
    echo "      added: $ADDED_COUNT"
)

rm -rf $CMP_DIR
