#!/bin/bash
# Script: mkvendorrpm
# Script that creates RPMs from the svnroot instead via the normal "rear mkvendorrpm"
# Author: Gratien D'haese
# License: see COPYING file
# Last Update: $Id: mkvendorrpm 736 2011-11-22 09:21:35Z gdha $

################### FUNCTIONS ##################
function rpmtopdir () {
# purpose is to translate %_topdir (used by rpmbuild) into valid dir-path
if [ -f $HOME/.rpmmacros ]; then
        RPM_TopDir=`grep _topdir $HOME/.rpmmacros | awk '{print $2,$3}'`
        echo ${RPM_TopDir} | grep -q HOME
        if [ $? -eq 0 ]; then
                x=`echo ${RPM_TopDir} | cut -d/ -f2`
                RPM_TopDir=$HOME/${x}
        fi
else
        RPM_TopDir=`rpmbuild --showrc | grep _topdir | grep -v "%{_topdir}" | awk '{print $3,$4}'`
        echo ${RPM_TopDir} | grep -q "^%{_usrsrc}"
        if [ $? -eq 0 ]; then
                x=`echo ${RPM_TopDir} | cut -d/ -f2`
                RPM_TopDir=/usr/src/${x}
        fi
fi
[ -d ${RPM_TopDir} ] && echo ${RPM_TopDir} || echo /usr/src/redhat
}

function getVERSION () {
# extract version number from rear script or use the one given (via the environment)
if ! test "$VERSION" ; then
	VERSION=$(grep ^VERSION= ${rear_root}/usr/sbin/rear | cut -d= -f2 | sed -e 's/"//g')
fi

# If VERSION is 0.0.0 then we are dealing with the trunk; try to extract current svn version
if [ "$VERSION" = 0.0.0 -a -d "$rear_root/.svn" ] ; then
	# try to set version from SVN
	svn up "$rear_root" >&/dev/null
	read junk rev < <(svn info "$rear_root" | grep ^Revision)
	VERSION="0.0.$rev"
fi
}
##################### M A I N ####################
EXITcode=0

echo "
******************************************
**	`basename $0` script for rear	**
******************************************
"
# find the full path to this script
ME="$(cd $(dirname "$0") ; pwd)/$(basename "$0")"
# Find the svnroot path for the initial tar-ball
rear_root="${ME%/usr/share/rear/contrib/mkvendorrpm}"
echo The root of rear SVN directory is $rear_root

if [ ! -d $rear_root ]; then
	EXITcode=1
	exit $EXITcode
fi

# get VERSION nr of rear (via function call)
getVERSION
echo Building RPMs for rear VERSION $VERSION

prod_ver="rear-$VERSION"
distarchive="/tmp/${prod_ver}.tar.gz"

BUILD_DIR=/tmp/rear.$$
rm -rf $BUILD_DIR
mkdir -p $BUILD_DIR/$prod_ver -v
echo "Will be using a temporary BUILD_DIR=${BUILD_DIR}"

echo "Creating archive '$distarchive'"
# use tar to copy the current rear while excluding development and obsolete files
cd $rear_root
tar --exclude=hpasmcliOutput.txt --exclude=\*~ --exclude=\*.rpmsave\* \
		--exclude=\*.rpmnew\* --exclude=.\*.swp --exclude .svn -c \
		. | tar -C $BUILD_DIR/$prod_ver -x

cd -

# cleaning up the $BUILD_DIR/$prod_ver directory before starting rpmbuild
echo Getting the $BUILD_DIR/$prod_ver directory ready for rpmbuild

pushd $BUILD_DIR/$prod_ver

# following variables are the base directories of rear
SHARE_DIR=/usr/share/rear
CONFIG_DIR=/etc/rear
VAR_DIR=/var/lib/rear
SBIN_DIR=/usr/sbin

# gentoo build file
test -s .$SHARE_DIR/contrib/rear-$VERSION.ebuild ||\
	mv -v .$SHARE_DIR/contrib/rear-*.ebuild .$SHARE_DIR/contrib/rear-$VERSION.ebuild
# copy doc files
cp -rv .$SHARE_DIR/{COPYING,README,AUTHORS,TODO,doc,contrib}  .
# convert files to UTF-8 if possible
for file in COPYING README AUTHORS TODO `echo doc/*` ; do
	mv "${file}" timestamp
	iconv -f ISO-8859-1 -t UTF-8 -o "${file}" timestamp
	touch -r timestamp "${file}"
done
rm -f timestamp

# insert correct vesion nr into main rear script
sed -i -e "s/^VERSION=\".*\"/VERSION=\"$VERSION\"/" .$SBIN_DIR/rear

# grab the correct rear.spec file according the OS_VENDOR we running this script on
OS_VENDOR="$(lsb_release -i -s | tr -s " \t" _)"
cp .$SHARE_DIR/lib/spec/$OS_VENDOR/rear.sourcespec .$SHARE_DIR/lib/rear.spec
sed -i -e "s/Version:.*/Version: $VERSION/" .$SHARE_DIR/lib/rear.spec
chmod 644 .$SHARE_DIR/lib/rear.spec


# write out standard site.conf and local.conf and templates

#cat >./$CONFIG_DIR/site.conf <<EOF
# site.conf
# another config file that is sourced BEFORE local.conf
# could be used to set site-wite settings
# you could then distribute the site.conf from a central location while you keep
# the machine-local settings in local.conf
#EOF

cat >./$CONFIG_DIR/local.conf <<EOF
# sample local configuration

# Create Rear rescue media as ISO image
OUTPUT=ISO

# optionally define (non-default) backup software, e.g. TSM, NBU, DP, BACULA
# BACKUP=TSM

# the following is required on older VMware VMs
MODULES_LOAD=( vmxnet )

# to see boot messages on the serial console (uncomment next line)
# KERNEL_CMDLINE="console=tty0 console=ttyS1"
EOF

tee ./$CONFIG_DIR/templates/PXE_pxelinux.cfg >/dev/null <<EOF
default hd
prompt 1
timeout 100

label hd
localboot -1
say ENTER - boot local hard disk
say --------------------------------------------------------------------------------
EOF

popd

echo Creating final $distarchive of rear version $prod_ver
tar -C $BUILD_DIR -czf $distarchive $prod_ver

# ready for the rpmbuild
RPM_TopDir=`rpmtopdir`	# find rpmbuild %{_topdir} path
cp -fpv $BUILD_DIR/$prod_ver/usr/share/rear/lib/rear.spec ${RPM_TopDir}/SPECS/rear.spec
chmod 644 ${RPM_TopDir}/SPECS/rear.spec
cp -fpv $distarchive ${RPM_TopDir}/SOURCES/
rpmbuild -ba -v ${RPM_TopDir}/SPECS/rear.spec 2>&1 > /tmp/rpmbuild.log
EXITcode=$?

echo The rpmbuild output has been saved in the file /tmp/rpmbuild.log
# write the target RPMs build to the output
grep Wrote: /tmp/rpmbuild.log

if [ $EXITcode -eq 0 ]; then
	rm -rf $BUILD_DIR	# remove the build directory
fi
exit $EXITcode
