#!/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 59 2009-11-18 13:33:53Z 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
VERSION=`grep ^VERSION= ${rear_root}/usr/sbin/rear | cut -d= -f2 | sed -e 's/"//g'`
if [ "${VERSION}" = "0.0.0" ]; then
	# 0.0.0 is the version nr of the trunk; extract the version nr from CHANGES and VERSION files
	if [ -f $rear_root/usr/share/rear/VERSION ]; then
		VERSION1=`cat $rear_root/usr/share/rear/VERSION`
	else
		VERSION1="0.0.0"
	fi
	VERSION2=`grep -i ^version $rear_root/usr/share/rear/CHANGES | head -1 | awk '{print $2}'`
	VERSION=`echo -e ${VERSION1}'\n'${VERSION2} | sort -n | tail -1`
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/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,CHANGES,README,doc,contrib}  .
# convert files to UTF-8 if possible
for file in COPYING CHANGES README "`find doc -type f`" ; 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 set your backup software
# BACKUP=TSM

# the following is required on older VMware VMs
MODULES_LOAD=( vmxnet )
EOF
	
tee ./$CONFIG_DIR/templates/{PXE_pxelinux.cfg,ISO_isolinux.cfg,USB_syslinux.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
