#! /bin/sh

tmp=/tmp/$$
status=0
prog=`basename $0`
trap "rm -f $tmp.*; exit \$status" 0 1 2 15
here=`pwd`

_usage()
{
    cat << EOF
Usage: $prog [-r mod] modlist workarea git-tree log

-r mod    Restart with the mod after this one

modlist   Output from modlist_sort
workarea  ptools workarea
git-tree  GIT tree
log       File to log progress, always appends
EOF
    exit 1
}

modlist=
ism=
git=
first=
log=
args=$*
commitset="$here/commitset"

while getopts "r:" c
do
    case $c in
	r) first=$OPTARG;;
	\?) _usage
	    exit 1
	    break;;
    esac
done

shift `expr $OPTIND - 1`

if [ $# -ne 4 ]
then
    _usage
    exit 1
fi
modlist=$1
ism=$2
git=$3
log=$4

if [ ! -r $modlist ]
then
    echo "$prog: Unable to read $modlist"
    exit 1
fi

if [ ! -d $ism ]
then
    echo "$prog: Unable to open $ism"
    exit 1
fi

if [ ! -d $git ]
then
    echo "$prog: Unable to open $git"
    exit 1
fi

if [ ! -f $log ]
then
    touch $log
fi

# get directory from where this command is coming from
# that is where the modlist_exceptions file should be 
miscdir=`which $0 | sed -e 's#[^/]*$##'`
exceptions=$miscdir/modlist_exceptions
if [ ! -f $exceptions ]
then
    echo "$prog: Unable to open $exceptions"
    exit 1
fi
cd $miscdir
state=`p_state $exceptions 2>/dev/null`
if ! echo $state | grep -q modified 
then
    echo ""
    echo "WARNING: *** $exceptions is not in modified state ***" | tee -a $log
    echo ""
fi
cd $here

# if we aren't continuing and we are starting from beginning
# then remove the commitset
# so that we don't concat on to something that is old
if [ -z "$first" ]
then
    rm -f $commitset
fi

echo `date`": $prog $args" >> $log
echo "" >> $log


for m in `cat $modlist`
do
    eval `echo $m | awk -F':' '{printf("mod=%s:%s:%s\norder=%s\n", $1, $2, $3, $4)}'`

    # Find first mod
    echo ""
    if [ "X$first" != "X" ]
    then
	echo "$mod: Restarting"
	echo "$mod Restarting" >> $log
	if [ "$mod" = "$first" ]
	then
	    first=
	fi
	continue
    fi

    # Check that it hasn't already been applied
    if [ $order -gt 0 ]
    then
	echo "$mod: Already applied $order, skipping"
	echo "$mod Already Applied" >> $log
	continue
    fi

    echo "$mod:"
    cd $ism
    export WORKAREA=`pwd`
    p_modinfo -m $mod
    cd $here

    echo ""
    echo -n "(c)ontinue (s)kip e(x)it [c]: "
    read ans

    if [ "X$ans" = "Xs" ]
    then
	echo -n "Skipping why?: "
	read ans
	echo "$mod Skipping because $ans" >> $log

	echo "Appending $mod to $exceptions" | tee -a $log
	echo "" >>$exceptions
	echo "# $ans" >>$exceptions
	echo "$mod" >>$exceptions

	continue
    fi
    
    if [ "X$ans" = "Xx" ]
    then
	status=1
	break
    fi

    cd $ism
    export WORKAREA=`pwd`
    p_mod2git $git $mod $commitset
    success=$?
    cd $here

    if [ $success -eq 0 ]
    then
	echo "$mod Applied" >> $log
	# look at diffstat of the applied change
	echo "$mod Applied"
	cd $git	
	git diff --stat HEAD^ HEAD
	cd $here
    else
	echo "$prog: Failed to apply $mod"
	echo ""
	echo -n "(s)kip e(x)it [x]: "
	read ans
	if [ "X$ans" = "X" -o "X$ans" = "Xx" ]
	then	
	    echo "$mod Failed" >> $log
	    status=1
	    break
	else
	    echo -n "Skipping why?: "
	    read ans
	    echo "$mod Failed and skipped because $ans" >> $log
	fi
    fi
done

echo ""
cat $log
