1#!/bin/bash
2#
3# Script to create a source tarball from Licq's git repository.
4# Latest version: https://github.com/licq-im/licq/blob/master/scripts/
5#
6# Copyright (c) 2007-2014 Erik Johansson <erijo@licq.org>
7# Distributed under the terms of the GNU GPL version 2.
8#
9
10### DEFAULT SETTINGS ###
11
12# Detect gnutar if available (BSD tar does not support --owner)
13TAR=$(which gnutar)
14if [ -z "$TAR" ]; then
15    TAR=tar
16fi
17
18# Where to save the tarball(s)
19TARDIR="."
20
21# What to name the tarball.
22# DESC is replaced with the output from git describe.
23# Note: Don't add .tar.gz or .tar.bz2
24TARNAME="DESC"
25
26# Archives to create (true or false)
27CREATE_GZ=false
28CREATE_BZ2=false
29
30# Sign archives using default GPG key (true or false)
31SIGN=false
32
33# Set extra version to exported revision
34EXTRA_VERSION=true
35
36# Which tree to export
37TREEISH="HEAD"
38
39# Ownership of files in tarball
40OWNER="nobody"
41GROUP="nogroup"
42
43# Test build after creating the tarball
44TEST_BUILD=false
45
46### END SETTINGS ###
47
48# Prints the usage
49function usage()
50{
51    echo "Usage: $0 (-g | -b) [OPTION]..."
52    echo "  -h, --help   This message"
53    echo "  -t treeish   Create tarball from treeish (default: ${TREEISH})"
54    echo "  -o dir       Save tarball in directory dir (default: ${TARDIR})"
55    echo "  -n name      Name the tarball name (default: ${TARNAME})"
56    echo "               DESC is replaced with the output from git describe"
57    echo "  -e           Don't set extra version (never done for tags)"
58    echo "  -g, --gzip   Create a tar.gz archive"
59    echo "  -b, --bzip2  Create a tar.bz archive"
60    echo "  -s, --sign   Sign archive(s) with default GPG key"
61    echo "  --build      Test build after creating the tarball"
62    echo ""
63    echo "To create a release:"
64    echo "$0 -e --bzip2 --gzip --sign -t licq-1.3.9 -n licq-1.3.9 --build"
65}
66
67if [ $# -eq 0 ]; then
68    echo "$0: Missing required argument (-g and/or -b)"
69    echo "Try \`$0 --help' for more information"
70    exit 2
71fi
72
73# Parse command line options
74args=$(getopt -n "$0" -o h,t:,o:,n:,e,g,b,s -l help,gzip,bzip2,sign,build -- $*)
75if [ $? -ne 0 ]; then
76    echo ""
77    usage
78    exit 1
79fi
80
81set -- $args
82while [ $# -gt 0 ]; do
83    case $1 in
84	-h|--help) usage; exit 0 ;;
85	-t) TREEISH=$(eval echo $2); shift ;;
86	-o) TARDIR=$(eval echo $2); shift ;;
87	-n) TARNAME=$(eval echo $2); shift ;;
88	-e) EXTRA_VERSION=false ;;
89	-g|--gzip) CREATE_GZ=true ;;
90	-b|--bzip2) CREATE_BZ2=true ;;
91	-s|--sign) SIGN=true ;;
92	--build) TEST_BUILD=true ;;
93	--) ;;
94	*) echo "$0: unknown option '$1'"; exit 1 ;;
95    esac
96    shift
97done
98
99if ! $CREATE_GZ && ! $CREATE_BZ2 ; then
100    echo "$0: You must choose to create a gzip and/or bzip2 archive"
101    echo ""
102    usage
103    exit 1
104fi
105
106if ! git rev-parse --verify --quiet "${TREEISH}" > /dev/null; then
107    echo "$0: Bad treeish '${TREEISH}'"
108    exit 1
109fi
110
111if git describe --exact-match "${TREEISH}" &> /dev/null; then
112    EXTRA_VERSION=false
113fi
114
115# Remove workdir
116function cleanup()
117{
118   if [ -n "${TMPDIR}" ]; then
119     echo "Removing ${TMPDIR}"
120     rm -rf "${TMPDIR}"
121     TMPDIR=""
122   fi
123}
124
125# Echos "$1 failed" or "failed" and then exits.
126function failed()
127{
128   if [ -z "$1" ]; then
129      echo "failed"
130   else
131      echo "$1 failed"
132   fi
133   if [ -r "${TMPFILE}" ]; then
134      cat "${TMPFILE}"
135   fi
136   cleanup
137   exit 1
138}
139
140function abort()
141{
142   echo "Aborted by user"
143   cleanup
144   exit 1
145}
146
147function run()
148{
149   "$@" &> "${TMPFILE}" || failed
150   rm -f "${TMPFILE}"
151}
152
153trap abort SIGHUP SIGINT SIGQUIT
154
155# Workdir/file
156TMPDIR=$(mktemp -d licq.XXXX) || failed "mktemp -d"
157TMPFILE="$(cd ${TMPDIR} && pwd)/.cmd.out"
158
159GITDESC="$(git describe ${TREEISH})"
160
161function expand_name()
162{
163    local name="$1"
164    name="${name//DESC/${GITDESC}}"
165    echo "${name}"
166}
167
168DIRNAME="$(expand_name "${TARNAME}")"
169TARNAME="$(expand_name "${TARNAME}")"
170LICQDIR="${TMPDIR}/${DIRNAME}"
171
172function exit_if_exists()
173{
174   if [ -e "$1" ]; then
175      echo "$1 already exists"
176      cleanup
177      exit 1
178   fi
179}
180
181function gitexport()
182{
183   local path="$1"
184   local dest="${LICQDIR}/$2"
185   local skip_missing="${3:-false}"
186
187   echo -n "Exporting ${path} ($(git describe --all --long ${TREEISH}))... "
188   if ${skip_missing} && ! git rev-parse --verify --quiet "${TREEISH}:${path}" > /dev/null; then
189       echo "not found, skipping"
190       return
191   fi
192
193   run git archive -o "${TMPDIR}/export.tar" ${TREEISH} "${path}"
194   run mkdir -p "${dest}"
195   run ${TAR} -xf "${TMPDIR}/export.tar" -C "${dest}" --strip-components 1
196   run rm "${TMPDIR}/export.tar"
197   echo "done"
198}
199
200function has_file()
201{
202   test -e "${LICQDIR}/$1"
203}
204
205function makecvs()
206{
207   echo -n "Running make -f $1/Makefile.cvs... "
208   run make -C "${LICQDIR}/$1" -f "Makefile.cvs"
209   rm -rf "${LICQDIR}/$1/autom4te.cache"
210   rm -f "${LICQDIR}/$1/Makefile.cvs"
211   echo "done"
212}
213
214${CREATE_GZ}  && exit_if_exists "${TARDIR}/${TARNAME}.tar.gz"
215${CREATE_BZ2} && exit_if_exists "${TARDIR}/${TARNAME}.tar.bz2"
216
217gitexport "licq" ""
218if has_file "Makefile.cvs"; then
219   gitexport "admin" "admin"
220   makecvs "."
221fi
222
223# Remove symbolic links in plugins dir
224if has_file plugins; then
225   run find "${LICQDIR}/plugins" -type l -delete
226fi
227
228# List of plugins that should be exported. Plugins that no longer are on trunk
229# should be listed in OLD_PLUGINS so that this script can continue to work with
230# older releases (e.g. the stable branches).
231PLUGINS="aosd auto-reply dbus forwarder icq jabber licqweb msn osd qt-gui rms"
232OLD_PLUGINS="console email qt4-gui"
233
234for plugin in ${PLUGINS} ${OLD_PLUGINS}; do
235   gitexport "${plugin}" "plugins/${plugin}" true
236   if has_file "plugins/${plugin}/Makefile.cvs"; then
237      if ! has_file "plugins/${plugin}/admin"; then
238         gitexport "admin" "plugins/${plugin}/admin"
239      fi
240      makecvs "plugins/${plugin}"
241   fi
242done
243
244if ${EXTRA_VERSION}; then
245   echo -n "Setting extra version (to -${GITDESC//-/_})..."
246   for file in $(find "${LICQDIR}" -name version.cmake -o -name LicqVersion.cmake); do
247      sed -i -r -e "s/^(set\(.*_VERSION_EXTRA).*\)/\1 \"-${GITDESC//-/_}\"\)/g" ${file}
248   done
249   echo "done"
250fi
251
252# Cleanup
253run find "${LICQDIR}" -name Makefile.common -delete
254
255echo "Creating tarball ${TARNAME}.tar"
256${TAR} --owner "${OWNER}" --group "${GROUP}" -C "${TMPDIR}" \
257    -cf "${TMPDIR}/${TARNAME}.tar" "${DIRNAME}" || failed
258
259# Test build only handles cmake
260if ${TEST_BUILD} && ! has_file "admin"; then
261    mkdir "${TMPDIR}/source" "${TMPDIR}/build"
262    tar xf "${TMPDIR}/${TARNAME}.tar" -C "${TMPDIR}/source" --strip-components 1 || failed
263
264    echo -n "Running cmake..."
265    (cd "${TMPDIR}/build" && run cmake -DCMAKE_INSTALL_PREFIX=../install -DBUILD_PLUGINS=On ../source)
266    echo "done"
267
268    echo -n "Compiling (this may take some time)..."
269    CCACHE_BASEDIR="$(cd ${TMPDIR} && pwd)" run make -j4 -C "${TMPDIR}/build"
270    echo "done"
271
272    echo -n "Installing..."
273    run make -C "${TMPDIR}/build" install
274    echo "done"
275
276    echo -n "Running licq..."
277    run "${TMPDIR}/install/bin/licq" -d 31 -b "${TMPDIR}/basedir" -l icq -l jabber -l msn
278    echo "done"
279fi
280
281function sign()
282{
283   if ${SIGN}; then
284      echo "Signing $1"
285      gpg --sign --armor --detach-sign --output "$1.sign" "$1" || failed "signing"
286   fi
287}
288
289if ${CREATE_GZ}; then
290   echo -n "Creating ${TARDIR}/${TARNAME}.tar.gz... "
291   cp "${TMPDIR}/${TARNAME}.tar" "${TARDIR}" || failed "copy tarball"
292   gzip --best "${TARDIR}/${TARNAME}.tar" || failed
293   echo "done"
294
295   sign "${TARDIR}/${TARNAME}.tar.gz"
296fi
297
298if ${CREATE_BZ2}; then
299   echo -n "Creating ${TARDIR}/${TARNAME}.tar.bz2... "
300   cp "${TMPDIR}/${TARNAME}.tar" "${TARDIR}" || failed "copy tarball"
301   bzip2 --best "${TARDIR}/${TARNAME}.tar" || failed
302   echo "done"
303
304   sign "${TARDIR}/${TARNAME}.tar.bz2"
305fi
306
307cleanup
308