#!/bin/sh
#
# p11 - pdp11 emulator; Copyright (C) 1994 Hartmut Brandt, Joerg Micheel 
# see the file LICENSE for further information
#
#
# the configurations in this list must be separated by spaces
#
CONFIGS="i386-bsdi sparc-sunos sparc-solaris alpha-osf1"

#
# need an ANSI-CPP which doesn't include any system files or define variables
#
#CPP="cccp -P -undef -nostdinc"
CPP="gcc -x c -P -undef -nostdinc -E"

VERBOSE=No
USE_SAVED=No
GENERIC=No


echo2() {
	echo 1>&2 "$*"
}

#
# print help
#
help() {
	echo2 "Usage: config [flags] machine-system"
	echo2
	echo2 "known configurations:"
	for i in ${CONFIGS} ; do
		echo2 "	$i"
	done
	echo2
	echo2 "flags are:"
	echo2 "	-a, --auto)	try autoconfiguration"
	echo2 "	-h, --help)	print this info"
	echo2 "	-v, --verbose)	trace execution"
	echo2 "	-s, --saved)	reconfigure using saved configuration"
	echo2 "	-g, --generic)	use generic code instead of machine specific"
	echo2 "	-p, --print)	show last configuration"
	exit 1
}

error() {
	echo 1>&2 "$*"
	exit 2
}
#
# autoconfiguration
#
auto()
{
	os=`uname -s`
	osmajor=`uname -r | sed 's/\..*$//'`

	case $os in
	  SunOS)
		if [ $osmajor -gt 4 ]; then
			proc=`uname -p`
			echo $proc-solaris
		else
			proc=`mach`
			echo $proc-sunos
		fi
		;;
	  BSD/386)
		proc=`uname -m`
		echo $proc-bsdi
		;;
	  OSF1)
		proc=`uname -m`
		echo $proc-osf1
		;;
	  *)
		echo unknown-unknown
		;;
	esac
}
#
# link $2 to $1 if exists else $3 to $1
#
opt_link() {
	if [ $# -ne 3 ] ; then error "Bad call to opt_link()" ; fi
	if [ -f $1 ] ; then error "opt_link() - file exists" ; fi
	if [ -f $2 ] ; then
		ln -s $2 $1
    		if [ ${VERBOSE} = Yes ] ; then echo "linking $2 to $1" ; fi
	else
		ln -s $3 $1
    		if [ ${VERBOSE} = Yes ] ; then echo "linking $3 to $1" ; fi
	fi
}
#
# link $2 to $1
#
make_link() {
	if [ $# -ne 2 ] ; then error "Bad call to make_link()" ; fi
	if [ -f $1 ] ; then error "make_link() - file exists" ; fi
	if [ -f $2 ] ; then
		ln -s $2 $1
    		if [ ${VERBOSE} = Yes ] ; then echo "linking $2 to $1" ; fi
	else
		error "$2: file does not exist"
	fi
}

#
# parse and shift off options
#
while [ $# -ne 0 ] ; do
  case $1 in
  -a|--auto)
	auto >.config
	USE_SAVED=Yes
	;;
  -h|--help)
	help
	exit 1
	;;
  -v|--verbose)
	VERBOSE=Yes
	;;
  -s|--saved)
	USE_SAVED=Yes
	;;
  -g|--generic)
	GENERIC=Yes
	;;
  -p|-print)
	if [ -f .config ] ; then
	  cat .config
	  exit 0
    	else
	  error "Saved configuration file not found"
        fi
	;;
  -*)	error "Unknown flag $1"
	;;
  *)	break
	;;
  esac
  shift
done

#
# there must be exactly one arg left
#
if [ ${USE_SAVED} = Yes ] ; then
  if [ $# -ne 0 ] ; then
    error "no argument needed for -s or -a"
  fi
  if [ -f .config ] ; then
    CONF=
    if read <.config CONF XGENERIC; then
      if [ "${CONF}" = "" ] ; then
        error "can't read saved configuration file"
      fi
      if [ "${XGENERIC}" = "Yes" ]; then
        GENERIC=Yes
      fi
    else
      error "can't read saved configuration file"
    fi
  else
    error "Saved configuration file not found"
  fi
else
  if [ $# -ne 1 ] ; then
    help
  else
    CONF=$1
  fi
fi

#
# check if configuration is ok
#
case ${CONF} in
  *-*)	;;
  *)	error "bad configuration: need machine-system"
	;;
esac

CONFIG=""
for i in ${CONFIGS} ; do
  if [ /$i/ = /${CONF}/ ] ; then
    CONFIG=$i
    break
  fi
done

if [ /${CONFIG}/ = // ] ; then
  error "unknown configuration ${CONF}. Use 'config -help' to get a list."
fi

#
# parse of parts
#
OIFS=${IFS}
IFS=-
set ${CONF}
MACHINE=${1}
SYSTEM=${2}
IFS=${OIFS}

echo machine: ${MACHINE}
echo system:  ${SYSTEM}

#
# make links (remove existing ones)
# use generic files if specific doesn't exist
#
rm -f system.h system.c machine.h Makefile.mach regi.h float.h

make_link Makefile.mach	Config/M-${MACHINE}-${SYSTEM}
make_link system.h	Config/s-${SYSTEM}.h
make_link system.c	Config/s-${SYSTEM}.c

if [ ${GENERIC} = Yes ] ; then
	make_link  machine.h	Config/m-generic.h
	make_link  regi.h	Config/r-generic.h
	make_link  float.h	Config/f-generic.h
else
	opt_link  machine.h	Config/m-${MACHINE}.h	Config/m-generic.h
	opt_link  regi.h	Config/r-${MACHINE}.h	Config/r-generic.h
	opt_link  float.h	Config/f-${MACHINE}.h	Config/f-generic.h
fi

#
# directory for p11 includes and makefile cpp-includes
INCL=`pwd`

#
# generate Makefiles
#
FILES=`find . -name Makefile.in -print`


for i in ${FILES} ; do
  DIR=`dirname $i`
  (
    cd ${DIR}
    if [ ${VERBOSE} = Yes ] ; then
      pwd
      set -x
    fi
    ${CPP} -I${INCL} -DMAKE_CONFIG="${CONF}" -DMAKE_P11INCL=\'\"${INCL}\"\' Makefile.in >Makefile
  )
done

#
# generate config saved configuration file
#
echo ${MACHINE}-${SYSTEM} ${GENERIC} >.config

echo "Don't forget to 'gmake deps'."
