#!/bin/sh
# This file is part of Checkbox.
#
# Copyright 2014 Canonical Ltd.
# Written by:
#   Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# Checkbox is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.

# Helper script to tags related to bug LP: #1319297
# =================================================
# https://bugs.launchpad.net/checkbox/+bug/1319297

get_affected_tags() {
    # NOTE: the following tags are also invalid in some way (to git) but are not
    # removed as they require more investigation.
    # checkbox-v0.17.6c1
    # refs/tags/0.10.1-1~ppa0
    # refs/tags/0.10.2-1~ppa0
    # refs/tags/0.10.4-3~ppa0
    # refs/tags/0.9-30~ppa0
    # refs/tags/0.9.2-10~ppa0
    # refs/tags/0.9.2-8~ppa0

    # A list of tags to remove. Created by applying the following vim
    # substitution on the output of git-fastimport. The resulting list was
    # sorted.
    #
    # :'<,'>s!^[^']\+'\([^']\+\)'.\+$!\1!
    cat << __TAGS__
0.1
0.10
0.10-10~ppa0
0.10-11~ppa0
0.10-12~ppa0
0.10-13~ppa0
0.10-14~ppa0
0.10-15~ppa0
0.10-1~ppa0
0.10-2~ppa0
0.10-3~ppa0
0.10-4~ppa0
0.10-5~ppa0
0.10-6~ppa0
0.10-7~ppa0
0.10-8~ppa0
0.10-9~ppa0
0.10.1
0.10.1-2~ppa0
0.10.1-3~ppa0
0.10.2
0.10.2-2~ppa0
0.10.2-3~ppa0
0.10.2ubuntu0~oem1
0.10.3
0.10.3ubuntu1
0.10.4-1~ppa0
0.10.4-2~ppa0
0.10.4-4~ppa0
0.12.9
0.13.1~ppa0
0.13.1~ppa17.12.04
0.17.4
0.2
0.2-ppa1
0.3-ppa1
0.3-ppa10
0.3-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0
0.9-44~ppa0
0.9-45~ppa0
0.9-46~ppa0
0.9-47~ppa0
0.9-48~ppa0
0.9-49~ppa0
0.9-50~ppa0
0.9-51~ppa0
0.9-52~ppa0
0.9-53~ppa0
0.9-54~ppa0
0.9-55~ppa0
0.9-56~ppa0
0.9-57~ppa0
0.9-58~ppa0
0.9-59~ppa0
0.9-60~ppa0
0.9-61~ppa0
0.9-62~ppa0
0.9-63~ppa0
0.9-64~ppa0
0.9-65~ppa0
0.9-66~ppa0
0.9-67~ppa0
0.9-68~ppa0
0.9-69~ppa0
0.9-70~ppa0
0.9-71~ppa0
0.9-72~ppa0
0.9-73~ppa0
0.9-74~ppa0
0.9-ppa1
0.9-ppa10
0.9-ppa11
0.9-ppa12
0.9-ppa13
0.9-ppa14
0.9-ppa15
0.9-ppa16
0.9-ppa17
0.9-ppa18
0.9-ppa19
0.9-ppa2
0.9-ppa20
0.9-ppa21
0.9-ppa22
0.9-ppa23
0.9-ppa24
0.9-ppa25
0.9-ppa27
0.9-ppa3
0.9-ppa4
0.9-ppa42
0.9-ppa5
0.9-ppa6
0.9-ppa7
0.9-ppa8
0.9-ppa9
0.9.1
0.9.2-1~ppa0
0.9.2-2~ppa0
0.9.2-3~ppa0
0.9.2-4~ppa0
0.9.2-5~ppa0
0.9.2-6~ppa0
0.9.2-7~ppa0
__TAGS__
}

# Find the top of checkbox tree
if [ "$CHECKBOX_TOP" = "" ]; then
    CHECKBOX_TOP="$(git rev-parse --show-toplevel 2>/dev/null)"
fi
if [ "$CHECKBOX_TOP" = "" ]; then
    CHECKBOX_TOP="$(bzr root)"
fi

# Remove all the bzr tags from pure bzr checkout
if [ -d "$CHECKBOX_TOP/.bzr" ]; then
    echo "Removing tags from bzr"
    for tag in $(get_affected_tags); do
        bzr tag --delete "$tag" --directory "$CHECKBOX_TOP" 
    done
fi

# Remove git tags (there should be none)
if [ -d "$CHECKBOX_TOP/.git/" ]; then
    echo "Removing tags from git"
    for tag in $(get_affected_tags); do
        git tag --delete "$tag"
    done
fi

# Remove bzr tags from all the embedded bzr branches
if [ -d "$CHECKBOX_TOP/.git/launchpad/repo/" ]; then
    for branch in $(ls $CHECKBOX_TOP/.git/launchpad/repo); do
        echo "Removing tags from git-lp branch $branch"
        for tag in $(get_affected_tags); do
            bzr tag --delete "$tag" --directory "$CHECKBOX_TOP/.git/launchpad/repo/$branch" 
        done
    done
fi

if [ "$1" = "--remove-from-repo" ] && [ -n "$2" ]; then
    echo "Removing tags from $2"
    for tag in $(get_affected_tags); do
        bzr tag --delete "$tag" --directory "$2"
    done
fi
