#!/bin/sh

# This has been hacked down to remove support for
# relocatability

echo "You should be running this in the source directory."
echo "The binaries should be built and the directory"
echo "otherwise clean.  It should be such that the disk"
echo "usage now, minus the disk usage after doing a"
echo "\"make clean\" will be the size-to-build"
echo "If not, you better quit, otherwise hit enter."
read ANS

DIR=$(basename $PWD)
ORIGPWD=$PWD
NAME=${DIR%-*}
VERSION=${DIR##*-}
SOURCE_PREFIX=${PWD%/*}

mkdir RPP
cd RPP
touch binary-description
touch source-description
touch binary-list
touch source-list
touch size-to-build

echo $NAME > name
echo $VERSION > version

echo "N" > binary-locate
echo "RBNR" > source-locate

echo -n "Binary prefix : "
read ANS
echo $ANS > binary-prefix
#echo -n "Source prefix : "
#read ANS
#echo $ANS > source-prefix
echo $SOURCE_PREFIX > source-prefix
echo "Source prefix set to $SOURCE_PREFIX"

#echo -n "Dependencies : "
#read ANS
echo "" > dependencies

# source-after-install
# ANS=0
# while [ $ANS -lt 1 -o $ANS -gt 3 ]; do
# 	echo "Choose a source-after-install script:"
# 	echo "1 - Standard GNU configure"
# 	echo "2 - Simple sed replacement of @@@prefix@@@ on Makefile.rpp"
# 	echo "3 - None"
# 	read ANS
# done
# if [ $ANS -ne 3 ] ; then
# 	echo "#!/bin/sh" > source-after-install
# 	echo "" >> source-after-install
# 	echo "PREFIX=\$1" >> source-after-install
# 	echo "" >> source-after-install
# 	echo "cd \$PREFIX/$DIR" >> source-after-install
# 	echo "if [ \$PWD != \$PREFIX/$DIR ]; then" >> source-after-install
# 	echo "	echo \"Bailing... directory \$PREFIX/$DIR does not exist!\"" >> source-after-install
# 	echo "	exit 1" >> source-after-install
# 	echo "fi" >> source-after-install
# 	echo "touch .\$\$bogus" >> source-after-install
# 	echo "if [ ! -f .\$\$bogus ]; then" >> source-after-install
# 	echo "	echo \"Bailing... directory \$PREFIX/$DIR not writeable!\"" >> source-after-install
# 	echo "	exit 1" >> source-after-install
# 	echo "fi" >> source-after-install
# 	echo "rm -f .\$\$bogus" >> source-after-install
# 	echo "echo \"Configuring $DIR for prefix \${PREFIX%%/src*}\"" >> source-after-install
# fi
# case $ANS in
#     1)
# 	ANS=0
# 	while [ $ANS -lt 1 -o $ANS -gt 2 ]; do
# 	    echo "Choose an option:"
# 	    echo "1 - Run configure --prefix=@@@prefix@@@ --no-create, and generate script"
# 	    echo "2 - Just generate the script, everything is already set"
# 	    read ANS
# 	done
# 	if [ $ANS = "1" ]; then
# 		cd ..
# 		echo "Running configure"
# 		./configure --prefix=@@@prefix@@@ --no-create
# 		cp config.status config.status.rpp
# 		cd RPP
# 	fi
# 	
# 	# Generate the script
# 	echo "sed \"s^@@@prefix@@@^\${PREFIX%%/src*}^\" < config.status.rpp > config.status" >> source-after-install
# 	echo "chmod +x config.status" >> source-after-install
# 	echo "./config.status" >> source-after-install
# 	chmod +x source-after-install
# 	echo "Created source-after-install for standard GNU configure"
# 
# 	if [ $ANS = "1" ]; then
# 		echo "Running the after-install script to generate and exec config.status"
# 		./source-after-install ${ORIGPWD%%/$DIR}
# 		echo "after-install script done."
# 	fi
# 	;;
#     2)
# 	echo "sed \"s^@@@prefix@@@^\${PREFIX%%/src*}^\" < Makefile.rpp > Makefile" >> source-after-install
# 	chmod +x source-after-install
# 	echo "Created source-after-install for simple Makefile.rpp"
# 	;;
#     3)
# 	echo "No source-after-install created"
# esac

# size-to-build
echo -n "Standard size-to-build method (with \"make clean\")? y/n : "
read ANS
if [ $ANS = "y" ] ; then
	cd ..
	AFTER=$(du -bs | awk '{print $1}')
	make clean
	BEFORE=$(du -bs | awk '{print $1}')
	expr $AFTER - $BEFORE > RPP/size-to-build
	echo -n "Size to build : "
	cat RPP/size-to-build
	cd RPP
else
	echo "Don't forget to edit size-to-build"
fi

# source-list
cd ../..
find $DIR > $DIR/RPP/source-list

echo "Don't forget to edit the binary-list"
echo "Don't forget to edit binary-description and source-description"
