##!/bin/bash
# 
#  Script to auto-generate rpms from stap scripts
#


#Checking if script is run by root
if [ $EUID -ne 0 ]
then
	echo "ERROR :You need to be root to run this !"
	exit
fi

prog=stap-buildpkg

echo_usage () {
  echo $"Usage: $prog scriptfile [options] [stap-options]"
  echo $"Options:"
  echo $"       -n pkg_name   		: specify a package name"
  echo $"       -v pkg_version		: specify a package version"
  echo $"       -c pkg_config  		: provide config parameters through a file"
  echo $"       -p post_process script   : post processing script"
  echo $"       -h help_config		: help text for config parameters"
  echo $"       All additonal options will be passed to the stap command"
}

parse_args() {
while [ -n "$1" ]; do
     case "$1" in
        -n)                    PKG_NAME="$2"
				shift 2
				echo $PKG_NAME
                                ;;
        -v)			PKG_VERSION=$2
                                shift 2
				echo $PKG_VERSION
                                ;;
        -c) 		        PKG_INPUT_CONFIG=$2
				echo $PKG_INPUT_CONFIG
				shift 2
                                ;;
        -p)			PKG_POST_SCRIPT=$2
				echo $PKG_POST_SCRIPT
				shift 2
                                ;;
        -H) 		        PKG_HELP_CONFIG=$2
				echo $PKG_HELP_CONFIG
                                shift 2
                                ;;
        *)                     STAP_OPTIONS=$@ 
				echo $STAP_OPTIONS
                                shift $#
esac
done
}

validate_args() {
 if test -z $PKG_NAME; then
   PKG_NAME=`basename $script .stp`
 fi
 if test -z $PKG_VERSION; then
   PKG_VERSION=1.0
 fi
 if test -z $PKG_RELEASE; then
   PKG_RELEASE="0"
 fi
 PKG_ARCH=`uname -i`
}

module_gen() {
stap -p4 -k -m $PKG_NAME.ko $script $STAP_OPTIONS
if [ -f $PKG_NAME.ko ]
then
        echo " Module $PKG_NAME.ko generated successfully ";
else
        echo " Module $PKG_NAME.ko generation failed. Exiting now... "
        rm -rf $TEMPDIR
        exit;
fi
}

create_wrappers() {
echo "Creating RPM wrapper from template..";
echo "s/TEMPLATE_PKG_NAME/$PKG_NAME/g" > $TEMPDIR/sd-installer-patterns
echo "s/TEMPLATE_PKG_VERSION/$PKG_VERSION/g" >> $TEMPDIR/sd-installer-patterns
echo "s/TEMPLATE_PKG_RELEASE/$PKG_RELEASE/" >> $TEMPDIR/sd-installer-patterns
echo "s/TEMPLATE_ARCH/$PKG_ARCH/g" >> $TEMPDIR/sd-installer-patterns

sed -f $TEMPDIR/sd-installer-patterns template.buildpkg > $TEMPDIR/wrapper
csplit -s $TEMPDIR/wrapper '/extractor template/'
mv xx00 ${PKG_NAME}_install
mv xx01 ${PKG_NAME}_binextractor

chmod a+x ${PKG_NAME}_install
rm $TEMPDIR/sd-installer-patterns
rm $TEMPDIR/wrapper
}

copy_files_to_temp() {

runtime=systemtap-runtime
if test -z $(rpm -qa $runtime); then
 runtime=systemtap 
fi

echo "using systemtap runtime executables from $runtime rpm"

cp `rpm -ql $runtime | grep staprun | grep -v staprun.8.gz` $TEMPDIR/.
cp `rpm -ql $runtime | grep stapio` $TEMPDIR/.

echo "Copying Systemtap module.."
mv $PKG_NAME.ko $TEMPDIR/ 
echo "Copying config file.."
echo "time=2m" > $TEMPDIR/$PKG_NAME.config
if [ -f $PKG_INPUT_CONFIG ] && [ "$PKG_INPUT_CONFIG" != "" ]
then 
   cat $PKG_INPUT_CONFIG >> $TEMPDIR/$PKG_NAME.config
fi
# Copying help file, or adding an empty file if none is provided.
if [ -f $PKG_HELP_CONFIG ] && [ "$PKG_HELP_CONFIG" != "" ]
then
        cp $PKG_HELP_CONFIG $TEMPDIR/$PKG_NAME.help
else
        echo " " > $TEMPDIR/$PKG_NAME.help
fi
echo "Copying the post processing script"
# copying the post processing script, or adding an empty file if none is provided.
if [ -f $PKG_POST_SCRIPT ] && [ "$PKG_POST_SCRIPT" != "" ]
then
    cp $PKG_POST_SCRIPT  $TEMPDIR/$PKG_NAME.post
else
   echo " " > $TEMPDIR/$PKG_NAME.post
fi
chmod +x $TEMPDIR/$PKG_NAME.post
mv ${PKG_NAME}_install $TEMPDIR/
}

#------------------------------------------------------------------
# Mainline script
#------------------------------------------------------------------
script=$1
echo $script
shift 1

if test -z $script; then
 echo "Please provide a script name"
 echo_usage
 exit
fi

if [ ! -f "$script" ]; then
 echo "Script file does not exist"
 echo "Please provide a valid script name"
 echo_usage
 exit
fi

 
#OPTS=`getopt -s bash -u -o 'n:v:R:c:p:h:' -- $@`

#parse arguments
parse_args $@
validate_args

# generate a tmp directory to store all components:remember to clean it up !
TEMPDIR=/tmp/$PKG_NAME-$PKG_VERSION;
mkdir  $TEMPDIR

# Generate Stap module
echo "Generating Stap module..";
module_gen

#generate installer(wrapper) scripts
create_wrappers

#copy systemtap-runtime files
copy_files_to_temp

MY_PWD=`pwd`
cd $TEMPDIR ; tar -cjf $PKG_NAME-$PKG_VERSION.tar.bz2 *
cd $MY_PWD;

echo "Building the self-extracting binary.."
mkdir $TEMPDIR/$PKG_NAME
mv $TEMPDIR/${PKG_NAME}_install $TEMPDIR/$PKG_NAME/
tar -xjf $TEMPDIR/$PKG_NAME-$PKG_VERSION.tar.bz2 -C $TEMPDIR/$PKG_NAME/
mv  ${PKG_NAME}_binextractor $TEMPDIR/${PKG_NAME}_binextractor 2>/dev/null
#mv /tmp/$PKG_NAME-$PKG_VERSION.tar.bz2 $TEMPDIR/

MY_PWD=`pwd`
cd $TEMPDIR; 
tar -czf - $PKG_NAME/ >> ${PKG_NAME}_binextractor
cd $MY_PWD;
mv $TEMPDIR/${PKG_NAME}_binextractor ./$PKG_NAME-$PKG_VERSION
echo "The installer is at `pwd`/$PKG_NAME-$PKG_VERSION"
chmod +x $PKG_NAME-$PKG_VERSION

#Cleanup the mess in temp dir
#rm -rf $TEMPDIR
exit
