1#/bin/bash
2
3#################################################################################################
4# Helper functions
5
6function usage()
7{
8	echo "Usage:"
9	echo "      $0 [options] <source_tree_dir> <kvirc_version>"
10	echo "Available options:"
11	echo "   --gzip"
12	echo "     Use gzip instead of bzip2"
13	echo "   --bzip"
14	echo "     Use bzip instead of gzip"
15	echo "   --md5sum"
16	echo "     Create md5sum of the package too"
17	echo "   --help"
18	echo "     Show this help"
19}
20
21#################################################################################################
22# Gather commandline params
23
24SOURCETREEDIR=""
25KVIRCVERSION=""
26COMPRESSSWITCH="j"
27OUTPUTEXTENSION="bz2"
28DOMD5SUM="no"
29
30for i in $*; do
31	case $i in
32		--gzip)
33				COMPRESSSWITCH="z"
34				OUTPUTEXTENSION="gz"
35			;;
36		--bzip)
37				COMPRESSSWITCH="j"
38				OUTPUTEXTENSION="bz2"
39			;;
40		--help)
41				usage
42				exit
43			;;
44		--md5sum)
45				DOMD5SUM="yes"
46			;;
47		--*)
48			echo "ERROR: unrecognized option $i"
49			usage
50			exit
51			;;
52		*)
53			if test -z "$SOURCETREEDIR"; then
54				SOURCETREEDIR=$i
55			else
56				KVIRCVERSION=$i
57			fi
58			;;
59	esac
60done
61
62if [ -z "$SOURCETREEDIR" ]; then
63	echo "ERROR: source tree directory missing"
64	usage
65	exit
66fi
67
68if [ -z "$KVIRCVERSION" ]; then
69	echo "ERROR: KVIrc version missing"
70	usage
71	exit
72fi
73
74if [ ! -d $SOURCETREEDIR ]; then
75	echo "ERROR: the source tree parameter doesn't identify a directory"
76	usage
77	exit
78fi
79
80#################################################################################################
81# Setup vars
82
83THISDIR=$(pwd)
84TEMPDIR=/tmp
85PKGSRCDIR=kvirc-"$KVIRCVERSION"
86TEMPSRCDIR="$TEMPDIR/$PKGSRCDIR"
87OUTPUTFILENAME="kvirc-${KVIRCVERSION}.tar.${OUTPUTEXTENSION}"
88OUTPUTFILE="${THISDIR}/${OUTPUTFILENAME}"
89MD5SUMOUTPUTFILENAME="${OUTPUTFILENAME}.md5"
90MD5SUMOUTPUTFILE="${THISDIR}/${MD5SUMOUTPUTFILENAME}"
91
92#################################################################################################
93# Clear the temporary dir
94
95if [ -d "$TEMPSRCDIR" ]; then
96	echo "Removing stale target directory..."
97	rm -rf "$TEMPSRCDIR"
98fi
99
100#################################################################################################
101# Export git into the temporary dir
102
103cd "$SOURCETREEDIR"
104echo "Exporting git dir into ${TEMPSRCDIR}..."
105# Ensure the prefix parameter has a slash at the end, or bad things could happen
106git checkout-index -a -f --prefix="${TEMPSRCDIR}/"
107
108
109#################################################################################################
110# Figure out the git revision
111
112echo "Determining git revision..."
113REVISION=$(git log -1 --date=short "--pretty=%h")
114
115echo "Revision is $REVISION"
116echo $REVISION > "$TEMPSRCDIR/.gitrevision"
117
118#################################################################################################
119# Remove stuff that should not be there
120
121echo "Removing unused stuff..."
122rm -f "${TEMPSRCDIR}/dist/secret.enc"
123
124#################################################################################################
125# Make room for the output files
126
127if [ -f "${OUTPUTFILE}" ]; then
128	echo "Removing the existing output file..."
129	rm -f "${OUTPUTFILE}"
130fi
131
132if [ "${DOMD5SUM}" = "yes" ]; then
133	if [ -f "${MD5SUMOUTPUTFILE}" ]; then
134		echo "Cleaning stale md5sum file..."
135		rm -f "${MD5SUMOUTPUTFILE}"
136	fi
137fi
138
139#################################################################################################
140# Do compress
141
142echo "Compressing sources into $OUTPUTFILE"
143
144cd "$TEMPDIR"
145
146TARPARAMS="${COMPRESSSWITCH}cf"
147tar -$TARPARAMS "$OUTPUTFILE" "$PKGSRCDIR" || exit
148
149#################################################################################################
150# Do md5sum if requested
151
152cd "$THISDIR"
153
154if [ "${DOMD5SUM}" = "yes" ]; then
155	cd "$THISDIR"
156	md5sum "${OUTPUTFILENAME}" > "${MD5SUMOUTPUTFILE}"
157fi
158
159#################################################################################################
160# Cleanup the exported Git branch
161
162echo "Removing target directory..."
163rm -rf "${TEMPSRCDIR}"
164
165#################################################################################################
166# Show the work done
167
168echo "Here is the output I've generated"
169
170cd "${THISDIR}"
171
172ls -al "${OUTPUTFILENAME}"
173
174if [ "${DOMD5SUM}" = "yes" ]; then
175	ls -la "${MD5SUMOUTPUTFILENAME}"
176fi
177
178
179#################################################################################################
180# Done :)
181
182echo "Done. Have a nice day. :)"
183