summaryrefslogtreecommitdiffstats
path: root/redhat/genrpm.sh
blob: a6df1f0b5aa434b58ba6198007fa7247cd80f99b (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/bin/bash

cd "$( dirname "$0" )"
ARGS=""

DIST="$(rpmdist.sh --dist)"

# CCACHE related stuff
if [ ! -d /var/cache/ccache ]; then
  [ -z "${DIST}" ] && DIST="$(rpm -E "%{dist}")"
  if [ -n "${DIST}" ]; then
    export CCACHE_DIR=~/.ccache${DIST}.$(uname -m)
  fi
fi

while [ $# -gt 0 ]; do
  case "$1" in
    "--auto"|"-a") AUTO=1;;
    "--version"|"-v") REQVERSION="$2"; shift;;
    "--"*) ARGS="${ARGS} $1";;
    *) COMP="${1%%/}";;
  esac
  shift
done

if [ -d ~/tde/tde-tarballs/${REQVERSION} ]; then
  eval TARBALLS_DIR=~/tde/tde-tarballs/${REQVERSION}
else
  eval TARBALLS_DIR=~/tde/tde-tarballs/3.5.13.2
  #eval TARBALLS_DIR=~/tde/tde-tarballs/14.0.0
fi

clear
cat <<EOF
$(< /etc/redhat-release) [$(uname -m)]
This script generates RPM of TDE from source tarball.

EOF

# Checks RPMBUILD environment
RHEL="$( rpm -E "%{rhel}" )"
FEDORA="$( rpm -E "%{fedora}" )"
SUSE="$( rpm -E "%{suse_version}" )"
PCLINUXOS="$( rpm -E "%{pclinuxos}" )"
MGAVERSION="$( rpm -E "%{mgaversion}" )"
if [ "${RHEL}" = "%{rhel}" ] && [ "${FEDORA}" = "%{fedora}" ] && [ "${SUSE}" = "%{suse_version}" ] && [ "${PCLINUXOS}" = "%{pclinuxos}" ] && [ "${MGAVERSION}" = "%{mgaversion}" ] ; then
  cat <<EOF
Error: RPM macro %rhel or %fedora must be set to the distribution version to build !
E.g:
  %rhel 6 
or:
  %fedora 15
EOF
  exit 1
fi

# Checks TDE version to use
#if [ -z "${REQVERSION}" ]; then
#	REQVERSION="3.5.13"
#fi

if [ -z "${COMP}" ]; then
  select COMP in $( cut -f1 "components.txt" | grep -v "^#" ) ; do break; done
fi

# Gets package version from 'components.txt' file
VERSION=$( awk '{ if ($1 == "'${COMP}'") { print $2; } }' components.txt )
	
# If no version is set in text file, get version number from source tarball name
COMPNAME="${COMP##*/}"
if [ -z "${VERSION}" ]; then
  for d in "${TARBALLS_DIR}/main/${COMP%/*}" "${TARBALLS_DIR}/main/" "${PWD}/main/${COMP}/"; do
    for n in "trinity-${COMPNAME}-${REQVERSION:-*}.tar.gz" "trinity-${COMPNAME}?-${REQVERSION:-*}.tar.gz" "trinity-${COMPNAME/kde/tde}-${REQVERSION:-*}.tar.gz"  "trinity-${COMPNAME/kde/tde}-${REQVERSION:-*}*.tar.gz"; do
#      echo $d/$n
      set $d/$n
      if [ $# -eq 0 ]; then
        echo "No tarball found !"
        exit 1
      elif [ $# -eq 1 ] && [ -r "$1" ]; then
        TARBALL=$1
      elif [ $# -gt 1 ]; then
        select TARBALL in $*; do break; done
      fi
    done
  done

  echo "TARBALL is ${TARBALL##*/}"
fi

# Checks for version
if [ -z "${VERSION}" ] && [ -n "${REQVERSION}" ]; then
  VERSION="${REQVERSION}"
fi

# Checks for preversion (non-final)
if [ "${TARBALL}" != "${TARBALL%%~pre*}" ]; then
  PREVERSION="${TARBALL##*~}"
  PREVERSION="${PREVERSION%.tar*}"
fi
	
# Chooses a spec file (if many)
set $( cd "${COMP}"; echo *${COMP##*/}*.spec )
if [ $# -gt 1 ]; then
	if [ -n "${REQVERSION}" ]; then
		set $( cd "${COMP}"; echo *-${REQVERSION}.spec )
		SPEC="$1"
	else
		select SPEC in $*; do break; done
	fi
elif [ -r "${COMP}/$1" ]; then
	SPEC="$1"
else
	echo "Fatal: no spec file found !"
	echo $1
	exit 2
fi
	
cat <<EOF

About to build '${COMP}':
  Version: '${VERSION}'
  Preversion: '${PREVERSION}'
  Spec file: '${SPEC}'
  Tarball: '${TARBALL}'

CCACHE_DIR='${CCACHE_DIR}'

Press ENTER to build, or CTRL+C to abort.
EOF
[ -z "${AUTO}" ] && read rep
	
# Specific prefix for installation of some components
case "${COMP##*/}" in
	"qt3") PREFIX="/usr";;
esac
	
# Determines if we are running an i386 or x86_64 distro
if [ "$(rpm -q --qf '%{arch}\n' kernel | tail -n 1)" = "i686" ]; then
	ARGS="${ARGS} --target=i686"
fi

LOGFILE=/tmp/log.${COMP##*/}

SOURCEDIR="$(mktemp -d)"
# Puts the GIT files in SOURCEDIR
#rsync -rLv "${PWD}/${COMP}/" "${SOURCEDIR}/"
while read a b; do
  case "${a}" in
    "Source"*|"Patch"*)
      b=$(rpm -E "${b##*/}")
      [ -r "${COMP}/${b}" ] && cp -fv "${COMP}/${b}" "${SOURCEDIR}"
    ;;
  esac
done < "${COMP}/${SPEC}"

# Copies the SPEC file
cp -f "${COMP}/${SPEC}" "${SOURCEDIR}"

# Puts the TARBALL in SOURCEDIR
#cp -f "${TARBALLS_DIR}/${COMP}-"*.tar* "${SOURCEDIR}"
echo "Copying TARBALL ..."
cp -fv "${TARBALL}" "${SOURCEDIR}"

BUILDDIR="/dev/shm/BUILD${DIST}.$(uname -i)"
BUILDROOTDIR="/dev/shm/BUILDROOT${DIST}.$(uname -i)"

TOPSRCRPMDIR="$(rpm -E %_srcrpmdir)"
TOPRPMDIR="$(rpm -E %_rpmdir)"
SUBDIR="${COMP%/*}"
if [ "${SUBDIR}" = "${COMP}" ]; then
	SUBDIR="main"
fi
RPMDIR="${TOPRPMDIR}/${SUBDIR}"
SRCRPMDIR="${TOPSRCRPMDIR}/${SUBDIR}"

[ -d "${BUILDDIR}" ] || mkdir "${BUILDDIR}"
[ -d "${BUILDROOTDIR}" ] || mkdir "${BUILDROOTDIR}"

set -x
(
rpmbuild -ba \
	${ARGS} \
	--define "_specdir ${SOURCEDIR}" \
	--define "_sourcedir ${SOURCEDIR}" \
	--define "_builddir ${BUILDDIR}" \
	--define "_buildrootdir ${BUILDROOTDIR}" \
	--define "tde_prefix ${PREFIX:-/opt/trinity}" \
	--define "version ${VERSION:-3.5.13.2}" \
	--define "preversion ${PREVERSION}" \
	${SOURCEDIR}/${SPEC}
	echo "RET=$?"
) 2>&1 | tee ${LOGFILE}
eval "$(grep ^RET= ${LOGFILE})"
set +x

# Removes temporary source directory
rm -rf "${SOURCEDIR}"

if [ "${RET}" -gt 0 ]; then
	exit ${RET}
fi

if grep -q "error: Failed build dependencies:" ${LOGFILE}; then
#	DEPS=$( sed -n -e "/.* is needed by .*/ s/^[ \t]*\([a-zA-Z2-9_-]*\) .*/\1/p" ${LOGFILE} )
	set $( grep " is needed by " ${LOGFILE} | cut -d " " -f1 )
	exit 2
fi

rm -rf "${SOURCEDIR}"

# Removes BUILDDIR if build succeeded
rm -rf "${BUILDDIR}/"*${COMP##*/}-${VERSION}*