#! /bin/sh

set -e
#set -x

if [ $# -ne 1 ]; then
 echo "error: no reset hard hash provided; check git log and get the last suitable hash"
 exit 1
fi

present="ampolish3.no-preinstall inc-mover patch-build um4 inc-src-patch.diff"

for p in $present
do
 if [ ! -f $p ]; then
  echo "error: mandatory file not found: $p"
  exit 1
 fi
done

git reset --hard $1

pwd_escaped=`echo $PWD | sed -e 's/\//\\\\\//g'`
cat ampolish3.no-preinstall | \
  sed -e 's/my \$audit_file = 0;/my \$audit_file = 1;/g' \
      -e "s/my \$inc_list = .*/my \$inc_list = \'$pwd_escaped\/inc-list\.txt\';/g" > ampolish3.tmp
chmod +x ampolish3.tmp
rm ampolish3 && mv ampolish3.tmp ampolish3

rm -f inc-list.txt
./bootstrap -p
if [ ! -f inc-list.txt ]; then
 echo "error: ampolish3 ok? No inc data found"
 exit 1
fi

./inc-mover inc-list.txt
./h-move

git rm c/src/aclocal/bsp-linkcmds.m4

./patch-build > build-patch.txt
./um4

patch -p1 < inc-src-patch.diff

/opt/work/chris/rtems/rsb/rtems-source-builder.git/source-builder/sb-bootstrap

exit 0
