summaryrefslogtreecommitdiff
path: root/rpm/buildrpm.sh
blob: 182ab11b3059bd1cadc4a202bc286578622ed996 (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
#!/bin/sh

# ******************************************************************
#      Author: Chad Elliott
#        Date: 8/13/2009
# Description: Create an MPC rpm based on the current version number.
# ******************************************************************

## First find out where this script is located
if [ "$0" != "`basename $0`" ]; then
  if [ "`echo $0 | cut -c1`" = "/" ]; then
    loc="`dirname $0`"
  else
    loc="`pwd`/`dirname $0`"
  fi
else
  ## Do my own 'which' here
  loc="."
  for i in `echo $PATH | tr ':' '\012'`; do
    if [ -x "$i/$0" -a ! -d "$i/$0" ]; then
      loc="$i"
      break
    fi
  done
fi

## Now, get back to where the main MPC script is located
while [ ! -x $loc/mpc.pl ]; do
  loc=`dirname $loc`
done

## Build up the packager name and email address
if [ -z "$REPLYTO" ]; then
  DOMAIN=`hostname | sed 's/[^\.][^\.]*\.//'`
  FULLDOMAIN=`echo $DOMAIN | grep '\.'`
  if [ -z "$DOMAIN" -o -z "$FULLDOMAIN" ]; then
    RESOLVDOMAIN=`grep '^search' /etc/resolv.conf | sed 's/.*\s//'`
    FULLDOMAIN=`echo $RESOLVDOMAIN | grep '\.'`
    if [ -z "$DOMAIN" -o -n "$FULLDOMAIN" ]; then
      DOMAIN=$RESOLVDOMAIN
    fi
  fi
  REPLYTO="$LOGNAME@$DOMAIN"
fi
PACKAGER=`getent passwd $LOGNAME | cut -d: -f5`
if [ -z "$PACKAGER" ]; then
  PACKAGER=$CL_USERNAME
fi
if [ -z "$PACKAGER" ]; then
  PACKAGER="<$REPLYTO>"
else
  PACKAGER="$PACKAGER <$REPLYTO>"
fi

## Save the MPC version
VERSION=`$loc/mpc.pl --version | sed 's/.*v//'`

## This is where we'll create the spec file and do the work
WDIR=/tmp/mpc.$$

## This is the directory name that RPM expects
MDIR=MPC-$VERSION

## This corresponds to BuildRoot in MPC.spec
BDIR=/tmp/mpc

## This is the final install directory and corresponds to the %files section
## of MPC.spec
FDIR=/usr/lib/MPC

##Check if build and work directory already exist
if [ -d "$BDIR" -o -f "$BDIR" ]; then
  echo "Necessary directory $BDIR already exists."
  echo "Exiting."
  exit
fi

if [ -d "$WDIR" -o -f "$WDIR" ]; then
  echo "Necessary directory $WDIR already exists."
  echo "Exiting."
  exit
fi

## Create our working directory
mkdir -p $WDIR

## The directory where RPM will place the resulting file
if [ -x /usr/src/redhat -a -w /usr/src/redhat ]; then
  RPMLOC=/usr/src/redhat
elif [ -x /usr/src/packages -a -w /usr/src/packages ]; then
  RPMLOC=/usr/src/packages
else
  RPMLOC=$WDIR/rpmbuild
  mkdir -p $RPMLOC
  mkdir -p $RPMLOC/BUILD
  mkdir -p $RPMLOC/RPMS
  mkdir -p $RPMLOC/SOURCES
fi

## Make the spec file
cd $WDIR
sed "s/VERSION/$VERSION/; s/PACKAGER/$PACKAGER/; s!FINALDIR!$FDIR!" $loc/rpm/MPC.templ > MPC.spec

## Make a copy of the original MPC source to the new directory
mkdir -p $MDIR/$FDIR
cd $loc
tar --exclude=.git\* -cf - . | (cd $WDIR/$MDIR/$FDIR; tar -xf -)

## Create the build source tar.bz2
cd $WDIR
tar --owner root --group root -cf $RPMLOC/SOURCES/$MDIR.tar $MDIR
bzip2 -9f $RPMLOC/SOURCES/$MDIR.tar

## Perform the RPM creation step
rm -rf $BDIR
mkdir -p $BDIR
rpmbuild --define "_topdir $RPMLOC" --define "_buildrootdir $BDIR" --define "buildroot $BDIR" --define "__arch_install_post %{nil}" -bb MPC.spec

if [ "$RPMLOC" = "$WDIR/rpmbuild" ]; then
  echo "Copying rpm to $loc/rpm"
  cp $RPMLOC/RPMS/*/*.rpm $loc/rpm
fi

## Clean everything up
cd ..
rm -rf $WDIR $BDIR