1311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff#!/bin/bash -e
264fe2324403ea78dbd781073f4e5b34768676e2copenvcdiff@gmail.com# Copyright 2005 Google Inc.
364fe2324403ea78dbd781073f4e5b34768676e2copenvcdiff@gmail.com# Author: Craig Silverstein
464fe2324403ea78dbd781073f4e5b34768676e2copenvcdiff@gmail.com#
564fe2324403ea78dbd781073f4e5b34768676e2copenvcdiff@gmail.com# Licensed under the Apache License, Version 2.0 (the "License");
664fe2324403ea78dbd781073f4e5b34768676e2copenvcdiff@gmail.com# you may not use this file except in compliance with the License.
764fe2324403ea78dbd781073f4e5b34768676e2copenvcdiff@gmail.com# You may obtain a copy of the License at
864fe2324403ea78dbd781073f4e5b34768676e2copenvcdiff@gmail.com#
964fe2324403ea78dbd781073f4e5b34768676e2copenvcdiff@gmail.com#      http://www.apache.org/licenses/LICENSE-2.0
1064fe2324403ea78dbd781073f4e5b34768676e2copenvcdiff@gmail.com#
1164fe2324403ea78dbd781073f4e5b34768676e2copenvcdiff@gmail.com# Unless required by applicable law or agreed to in writing, software
1264fe2324403ea78dbd781073f4e5b34768676e2copenvcdiff@gmail.com# distributed under the License is distributed on an "AS IS" BASIS,
1364fe2324403ea78dbd781073f4e5b34768676e2copenvcdiff@gmail.com# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1464fe2324403ea78dbd781073f4e5b34768676e2copenvcdiff@gmail.com# See the License for the specific language governing permissions and
1564fe2324403ea78dbd781073f4e5b34768676e2copenvcdiff@gmail.com# limitations under the License.
1664fe2324403ea78dbd781073f4e5b34768676e2copenvcdiff@gmail.com#
17311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff# This takes one commandline argument, the name of the package.  If no
18311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff# name is given, then we'll end up just using the name associated with
19311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff# an arbitrary .tar.gz file in the rootdir.  That's fine: there's probably
20311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff# only one.
21311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff#
22311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff# Run this from the 'packages' directory, just under rootdir
23311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff
24311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff## Set LIB to lib if exporting a library, empty-string else
25311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiffLIB=
26311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff#LIB=lib
27311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff
28311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiffPACKAGE="$1"
29d18457863096b3685e56f5a8919959f6afbdb121openvcdiffVERSION="$2"
30311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff
31311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff# We can only build Debian packages, if the Debian build tools are installed
32311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiffif [ \! -x /usr/bin/debuild ]; then
33311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff  echo "Cannot find /usr/bin/debuild. Not building Debian packages." 1>&2
34311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff  exit 0
35311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdifffi
36311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff
37311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff# Double-check we're in the packages directory, just under rootdir
38311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiffif [ \! -r ../Makefile -a \! -r ../INSTALL ]; then
39311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff  echo "Must run $0 in the 'packages' directory, under the root directory." 1>&2
40311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff  echo "Also, you must run \"make dist\" before running this script." 1>&2
41311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff  exit 0
42311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdifffi
43311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff
44311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff# Find the top directory for this package
45311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdifftopdir="${PWD%/*}"
46311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff
47311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff# Find the tar archive built by "make dist"
48d18457863096b3685e56f5a8919959f6afbdb121openvcdiffarchive="${PACKAGE}-${VERSION}"
49d18457863096b3685e56f5a8919959f6afbdb121openvcdiffarchive_with_underscore="${PACKAGE}_${VERSION}"
50311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiffif [ -z "${archive}" ]; then
51311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff  echo "Cannot find ../$PACKAGE*.tar.gz. Run \"make dist\" first." 1>&2
52311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff  exit 0
53311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdifffi
54311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff
55311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff# Create a pristine directory for building the Debian package files
56311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdifftrap 'rm -rf '`pwd`/tmp'; exit $?' EXIT SIGHUP SIGINT SIGTERM
57311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff
58311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiffrm -rf tmp
59311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiffmkdir -p tmp
60311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiffcd tmp
61311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff
62311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff# Debian has very specific requirements about the naming of build
63311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff# directories, and tar archives. It also wants to write all generated
64311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff# packages to the parent of the source directory. We accommodate these
65311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff# requirements by building directly from the tar file.
66311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiffln -s "${topdir}/${archive}.tar.gz" "${LIB}${archive}.orig.tar.gz"
67d18457863096b3685e56f5a8919959f6afbdb121openvcdiff# Some version of debuilder want foo.orig.tar.gz with _ between versions.
68d18457863096b3685e56f5a8919959f6afbdb121openvcdiffln -s "${topdir}/${archive}.tar.gz" "${LIB}${archive_with_underscore}.orig.tar.gz"
69311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdifftar zfx "${LIB}${archive}.orig.tar.gz"
70311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff[ -n "${LIB}" ] && mv "${archive}" "${LIB}${archive}"
71311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiffcd "${LIB}${archive}"
72311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff# This is one of those 'specific requirements': where the deb control files live
73d18457863096b3685e56f5a8919959f6afbdb121openvcdiffcp -a "packages/deb" "debian"
74311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff
75311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff# Now, we can call Debian's standard build tool
76311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiffdebuild -uc -us
77311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiffcd ../..                            # get back to the original top-level dir
78311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff
79311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff# We'll put the result in a subdirectory that's named after the OS version
80311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff# we've made this .deb file for.
81311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiffdestdir="debian-$(cat /etc/debian_version 2>/dev/null || echo UNKNOWN)"
82311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff
83311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiffrm -rf "$destdir"
84311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiffmkdir -p "$destdir"
85311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiffmv $(find tmp -mindepth 1 -maxdepth 1 -type f) "$destdir"
86311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiff
87311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiffecho
88311c71486f5f6074e5ba62a7f4c5397c8700b868openvcdiffecho "The Debian package files are located in $PWD/$destdir"
89