| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
# Copyright (C) 2009 Modestas Vainius <[email protected]> |
| 4 |
# |
| 5 |
# This program is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# This program is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with this program. If not, see <http://www.gnu.org/licenses/> |
| 17 |
# Tag debian package release |
| 18 |
|
| 19 |
info() |
| 20 |
{ |
| 21 |
echo "pkgkde-vcs: $*" |
| 22 |
} |
| 23 |
|
| 24 |
die() |
| 25 |
{ |
| 26 |
info "fatal: $*" >&2 |
| 27 |
exit 1 |
| 28 |
} |
| 29 |
|
| 30 |
# Determine VCS type of the repository in the current directory and echo |
| 31 |
# its type to standard output. |
| 32 |
# Supports Git at the moment |
| 33 |
determine_vcs() |
| 34 |
{ |
| 35 |
# VCS might have been requested via program name |
| 36 |
local vcs |
| 37 |
vcs="`basename "$0"`" |
| 38 |
vcs="${vcs##pkgkde-}" |
| 39 |
case "$vcs" in |
| 40 |
vcs) |
| 41 |
if which git > /dev/null && git rev-parse --git-dir >/dev/null 2>/dev/null; then |
| 42 |
echo git |
| 43 |
return 0 |
| 44 |
fi |
| 45 |
return 1 |
| 46 |
;; |
| 47 |
git) |
| 48 |
if which git > /dev/null; then |
| 49 |
echo "$vcs" |
| 50 |
return 0 |
| 51 |
fi |
| 52 |
return 1 |
| 53 |
;; |
| 54 |
*) |
| 55 |
return 1 |
| 56 |
;; |
| 57 |
esac |
| 58 |
} |
| 59 |
|
| 60 |
# Determine LIBDIR. Supports both running in-source and installed |
| 61 |
# version. |
| 62 |
determine_libdir() |
| 63 |
{ |
| 64 |
local libdir |
| 65 |
libdir=`dirname $0 2>/dev/null` |
| 66 |
if [ -n "$libdir" ] && [ -d "$libdir/vcslib" ]; then |
| 67 |
echo "$libdir/vcslib" |
| 68 |
return 0 |
| 69 |
else |
| 70 |
libdir="/usr/share/pkg-kde-tools/vcs" |
| 71 |
if [ -d "$libdir" ]; then |
| 72 |
echo "$libdir" |
| 73 |
return 0 |
| 74 |
fi |
| 75 |
fi |
| 76 |
return 1 |
| 77 |
} |
| 78 |
|
| 79 |
# Run the command prompting for confirmation if necessary |
| 80 |
runcmd() |
| 81 |
{ |
| 82 |
local ans exitval |
| 83 |
if [ -n "$OPT_VERBOSE" ] || [ "$OPT_PROMPT" = "prompt" ]; then |
| 84 |
echo "$" "$*" >&2 |
| 85 |
fi |
| 86 |
if [ "$OPT_PROMPT" = "prompt" ]; then |
| 87 |
echo -n "Run the command above and all others? (p = prompt next time) [Y/n/p]: " |
| 88 |
while true; do |
| 89 |
read ans |
| 90 |
if [ "$ans" = "Y" ] || [ "$ans" = "y" ] || [ -z "$ans" ]; then |
| 91 |
OPT_PROMPT="y" |
| 92 |
break |
| 93 |
elif [ "$ans" = "p" ] || [ "$ans" = "P" ]; then |
| 94 |
break; |
| 95 |
elif [ "$ans" = "N" ] || [ "$ans" = "n" ]; then |
| 96 |
info "Cancelled by user." >&2 |
| 97 |
exit 2 |
| 98 |
else |
| 99 |
echo -n "Enter Y (default) or N: " |
| 100 |
fi |
| 101 |
done |
| 102 |
fi |
| 103 |
|
| 104 |
if [ "$OPT_PROMPT" = "n" ]; then |
| 105 |
return 0 |
| 106 |
fi |
| 107 |
|
| 108 |
# Run the command |
| 109 |
"$@" |
| 110 |
exitval="$?" |
| 111 |
if [ "$exitval" -eq "0" ]; then |
| 112 |
return 0 |
| 113 |
else |
| 114 |
info "Command failed with $exitval" >&2 |
| 115 |
exit $exitval |
| 116 |
fi |
| 117 |
} |
| 118 |
|
| 119 |
_get_deb_field() |
| 120 |
{ |
| 121 |
cat | grep "^$1:" | sed "s/^$1:[[:space:]]*\(.*\)\$/\1/" |
| 122 |
} |
| 123 |
|
| 124 |
# Given path to the package root (where debian/ subdirectory is), set some |
| 125 |
# informational variables (DEB_* namespace) about the package |
| 126 |
get_debian_package_info() |
| 127 |
{ |
| 128 |
local rootdir _pchangelog |
| 129 |
rootdir="$1" |
| 130 |
|
| 131 |
if [ -f "$rootdir/debian/changelog" ]; then |
| 132 |
_pchangelog="$(dpkg-parsechangelog "-l$rootdir/debian/changelog")" |
| 133 |
DEB_VERSION="$(echo "$_pchangelog" | _get_deb_field 'Version')" |
| 134 |
if [ -z "$DEB_VERSION" ]; then |
| 135 |
die "unable to determine package version." |
| 136 |
fi |
| 137 |
DEB_SOURCE="$(echo "$_pchangelog" | _get_deb_field 'Source')" |
| 138 |
DEB_DISTRIBUTION="$(echo "$_pchangelog" | _get_deb_field 'Distribution')" |
| 139 |
DEB_URGENCY="$(echo "$_pchangelog" | _get_deb_field 'Urgency')" |
| 140 |
else |
| 141 |
die "debian/changelog could not be found in $rootdir" |
| 142 |
fi |
| 143 |
} |
| 144 |
|
| 145 |
is_valid_package_root() |
| 146 |
{ |
| 147 |
test -f "$1/debian/changelog" && test -f "$1/debian/control" |
| 148 |
} |
| 149 |
|
| 150 |
is_distribution_valid() |
| 151 |
{ |
| 152 |
local distro |
| 153 |
test -n "$1" || distro="$DEB_DISTRIBUTION" |
| 154 |
if [ "$(expr match "$distro" '^\(\(wheezy\|jessie\|stretch\|buster\|bullseye\|bookworm\|trixie\|forky\)\(-security\|-backports\|-backports-sloppy\)\?\)$')" = "$distro" ]; then |
| 155 |
return 0 |
| 156 |
elif [ "$(expr match "$distro" '^\(testing\(-proposed-updates\)\?\|unstable\|experimental\)$')" = "$distro" ]; then |
| 157 |
return 0 |
| 158 |
fi |
| 159 |
return 1 |
| 160 |
} |
| 161 |
|
| 162 |
opt_till_double_dash() { |
| 163 |
local name |
| 164 |
# Parse out remaining -- if needed |
| 165 |
while getopts ":" name; do |
| 166 |
case "$name" in |
| 167 |
?) if [ -n "$OPTARG" ]; then OPTIND=$(($OPTIND-1)); fi; break;; |
| 168 |
:) die "$OPTARG option is missing a required argument" ;; |
| 169 |
esac |
| 170 |
done |
| 171 |
if [ "$OPTIND" -gt 1 ]; then |
| 172 |
echo "$(($OPTIND-1))" |
| 173 |
else |
| 174 |
echo "0" |
| 175 |
fi |
| 176 |
} |
| 177 |
|
| 178 |
# Parse common options |
| 179 |
OPT_VERBOSE= |
| 180 |
OPT_PROMPT='prompt' |
| 181 |
|
| 182 |
while getopts ":vyn" name; do |
| 183 |
case "$name" in |
| 184 |
v) OPT_VERBOSE=1 ;; |
| 185 |
y) OPT_PROMPT='y' ;; |
| 186 |
n) OPT_PROMPT='n' ;; |
| 187 |
?) if [ -n "$OPTARG" ]; then OPTIND=$(($OPTIND-1)); fi; break;; |
| 188 |
:) die "$OPTARG option is missing a required argument" ;; |
| 189 |
esac |
| 190 |
done |
| 191 |
|
| 192 |
if [ "$OPTIND" -gt 1 ]; then |
| 193 |
shift "$(($OPTIND-1))" |
| 194 |
OPTIND=1 |
| 195 |
fi |
| 196 |
|
| 197 |
# Determine VCS type and source its library script |
| 198 |
|
| 199 |
VCS=$(determine_vcs) |
| 200 |
case "$VCS" in |
| 201 |
"") |
| 202 |
die "unable to determine VCS type or VCS tools (git, svn) are not installed." |
| 203 |
;; |
| 204 |
*) |
| 205 |
# Load VCS script file. Determines LIBDIR in the process |
| 206 |
if LIBDIR=$(determine_libdir) && [ $? -eq 0 ]; then |
| 207 |
if [ -f "$LIBDIR/$VCS.sh" ]; then |
| 208 |
. "$LIBDIR/$VCS.sh" |
| 209 |
else |
| 210 |
die "$VCS VCS is NOT supported (via $LIBDIR)." |
| 211 |
fi |
| 212 |
else |
| 213 |
die "unable to determine pkgkde-vcs library directory." |
| 214 |
fi |
| 215 |
;; |
| 216 |
esac |
| 217 |
|
| 218 |
exit 0 |