#!/bin/sh

#
# generate a makefile from makefile.raw that is pre-tailored to certain hosts
# 
# Each command-line arguement is used to filter out particular settings.
# These settings are controlled by <hash>name<hash>
#


# c++        = sun3 OS 3.0 with ATT C++
# sunos-4.0  = sun3 OS 4.0 with some GNU hacks in include files
# vax-bsd4.3 = vax 780 running BSD 4.3 (gcc-1.35 -O breaks on place.c)
# hpux       = HP300 running HPUX 7.0
# msdos      = pc/at running MSDOS 3.2
# turboc-2.0 = Turbo C 2.0 compiler
# ultrix     = decstation running ultrix at ifi (Bassen)
# linux	     = linux on Intel platform


case $# in
  0) echo "usage:  $0 hosttype [hosttype2 ...]"
     echo "   where valid hosttypes are:"
#     grep "#.*# " makefile.raw | cut -f1 -d' ' | sort | uniq | sed -e 's/#//g'
     grep "#.*# " makefile.raw | awk '{print $1}' | \
                   sort | uniq | sed -e 's/#//g'
     echo
     echo "Specify as many hosttypes as needed."
     exit 1 ;;
  *) ;;
esac

echo "Creating makefile for host: $@"

if [ -f makefile ]; then
  echo "Editing existing makefile.  Saving copy in makefile.old"
  mv makefile makefile.old
  cp makefile.old makefile
else
  if [ -f makefile.raw ]; then
    echo "No makefile exists.  Copying makefile.raw to makefile"
    cp makefile.raw makefile
  else
    if [ -f ../makefile.raw ]; then
      echo "No makefile exists.  Copying ../makefile.raw to makefile"
      sed -e "s/VPATH=./VPATH=../g" < ../makefile.raw > makefile
    else
      echo "Unable to find makefile or makefile.raw.  Exiting"
      exit -1
    fi
  fi
fi

for hosttype do
  sed -e "s/#$hosttype# *//g" < makefile > makefile.tmp
  mv makefile.tmp makefile
done

# now document what switch settings were actually used
mv makefile makefile.tmp
echo '# Makefile created with HOSTTYPEs:' > makefile
for hosttype do
  echo '# ' $hosttype >> makefile
done
echo ' ' >> makefile
cat makefile.tmp >> makefile
rm makefile.tmp
