#!/bin/bash
# Makefile.config -- script for automatic configuration of Makefile variables
# Copyright (C) 2002-2003 Stefan Ziegenbalg

configfile=Makefile.conf

# nice output routines
#    \033          is just ascii ESC
#    \033[<NUM>G   move to column <NUM>
#    \033[1m       switch bold on
#    \033[31m      switch red on
#    \033[32m      switch green on
#    \033[33m      switch yellow on
#    \033[m        switch color/bold off
echo_red () {
    echo -e "\033[31m\033[1m$1\033[m" >&2
}

echo_green () {
    echo -e "\033[32m$1\033[m" >&2
}

echo_yellow () {
    echo -e "\033[33m\033[1m$1\033[m" >&2
}

# read config file 
readconfig () {
    result="-"
    read var par
    while [ "$result" = "-" -a "$var" != "" ]; do
	[ "$var" = "$1" ] && result=$par
	read var par
    done
    echo $result
}
 
# remove temporay files 
rmtmp () {
    rm -f $tmpfile $tmpfile.*
}

# checks coompilinmg + running
# $1 compiler command + flags
# $2 test file (without .pas) 
checkcr () {
    echo "$1 $2" >> $tmpfile.log
    $1 $2 >> $tmpfile.log 2>> $tmpfile.log
    if [ "$?" = "0" ]; then
      echo "./$2" >> $tmpfile.log
      ./$2
    else 
      return 1  
    fi
}

# checks command
findexec () {
    result=""
    for i; do
      if [ "$result" = "" ]; then
        if [ "${i#*/}" = "$i" ]; then 
    	    which $i >> $tmpfile.log 2>> $tmpfile.log
	    [ "$?" = "0" ] && result=$i
	else
	    [ -x $i -a ! -d $i ] && result=$i
	fi
      fi	
    done
    echo $result
}


failure () {
    echo -n $failreturn
    exit 1
}    

#####################
##### main part #####
#####################
failreturn=""
case "$1" in
  "pc2") cmd=pc; failreturn="ppc386" ;;
  "install2") cmd=install; failreturn="install" ;;
  "findexec2") cmd=findexec; failreturn=${2%% *} ;;
  *) cmd=$1 ;;
esac

case "$cmd" in
    "findexec")	 varname=$cmd_$2 ;;
    "have_unit") varname=have_$3 ;;
    "unit_flags") varname=$3"_flags" ;;
    *) varname=$cmd ;;
esac
tmpfile="conf_$varname"

result="-"
[ -f $configfile -a "$varname" != "" ] && result=`readconfig $varname < $configfile` 

if [ "$result" != "-" ]; then
    echo $result
    exit 0
fi

result=""
rm -f $tmpfile.log
case "$cmd" in
    "pc")
        echo -n "Checking for freepascal compiler (FLAGS=\"$2\"): " >&2
	echo -e "begin\nend." > $tmpfile.pas
	for i in ppc386 fpc pc $3; do
	    if [ "$result" = "" ]; then
    		if [ "${i#*/}" = "$i" ]; then 
    		    which $i >> $tmpfile.log 2>> $tmpfile.log
		    [ "$?" = "0" ] && checkcr "$i $2" $tmpfile && result=$i
		else
		    [ -x $i -a ! -d $i ] && checkcr "$i $2" $tmpfile && result=$i
		fi
            fi
	done       
	if [ "$result" != "" ]; then
	    echo_green "$result"
	    rmtmp
	else 
	  echo_red "not found"
	  failure
	fi
    ;;  
    "install")
        echo -n "Checking for install command: " >&2
	result=`findexec install ginstall $2`
	if [ "$result" != "" ]; then
	    echo_green "$result"
	    rmtmp
	else 
	  echo_red "not found"
	  failure
	fi
    ;;
    "have_unit" | "unit_flags" )
        echo -n "Checking for $3 unit: " >&2
	echo -e "uses $3;\nbegin\nend." > $tmpfile.pas
	for i in $4 " "; do
	  if [ "$result" = "" ]; then
	    if [ "$i" = " " ]; then
     	      checkcr "$2" $tmpfile && result=$i
	    else   
     	      [ -f $i/$3.ppu ] && checkcr "$2 -Fu$i" $tmpfile && result=$i
	    fi
	  fi       
	done
	if [ "$result" != "" ]; then 
	  if [ "$result" = " " ]; then
	    echo_green "available"
	  else
	    echo_green "$result"
	    result="-Fu$result"
	  fi    
	  if [ "$1" = "have_unit" ]; then
            echo "$3""_flags $result" >> $configfile 
	    result=1
	  else 
            echo "have_$3 1" >> $configfile 
	  fi
	  rmtmp
	else 
	  echo_yellow "not available"
          echo "$3""_flags " >> $configfile 
	  if [ "$1" = "have_unit" ]; then
	    result=0
	  else 
            echo "have_$3 0" >> $configfile
	    failure	  
	  fi
	fi
    ;;  
    "optalign")
        echo -n "Checking for -Oa flag: " >&2
	echo -e "begin\nend." > $tmpfile.pas
	checkcr "$2 -Oa" $tmpfile
	if [ "$?" = "0" ]; then 
	  echo_green "works"
	  result=1
	  rmtmp
	else 
	  echo_yellow "failed"
	  result=0
	fi
    ;;  
    "findexec")
        echo -n "Checking for $2: " >&2
	result=`findexec $3`
	if [ "$result" != "" ]; then
	    echo_green "$result"
	    rmtmp
	else 
	  echo_red "not found"
	  failure
	fi
    ;;
    *)
	echo "$0 -- script for automatic configuration of Makefile variables"
	echo
	echo
	echo "    !! Run make to build the project or run ./update to update the Makefile (e.g. after changing sources)!!"
	echo
	echo
	echo "Usage: $0 <variable> [options]"
	echo ""
	echo "variables:"
	echo "  pc [compiler flags [additional compilers]]"
	echo " 	  checks for freepascal compiler, returns \"\" on failure"
	echo ""
	echo "  pc2 [compiler flags [additional compilers]]"
	echo " 	  as above, but ppc386 is returned on failure"
	echo ""
	echo "  install [additonal commands]"
	echo "    checks for install command, returns \"\" on failure"
	echo ""
	echo "  install2 [additonal commands]"
	echo "    as above, but install is returned on failure"
	echo ""
	echo "  have_unit <compiler command> <unit name> [<alternate locations>]"
	echo "    checks whether <unit list> is available; returns 0 on failure, 1 on success; unit list must be coma separated"
	echo ""
	echo "  unit_flags <compiler command> <name of unit package> <unit list> [<alternate locations>]"
	echo "    as above, but returns \"-ul<location>\" if unit found in [<alternate locations>]"
	echo ""
	echo "  optalign <compiler command>"
	echo "    checks whether -Oa flag works"
	echo ""
	echo "  findexec <name> <list variants>"
	echo "    checks for existing executable in <list variants>"
	echo ""
	echo "  findexec2 <name> <list variants>"
	echo "    as above, but first variant is returned on failure"
	echo ""
	exit 1
    ;;
esac	

[ "$result" != "" -a "$varname" != "" ] && echo "$varname $result" >> $configfile 
echo $result

