summaryrefslogtreecommitdiff
path: root/pkgkde-vcs
blob: 96269b74cbfbcec320d4a5f7b247110226383cbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/bin/sh

# Copyright (C) 2009 Modestas Vainius <modax@debian.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>
# Tag debian package release

info()
{
    echo "pkgkde-vcs: $*"
}

die()
{
    info "fatal: $*" >&2
    exit 1
}

# Determine VCS type of the repository in the current directory and echo
# its type to standard output.
# Supports Git at the moment
determine_vcs()
{
    # VCS might have been requested via program name
    local vcs
    vcs="`basename "$0"`"
    vcs="${vcs##pkgkde-}"
    case "$vcs" in
        vcs)
            if which git > /dev/null && git rev-parse --git-dir >/dev/null 2>/dev/null; then
                echo git
                return 0
            fi
            return 1
            ;;
        git)
            if which git > /dev/null; then
                echo "$vcs"
                return 0
            fi
            return 1
            ;;
        *)
            return 1
            ;;
    esac
}

# Determine LIBDIR. Supports both running in-source and installed
# version.
determine_libdir()
{
    local libdir
    libdir=`dirname $0 2>/dev/null`
    if [ -n "$libdir" ] && [ -d "$libdir/vcslib" ]; then
        echo "$libdir/vcslib"
        return 0
    else
        libdir="/usr/share/pkg-kde-tools/vcs"
        if [ -d "$libdir" ]; then
            echo "$libdir"
            return 0
        fi
    fi
    return 1
}

# Run the command prompting for confirmation if necessary
runcmd()
{
    local ans exitval
    if [ -n "$OPT_VERBOSE" ] || [ "$OPT_PROMPT" = "prompt" ]; then
        echo "$" "$*" >&2
    fi
    if [ "$OPT_PROMPT" = "prompt" ]; then
        echo -n "Run the command above and all others? (p = prompt next time) [Y/n/p]: "
        while true; do
            read ans
            if [ "$ans" = "Y" ] || [ "$ans" = "y" ] || [ -z "$ans" ]; then
                OPT_PROMPT="y"
                break
            elif [ "$ans" = "p" ] || [ "$ans" = "P" ]; then
                break;
            elif [ "$ans" = "N" ] || [ "$ans" = "n" ]; then
                info "Cancelled by user." >&2
                exit 2
            else
                echo -n "Enter Y (default) or N: "
            fi
        done
    fi

    if [ "$OPT_PROMPT" = "n" ]; then
        return 0
    fi

    # Run the command
    "$@"
    exitval="$?"
    if [ "$exitval" -eq "0" ]; then
        return 0
    else
        info "Command failed with $exitval" >&2
        exit $exitval
    fi
}

_get_deb_field()
{
    cat | grep "^$1:" | sed "s/^$1:[[:space:]]*\(.*\)\$/\1/"
}

# Given path to the package root (where debian/ subdirectory is), set some
# informational variables (DEB_* namespace) about the package
get_debian_package_info()
{
    local rootdir _pchangelog
    rootdir="$1"

    if [ -f "$rootdir/debian/changelog" ]; then
        _pchangelog="$(dpkg-parsechangelog "-l$rootdir/debian/changelog")"
        DEB_VERSION="$(echo "$_pchangelog" | _get_deb_field 'Version')"
        if [ -z "$DEB_VERSION" ]; then
            die "unable to determine package version."
        fi
        DEB_SOURCE="$(echo "$_pchangelog" | _get_deb_field 'Source')"
        DEB_DISTRIBUTION="$(echo "$_pchangelog" | _get_deb_field 'Distribution')"
        DEB_URGENCY="$(echo "$_pchangelog" | _get_deb_field 'Urgency')"
    else
        die "debian/changelog could not be found in $rootdir"
    fi
}

is_valid_package_root()
{
    test -f "$1/debian/changelog" && test -f "$1/debian/control"
}

is_distribution_valid()
{
    local distro
    test -n "$1" || distro="$DEB_DISTRIBUTION"
    if [ "$(expr match "$distro" '^\(\(wheezy\|jessie\|stretch\)\(-security\|-backports\|-backports-sloppy\)\?\)$')" = "$distro" ]; then
        return 0
    elif [ "$(expr match "$distro" '^\(testing\(-proposed-updates\)\?\|unstable\|experimental\)$')" = "$distro" ]; then
        return 0
    fi
    return 1
}

opt_till_double_dash() {
    local name
    # Parse out remaining -- if needed
    while getopts ":" name; do
        case "$name" in
            ?)  if [ -n "$OPTARG" ]; then OPTIND=$(($OPTIND-1)); fi; break;;
            :)  die "$OPTARG option is missing a required argument" ;;
        esac
    done
    if [ "$OPTIND" -gt 1 ]; then
        echo "$(($OPTIND-1))"
    else
        echo "0"
    fi
}

# Parse common options
OPT_VERBOSE=
OPT_PROMPT='prompt'

while getopts ":vyn" name; do
    case "$name" in
        v)  OPT_VERBOSE=1 ;;
        y)  OPT_PROMPT='y' ;;
        n)  OPT_PROMPT='n' ;;
        ?)  if [ -n "$OPTARG" ]; then OPTIND=$(($OPTIND-1)); fi; break;;
        :)  die "$OPTARG option is missing a required argument" ;;
    esac
done

if [ "$OPTIND" -gt 1 ]; then
    shift "$(($OPTIND-1))"
    OPTIND=1
fi

# Determine VCS type and source its library script

VCS=$(determine_vcs)
case "$VCS" in
    "")
        die "unable to determine VCS type or VCS tools (git, svn) are not installed."
        ;;
    *)
        # Load VCS script file. Determines LIBDIR in the process
        if LIBDIR=$(determine_libdir) && [ $? -eq 0 ]; then
            if [ -f "$LIBDIR/$VCS.sh" ]; then
                . "$LIBDIR/$VCS.sh"
            else
                die "$VCS VCS is NOT supported (via $LIBDIR)."
            fi
        else
            die "unable to determine pkgkde-vcs library directory."
        fi
        ;;
esac

exit 0