1#!/bin/bash
2
3# cvs-debi:  Install current version of deb package
4# cvs-debc:  List contents of current version of deb package
5#
6# Based on debi/debc; see them for copyright information
7# Based on cvs-buildpackage, copyright 1997 Manoj Srivastava
8# (CVS Id: cvs-buildpackage,v 1.58 2003/08/22 17:24:29 srivasta Exp)
9# This code is copyright 2003, Julian Gilbey <jdg@debian.org>
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program. If not, see <https://www.gnu.org/licenses/>.
23
24set -e
25
26PROGNAME=`basename $0 .sh`  # .sh for debugging purposes
27
28usage () {
29    if   [ "$PROGNAME" = cvs-debi ];  then usage_i
30    elif [ "$PROGNAME" = cvs-debc ];  then usage_c
31    else echo "Unrecognised invocation name: $PROGNAME" >&2; exit 1
32    fi;
33}
34
35usage_i () {
36    echo \
37"Usage: $PROGNAME [options] [package ...]
38  Install the .deb file(s) just created by cvs-buildpackage or cvs-debuild,
39  as listed in the .changes file generated on that run.  If packages are
40  listed, only install those specified binary packages from the .changes file.
41
42  Note that unlike cvs-buildpackage, the only way to specify the
43  source package name is with the -P option; you cannot simply have it
44  as the last parameter.
45
46  Also uses the cvs-buildpackage configuration files to determine the
47  location of the build tree, as described in the manpage.
48
49  Available options:
50    -M<module>        CVS module name
51    -P<package>       Package name
52    -V<version>       Package version
53    -T<tag>           CVS tag to use
54    -R<root dir>      Root directory
55    -W<work dir>      Working directory
56    -x<prefix>        CVS default module prefix
57    -a<arch>          Search for .changes file made for Debian build <arch>
58    -t<target>        Search for .changes file made for GNU <target> arch
59    --help            Show this message
60    --version         Show version and copyright information
61  Other cvs-buildpackage options will be silently ignored."
62}
63
64usage_c () {
65    echo \
66"Usage: $PROGNAME [options] [package ...]
67  Display the contents of the .deb file(s) just created by
68  cvs-buildpackage or cvs-debuild, as listed in the .changes file generated
69  on that run.  If packages are listed, only display those specified binary
70  packages from the .changes file.
71
72  Note that unlike cvs-buildpackage, the only way to specify the
73  source package name is with the -P option; you cannot simply have it
74  as the last parameter.
75
76  Also uses the cvs-buildpackage configuration files to determine the
77  location of the build tree, as described in its manpage.
78
79  Available options:
80    -M<module>        CVS module name
81    -P<package>       Package name
82    -V<version>       Package version
83    -T<tag>           CVS tag to use
84    -R<root dir>      Root directory
85    -W<work dir>      Working directory
86    -x<prefix>        CVS default module prefix
87    -a<arch>          Search for .changes file made for Debian build <arch>
88    -t<target>        Search for .changes file made for GNU <target> arch
89    --help            Show this message
90    --version         Show version and copyright information
91  Other cvs-buildpackage options will be silently ignored."
92}
93
94version () { echo \
95"This is $PROGNAME, from the Debian devscripts package, version ###VERSION###
96This code is copyright 2003, Julian Gilbey <jdg@debian.org>,
97all rights reserved.
98Based on original code by Christoph Lameter and Manoj Srivastava.
99This program comes with ABSOLUTELY NO WARRANTY.
100You are free to redistribute this code under the terms of
101the GNU General Public License, version 2 or later."
102}
103
104setq() {
105    # Variable Value Doc string
106    if [ "x$2" = "x" ]; then
107	echo >&2 "$progname: Unable to determine $3"
108	exit 1;
109    else
110	if [ ! "x$Verbose" = "x" ]; then
111	    echo "$progname: $3 is $2";
112	fi
113	eval "$1=\"\$2\"";
114    fi
115}
116
117# Is cvs-buildpackage installed?
118if ! command -v cvs-buildpackage >/dev/null 2>&1; then
119    echo "$PROGNAME: need the cvs-buildpackage package installed to run this" >&2
120    exit 1
121fi
122
123# Long term variables, which may be set in the cvsdeb config file or the
124# environment:
125# rootdir workdir (if all original sources are kept in one dir)
126
127TEMPDIR=$(mktemp -dt cvs-debi.XXXXXXXX) || {
128    echo "$PROGNAME: unable to create temporary directory" >&2
129    echo "Aborting..." >&2
130    exit 1
131}
132TEMPFILE=$TEMPDIR/cl-tmp
133trap 'rm -f "$TEMPFILE"; rmdir "$TEMPDIR"' EXIT
134
135TAGOPT=
136
137# Command line; will bomb out if unrecognised options
138TEMP=$(getopt -a -s bash \
139       -o hC:EH:G:M:P:R:T:U:V:W:Ff:dcnr:x:Bp:Dk:a:Sv:m:e:i:I:t: \
140       --long help,version,ctp,tC,sgpg,spgp,us,uc,op \
141       --long si,sa,sd,ap,sp,su,sk,sr,sA,sP,sU,sK,sR,ss,sn \
142       -n "$PROGNAME" -- "$@")
143eval set -- $TEMP
144
145while true ; do
146    case "$1" in
147        -h|--help)   usage;   exit 0  ; shift   ;;
148        --version)   version; exit 0  ; shift   ;;
149	-M) opt_cvsmodule="$2"        ; shift 2 ;;
150	-P) opt_package="$2"          ; shift 2 ;;
151	-R) opt_rootdir="$2"          ; shift 2 ;;
152	-T) opt_tag="$2"              ; shift 2 ;;
153	-V) opt_version="$2"          ; shift 2 ;;
154	-W) opt_workdir="$2"          ; shift 2 ;;
155	-x) opt_prefix="$2"           ; shift 2 ;;
156        -a) targetarch="$2"           ; shift 2 ;;
157        -t) if [ "$2" != "C" ]; then targetgnusystem="$2"; fi
158	                              shift 2 ;;
159
160	# everything else is silently ignored
161	-[CHfGUr])                      shift 2 ;;
162	-[FnE])                         shift   ;;
163       --ctp|--op|--tC)                 shift   ;;
164	-[dDBbS])                       shift   ;;
165        -p)                             shift 2 ;;
166       --us|--uc|--sgpg|--spgp)         shift   ;;
167       --s[idapukrAPUKRns])             shift   ;;
168       --ap)                            shift   ;;
169        -[kvmeiI])                      shift 2 ;;
170
171        --) shift ; break ;;
172         *) echo >&2 "Internal error! ($1)"
173            usage; exit 1 ;;
174    esac
175done
176
177if [ "x$opt_cvsmodule" = "x" -a "x$opt_package" = "x" -a \
178      ! -e 'debian/changelog' ] ; then
179    echo >&2 "$progname should be run in the top working directory of"
180    echo >&2 "a Debian Package, or an explicit package (or CVS module) name"
181    echo >&2 "should be given."
182    exit 1
183fi
184
185if [ "x$opt_tag" != "x" ]; then
186    TAGOPT=-r$opt_tag
187fi
188
189# Command line, env variable, config file, or default
190# This anomalous position is in case we need to check out the changelog
191# below (anomalous since we have not loaded the config file yet)
192if [ ! "x$opt_prefix" = "x" ]; then
193    prefix="$opt_prefix"
194elif [ ! "x$CVSDEB_PREFIX" = "x" ]; then
195    prefix="$CVSDEB_PREFIX"
196elif [ ! "x$conf_prefix" = "x" ]; then
197    prefix="$conf_prefix"
198else
199    prefix=""
200fi
201
202# put a slash at the end of the prefix
203if [ "X$prefix" != "X" ]; then
204    prefix="$prefix/";
205    prefix=`echo $prefix | sed 's://:/:g'`;
206fi
207
208if [ ! -f CVS/Root ]; then
209    if [ "X$CVSROOT" = "X" ]; then
210	echo "no CVS/Root file found, and CVSROOT var is empty" >&2
211	exit 1
212    fi
213else
214    CVSROOT=$(cat CVS/Root)
215    export CVSROOT
216fi
217
218if [ "x$opt_package" = "x" ]; then
219    # Get the official package name and version.
220    if [ -f debian/changelog ]; then
221	# Ok, changelog exists
222	 setq "package" \
223	    "`dpkg-parsechangelog -SSource`" \
224		"source package"
225	setq "version" \
226	    "`dpkg-parsechangelog -SVersion`" \
227		"source version"
228    elif [ "x$opt_cvsmodule" != "x" ]; then
229	# Hmm. Well, see if we can checkout the changelog file
230	rm -f $TEMPFILE
231	cvs -q co -p $TAGOPT $opt_cvsmodule/debian/changelog > $TEMPFILE
232        setq "package" \
233	    "`dpkg-parsechangelog -l$TEMPFILE -SSource`" \
234          "source package"
235        setq "version" \
236          "`dpkg-parsechangelog -l$TEMPFILE -SVersion`" \
237          "source version"
238        rm -f "$TEMPFILE"
239    else
240	# Well. We don't know what this package is.
241	echo >&2 " This does not appear be a Debian source tree, since"
242	echo >&2 " there is no debian/changelog, and there was no"
243	echo >&2 " package name or cvs module given on the command line"
244	echo >&2 " it is hard to figure out what the package name "
245	echo >&2 " should be. I give up."
246	exit 1
247    fi
248else
249    # The user knows best; package name is provided
250    setq "package" "$opt_package" "source package"
251
252    # Now, the version number
253    if [ "x$opt_version" != "x" ]; then
254	# All hail the user provided value
255	setq "version" "$opt_version" "source package"
256    elif [ -f debian/changelog ]; then
257	# Fine, see what the changelog says
258	setq "version" \
259	    "`dpkg-parsechangelog -SVersion`" \
260		"source version"
261    elif [ "x$opt_cvsmodule" != "x" ]; then
262	# Hmm. The CVS module name is known, so lets us try exporting changelog
263	rm -f $TEMPFILE
264	cvs -q co -p $TAGOPT $opt_cvsmodule/debian/changelog > $TEMPFILE
265        setq "version" \
266          "`dpkg-parsechangelog -l$TEMPFILE -SVersion`" \
267          "source version"
268        rm -f "$TEMPFILE"
269    else
270	# Ok, try exporting the package name
271	rm -f $TEMPFILE
272	cvsmodule="${prefix}$package"
273	cvs -q co -p $TAGOPT $cvsmodule/debian/changelog > $TEMPFILE
274        setq "version" \
275          "`dpkg-parsechangelog -l$TEMPFILE -SVersion`" \
276          "source version"
277        rm -f "$TEMPFILE"
278    fi
279fi
280
281rm -f $TEMPFILE
282rmdir $TEMPDIR
283trap "" 0 1 2 3 7 10 13 15
284
285
286non_epoch_version=$(echo -n "$version" | perl -pe 's/^\d+://')
287upstream_version=$(echo -n "$non_epoch_version" | sed  -e 's/-[^-]*$//')
288debian_version=$(echo -n $non_epoch_version |  perl -nle 'm/-([^-]*)$/ && print $1')
289
290# The default
291if [ "X$opt_rootdir" != "X" ]; then
292    rootdir="$opt_rootdir"
293else
294    rootdir='/usr/local/src/Packages'
295fi
296
297if [ "X$opt_workdir" != "X" ]; then
298    workdir="$opt_workdir"
299else
300    workdir="$rootdir/$package"
301fi
302
303# Load site defaults and over rides.
304if [ -f /etc/cvsdeb.conf ]; then
305    . /etc/cvsdeb.conf
306fi
307
308# Load user defaults and over rides.
309if [ -f ~/.cvsdeb.conf ]; then
310    . ~/.cvsdeb.conf
311fi
312
313# Command line, env variable, config file, or default
314if [ ! "x$opt_rootdir" = "x" ]; then
315    rootdir="$opt_rootdir"
316elif [ ! "x$CVSDEB_ROOTDIR" = "x" ]; then
317    rootdir="$CVSDEB_ROOTDIR"
318elif [ ! "x$conf_rootdir" = "x" ]; then
319    rootdir="$conf_rootdir"
320fi
321
322# Command line, env variable, config file, or default
323if [ ! "x$opt_workdir" = "x" ]; then
324    workdir="$opt_workdir"
325elif [ ! "x$CVSDEB_WORKDIR" = "x" ]; then
326    workdir="$CVSDEB_WORKDIR"
327elif [ ! "x$conf_workdir" = "x" ]; then
328    workdir="$conf_workdir"
329else
330    workdir="$rootdir/$package"
331fi
332
333if [ ! -d "$workdir" ]; then
334    echo >&2 "The working directory, $workdir, does not exist. Aborting."
335    if [ ! -d "$rootdir" ]; then
336	echo >&2 "The root directory, $rootdir, does not exist either."
337    fi
338    exit 1;
339fi
340
341# The next part is based on debi
342
343if [ -n "$targetarch" ] && [ -n "$targetgnusystem" ]; then
344    setq arch "$(dpkg-architecture "-a${targetarch}" "-t${targetgnusystem}" -qDEB_HOST_ARCH)" "build architecture"
345elif [ -n "$targetarch" ]; then
346    setq arch "$(dpkg-architecture "-a${targetarch}" -qDEB_HOST_ARCH)" "build architecture"
347elif [ -n "$targetgnusystem" ]; then
348    setq arch "$(dpkg-architecture "-t${targetgnusystem}" -qDEB_HOST_ARCH)" "build architecture"
349else
350    setq arch "$(dpkg-architecture -qDEB_HOST_ARCH)" "build architecture"
351fi
352
353pva="${package}_${non_epoch_version}_${arch}"
354changes="$pva.changes"
355
356cd $workdir || {
357    echo "Couldn't cd $workdir.  Aborting" >&2
358    exit 1
359}
360
361if [ ! -r "$changes" ]; then
362    echo "Can't read $workdir/$changes!  Have you built the package yet?" >&2
363    exit 1
364fi
365
366# Just call debc/debi respectively, now that we have a changes file
367
368SUBPROG=${PROGNAME#cvs-}
369
370exec $SUBPROG --check-dirname-level 0 $changes "$@"
371