#!/bin/sh
#######################################################
#
# makeosconf 2.22
#
# Tries to determine the OS settings for the development
# tools.
# 
# If it fails to determine the OS, it uses unknown
# generic settings.
#
# $1 - Makefile 
#
#######################################################

echo "MakeOSConf 2.22"

##### Determine options

USECC=0
USELEX=0

for o in ${OPTIONS:=""}
do
  if [ "$o" = "--use-cc" ]; then
    USECC=1
    echo "--use-cc specified"
  elif [ "$o" = "--use-lex" ]; then
    USELEX=1
    echo "--use-lex specified"
  fi
done

##### Determine my directory

NAME=`basename $0`
DIR=`echo $0 | sed -e s/$NAME\$//`
if [ "$DIR" = "" ]; then
  DIR="."
fi

##### Check for the existance of the OS directory
##### in the directory where I'm run from

DIR="$DIR/Makefile.os"
if [ ! -d $DIR ]; then 
  echo "Can't find OS dir for makefile templates!"
  exit
fi

##### Determine the OS used

OS=`uname | sed -e s/-//g`
if [ ! -r $DIR/$OS ]; then
  OS=UNKNOWN
fi
echo "Generating configuration for OS=$OS"

##### cat the OS configuration

CC=$OS
if [ $USECC = 0 ]; then
  T=`which gcc | grep -i no\  | grep -i in\  `
  if [ "$T" = "" ]; then
    echo "gcc found, using gcc"
    CC=$OS.gcc
  fi
fi

LEX=$OS.lex
if [ $USELEX = 0 ]; then
  T=`which flex | grep -i no\  | grep -i in\  `
  if [ "$T" = "" ]; then
    echo "flex found, using flex"
    LEX=$OS.flex
  fi
fi

##### Now make the OS configuration 


echo "####BEGIN_OS_CONF"  	>>$1
echo "#cat $DIR/$LEX"		>>$1
cat $DIR/$LEX			>>$1
echo "#cat $DIR/$CC"		>>$1
cat $DIR/$CC			>>$1
echo "####END_OS_CONF"		>>$1

#### we're ready

echo ""

