summaryrefslogtreecommitdiffstats
path: root/makerpm.sh
blob: b00d5d7196641f33c77d03a23b0a073051e59997 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash

# Simple script to create RPMs for G4S

cleanup()
{
	rm -rf ${RPMBUILDDIR} > /dev/null 2>&1
	rm -f ${PKGCONFIG} > /dev/null 2>&1
}

fail()
{
	cleanup
	echo $1
	exit $2
}

create_dir()
{
	if [ ! -d "$1" ] ; then
		mkdir -p "$1"
		if [ $? -ne 0 ] ; then
			fail "Unable to create dir $1" $?
		fi
	fi
}

gittotar()
{
	# Only archives committed changes
	gitarchive_dir="${RPMBUILDDIR}/gitarchive"
	specfile="${gitarchive_dir}/${SRCTAR_DIR}/${PKG_NAME}.spec"
	create_dir "${gitarchive_dir}"

	# Export the current commited git changes to a directory
	git archive --format=tar --prefix=${SRCTAR_DIR}/ HEAD | (cd ${gitarchive_dir} && tar xf -)

	# Create a new spec file with the current package version information
	sed -e "s#__PKG_RELEASE__#${PKG_RELEASE}#" \
		-e "s#__PKG_NAME__#${PKG_NAME}#" \
		-e "s#__PKG_VERSION__#${PKG_VERSION}#" \
		${specfile} > ${specfile}.new
	mv ${specfile}.new ${specfile}

	# Now create a tar file
	( cd ${gitarchive_dir} && tar cf - ${SRCTAR_DIR} | gzip -c > ${SRCTAR} )
	if [ $? -ne 0 -o \! -s ${SRCTAR} ] ; then
		fail "Unable to create git archive" $?
	fi
}

prep()
{
	rm -rf ${RPMBUILDDIR} > /dev/null 2>&1
	create_dir ${RPMBUILDDIR}

	# Create a tar file out of the current committed changes
	gittotar

}

create_rpm()
{
	# Create the rpm
	# _topdir Notifies rpmbuild the location of the root directory
	#         containing the RPM information
	# _release Allows Jenkins to setup the version using the
	#          build number
	rpmbuild --define "_topdir ${RPMBUILDDIR}" \
		-ta ${SRCTAR}
	if [ $? -ne 0 ] ; then
		fail "Unable to create rpm" $?
	fi

	# Move the rpms to the root directory
	mv ${RPMBUILDDIR_RPMS}/noarch/*rpm ${BUILDDIR}
	mv ${RPMBUILDDIR_SRPMS}/*rpm ${BUILDDIR}
	if [ $? -ne 0 ] ; then
		fail "Unable to move rpm to ${BUILDDIR}" $?
	fi

	echo "RPMS are now available in ${BUILDDIR}"
}

################## MAIN #####################

# Create a config file with the package information
PKGCONFIG=${PWD}/pkgconfig.in
env python pkgconfig.py
if [ ! -f "${PKGCONFIG}" ] ; then
	fail "Unable to create package information file ${PKGCONFIG}" 1
fi

# Get package version information
. ${PKGCONFIG}
if [ -z "${NAME}" ] ; then
	fail "Unable to read the package name from the file created by pkgconfig.py" 1
fi
if [ -z "${VERSION}" ] ; then
	fail "Unable to read the package version from the file created by pkgconfig.py" 1
fi
if [ -z "${RELEASE}" ] ; then
	fail "Unable to read the package version from the file created by pkgconfig.py" 1
fi

PKG_NAME=$NAME
PKG_VERSION=$VERSION

#
# This can be set by JENKINS builds
# If the environment variable PKG_RELEASE
# has not been set, then we set it locally to
# a default value
#
if [ -z "$PKG_RELEASE" ] ; then
	PKG_RELEASE="${RELEASE}"
else
	PKG_RELEASE="${RELEASE}.${PKG_RELEASE}"
fi

BUILDDIR=$PWD/build
RPMBUILDDIR=${BUILDDIR}/rpmbuild
RPMBUILDDIR_RPMS=${RPMBUILDDIR}/RPMS
RPMBUILDDIR_SRPMS=${RPMBUILDDIR}/SRPMS
SRCNAME=${PKG_NAME}-${PKG_VERSION}-${PKG_RELEASE}
SRCTAR_DIR=${PKG_NAME}-${PKG_VERSION}
SRCTAR=${RPMBUILDDIR}/${SRCNAME}.tar.gz

prep
create_rpm
cleanup