#!/bin/bash

MODE=$1
PROJECT=$2

if [ "$MODE" == "" -o "$PROJECT" == "" ]; then
	echo -e "\n\tUsage: $0 mode path\n"
	echo -e "\tmode\torg or diff according to your needs"
	echo -e "\tpath\tyour project path\n"
	exit 1
fi

SPATCHVER=`spatch -version 2>&1 | sed "s|spatch version \([^ ]*\) .*|\1|"`
WITHPYTHON=`spatch -version 2>&1 | sed "s|spatch version \([^ ]*\) with Python support|yes|"`
WITHPYTHON=`echo $WITHPYTHON | sed "s|spatch version \([^ ]*\) without Python support||"`

echo "Using spatch version $SPATCHVER"
#TODO: Add version check

if [ "$WITHPYTHON" ] ; then
    echo "Your version has been build with Python support"
else
    echo "Your version has no Python support"
    if [ "org" == "$MODE" ] ; then
	echo "org mode requires Python support."
	exit 1
    fi
fi
echo

echo "Using PREFIX="${PREFIX:=`pwd`}
echo "Using PROJECT="${PROJECT:=$PREFIX/project}
echo "Using COCCI="${COCCI:=$PREFIX/cocci}
echo "Using RESULTS="${RESULTS:=$PREFIX/results}

if [ "`which glimpse`" ]; then
    FLAGS=${SPFLAGS:="-timeout 60 -use_glimpse"}
else
    FLAGS=${SPFLAGS:="-timeout 60"}
fi
SPFLAGS="$FLAGS -D $MODE"
echo "Using SPFLAGS="$SPFLAGS

echo -e "\nFor efficiency, run \`<coccinelle dir>/scripts/glimpseindex_cocci.sh\`"
echo -e "in $PROJECT"
echo -e "NB: glimpse is available at http://webglimpse.net/ but it is not under the GPL.\n"

sed -i "s|^PREFIX?=.*$|PREFIX?=${PREFIX}|" Makefile
sed -i "s|^PROJECT?=.*$|PROJECT?=${PROJECT}|" Makefile
sed -i "s|^COCCI?=.*$|COCCI?=${COCCI}|" Makefile
sed -i "s|^RESULTS?=.*$|RESULTS?=${RESULTS}|" Makefile
sed -i "s|^SPFLAGS?=.*$|SPFLAGS?=\"${SPFLAGS}\"|" Makefile

