#!/bin/sh

FILES="makeExtension makeIncludes MakefileInit"

OS=`uname`
OLD_OS=`cat .simplemake/OS`

COMPILE=0

if [ "$OS" != "$OLD_OS" ]; then
  echo "I need to bootstrap, $OS!=$OLD_OS"
  COMPILE=1
else 
  for f in $FILES
  do
    if [ ! -x .simplemake/$f ]; then
      echo "I need to bootstrap, .simplemake/$f is not executable"
      COMPILE=1
    fi
  done
fi

if [ $COMPILE = 1 ]; then

  echo "Creating OS dependencies..."

  NETFLAG=""
  YACCFLAG=""
  CFLAG=""
  SHL_LIB=""
  LIB=""
  INCL=""

  .simplemake/makeosconf /tmp/makebootstrap.$$.1
  cat /tmp/makebootstrap.$$.1 | \
    sed -e s/[\(]//g | \
    sed -e s/[\)]//g | \
    sed -e s/=/=\"/g | \
    sed -e s/\$/\"/g | \
    sed -e s/\"\"/\"/g \
       >/tmp/makebootstrap.$$
  . /tmp/makebootstrap.$$
  rm -f /tmp/makebootstrap.$$.1
  mv -f /tmp/makebootstrap.$$ /tmp/makebootstrap

  cd .simplemake
  for f in $FILES
  do 
    echo "bootstrapping '$f'..."
    if [ -f $f.lex ]; then
      $LEX $f.lex
      $CC -o $f lex.yy.c $LIBS
      rm -f lex.yy.c lex.yy.o
    elif [ -f $f.c ]; then
      $CC -o $f $f.c $LIBS
      rm -f $f.o
    else 
      echo "Fatal: Can't find needed source"
      exit 2
    fi
    strip $f >/dev/null 2>&1
  done
  cd ..

  echo "done."
fi

uname >.simplemake/OS

