# FUNCTION dirpatch
#     ARG-1 : DIR-NAME
#
#     RETURN: patched string 
#
#     append a slash at the end
#     cut all this-dir events
#     replace the slashes as edit-ones
function dirpatch {
	dir=$( echo $1 | sed '1,$s/$/\//g' )
	dir=$( echo $dir | sed '1,$s/\/\.\//\//g' )
	dir=$( echo $dir | sed '1,$s/\//\\\//g' )
	echo $dir
}

function dir2dosdir {
	dir=$( echo $1 | sed '1,$s/\\\//\\\\\\\\/g' )
	echo $dir
}

# FUNCTION findnpatch
#     ARG-1 : ROOT-DIR
#     ARG-2 : DIR-NAME
#
#     RETURN: patched string 
#
function findnpatch {
	dir=$(find $1 -name $2 -a -type d)
	dir=$( echo $dir | sed '1,$s/$/\//g' )
	dir=$( echo $dir | sed '1,$s/\/\.\//\//g' )
	dir=$( echo $dir | sed '1,$s/\//\\\//g' )
	echo $dir
}
  
echo THIS IS THE SETUP-TOOL FOR THE RCFS-FILESYSTEM
echo
echo please enter the correct numbers, if requested !
echo you have to enter a value ! [VALUES] are for default !
echo
echo ALL DIRECTORYs SHOULD USE THE SLASH NOT THE BACKSLASH LIKE DOS DOES !
echo
echo
echo COMPILER AREA
echo =============
echo
echo 'please enter the compiler's directory, where the include'
echo 'and bin dir's are placed !!!!'
echo 'NOTE : dont enter a slash at the end !!!!!!!!! '
read CC_HOME?"compiler-directory (if needed) : "
echo
CC_HOME=$(find $CC_HOME -level 0 -a -type d)
CC_HOME=$(dirpatch $CC_HOME)
DCC_HOME=$(dir2dosdir $CC_HOME)
echo
echo
read bc_flag?"do you use a borland compiler (y,n) [y] ? "
echo
echo
if [ $bc_flag != "y" ]
then
  let mem_model=3
	let flag=1
else
	let flag=0
fi
while [ flag -eq 0 ]
do
  echo 'MEMORY MODEL'
	echo '============='
	echo ' 1 - LARGE'
	echo " 2 - HUGE  ( C166 -- not tested / bc31 -- default )"
	echo " 3 - ANSI  ( no specific model, using compiler default )"
	echo
	echo 
	read mem_model?"please enter (1-3) [1] ? "
	echo
  if [ mem_model -ge 1 -a mem_model -le 3 ]
	then
	  let flag=1
  else
	  let flag=0
  fi
done
echo
let flag=0
while [ flag -eq 0 ]
do
  echo 'DEBUG MODE'
	echo '============='
	echo " 0 - NO DEBUG ( using both, debug and no-debug flags )"
	echo " 1 - DEBUG    ( just using debug flags )"
	echo
	read debug?"please enter (0-1) [1] ? "
	echo
  if [ debug -ge 0 -a debug -le 1 ]
	then
	  let flag=1
  else
	  let flag=0
  fi
done
echo
echo 
echo LOCATION AREA
echo ==============
echo
read ROOTDIR?'please give me a start dir for searching [../.] ? '
echo

JAUDIR=$(findnpatch $ROOTDIR jaulibs.c)
LIBDIR=$(findnpatch $ROOTDIR kl806.lib)
TERMDIR=$(findnpatch $ROOTDIR kl806.lib/termdac1)
TOOLDIR=$(findnpatch $ROOTDIR kl806.lib/tools)
RCFSDIR=$(findnpatch $ROOTDIR rcfs)
DJAUDIR=$(dir2dosdir $JAUDIR)
DLIBDIR=$(dir2dosdir $LIBDIR)
DTERMDIR=$(dir2dosdir $TERMDIR)
DTOOLDIR=$(dir2dosdir $TOOLDIR)
DRCFSDIR=$(dir2dosdir $RCFSDIR)

echo 'you choose the following options :'
echo '==================================='
echo
CCPROG=''
ASPROG=''
LNPROG=''
CCDBOPTPRE=''
CCNDBOPTPRE=''
CCDBOPTSUCC=""
CCNDBOPTSUCC=""
ASOPTPRE=""
ASOPTSUCC=""
LNOPT=""
LNKFILE=""
MKFILE=""
BATFILE=""

if [ $bc_flag = "y" ]
then
  echo 'YOU ARE USING A BORLAND COMPILER'
	if [ debug -eq 1 ]
	then
		LNOPT="-P- -x -v -l -c -L?DCC_HOMElib @?LNKFILE"
	else
		LNOPT="-P- -x -c -L?CC_HOMElib @?LNKFILE"
	fi
	ASPROG='asm.exe'
	CCPROG='bcc.exe'
	LNPROG='tlink.exe'
	CCDBOPTPRE="+bc.cfg -o\$@ -c -v -y"
	CCNDBOPTPRE="+bc.cfg -o\$@ -c "
	ASOPTPRE="-o\$@"
else
  echo 'YOU ARE USING A ANSI-C COMPILER'
	LNOPT="@?LNKFILE"
	ASPROG='as'
	CCPROG='cc'
	LNPROG='cc'
	CCDBOPTPRE="-o\$@ -c"
	CCNDBOPTPRE="-o\$@ -c"
	ASOPTPRE="-o\$@"
		DESTINY='kl806'
		OBJTYPE='o'
		CCDBOPTPRE=$CCDBOPTPRE' -ml -D_WITH_KL_806_'
		CCNDBOPTPRE=$CCNDBOPTPRE' -ml -DNDEBUG -D_WITH_KL_806_'
		LNKFILE='dinio806.lnk'
		MKFILE='makefile'
		BATFILE="gmake"
fi
DESTINY=''
OBJTYPE=''
print -n 'MEMORY MODELL : '
if [ mem_model -eq 1 ]
then
  echo  'LARGE'
	if [ $bc_flag = "y" ]
	then
		DESTINY='rcfs_l.exe'
		OBJTYPE='obl'
		CCDBOPTPRE=$CCDBOPTPRE' -ml -D__LARGE__ -D_WITH_KL_806_'
		CCNDBOPTPRE=$CCNDBOPTPRE' -ml -DNDEBUG -D__LARGE__ -D_WITH_KL_806_'
		LNKFILE='bcc_l.lnk'
		MKFILE='bcc_l.mak'
		BATFILE="gbcc_l"
	fi
elif [ mem_model -eq 2 ]
then
  echo  'HUGE'
	if [ $bc_flag = "y" ]
	then
		DESTINY='rcfs_h.exe'
		OBJTYPE='obh'
		CCDBOPTPRE=$CCDBOPTPRE' -mh -D__LARGE__ -D_WITH_KL_806_'
		CCNDBOPTPRE=$CCNDBOPTPRE' -mh -DNDEBUG -D__LARGE__ -D_WITH_KL_806_'
		LNKFILE='bcc_h.lnk'
		MKFILE='bcc_h.mak'
		BATFILE="gbcc_h"
	fi
fi
echo
echo ... and the following directorys :
echo
echo cc_home    : $CC_HOME
echo jaudir 		: $JAUDIR
echo libdir 	  : $LIBDIR
echo termdir 		: $TOOLDIR
echo rcfsdir 		: $RCFSDIR
echo tools 			: $TOOLDIR
echo 

if [ -e config ]
then
  rm -Rf config
fi

if [ ! -e config ]
then
  mkdir config
fi

#fixing if debug is forced
#
if [ debug -eq 1 ]
then
	CCNDBOPTPRE=$CCDBOPTPRE
	CCNDBOPTSUCC=$CCDBOPTSUCC
fi

# build an SED-SCRIPT FOR SUBSTITUTION ALL THE PLACEHOLDERS !!!
#
print -r "1,\$s/\?LNOPT/$LNOPT/g" >> config/prepmake.sed
print -r "1,\$s/\?CCDBOPTPRE/$CCDBOPTPRE/g" >> config/prepmake.sed
print -r "1,\$s/\?CCNDBOPTPRE/$CCNDBOPTPRE/g" >> config/prepmake.sed
print -r "1,\$s/\?CCDBOPTSUCC/$CCDBOPTSUCC/g" >> config/prepmake.sed
print -r "1,\$s/\?CCNDBOPTSUCC/$CCNDBOPTSUCC/g" >> config/prepmake.sed
print -r "1,\$s/\?ASOPTPRE/$ASOPTPRE/g" >> config/prepmake.sed
print -r "1,\$s/\?ASOPTSUCC/$ASOPTSUCC/g" >> config/prepmake.sed

print -r "1,\$s/\?CCPROG/$CCPROG/g" >> config/prepmake.sed
print -r "1,\$s/\?ASPROG/$ASPROG/g" >> config/prepmake.sed
print -r "1,\$s/\?LNPROG/$LNPROG/g" >> config/prepmake.sed

print -r "1,\$s/\?DESTINY/$DESTINY/g" >> config/prepmake.sed
print -r "1,\$s/\?OBJTYPE/$OBJTYPE/g" >> config/prepmake.sed
print -r "1,\$s/\?LNKFILE/$LNKFILE/g" >> config/prepmake.sed
print -r "1,\$s/\?MKFILE/$MKFILE/g" >> config/prepmake.sed

print -r "1,\$s/\?CC_HOME/$CC_HOME/g" >> config/prepmake.sed
print -r "1,\$s/\?JAUDIR/$JAUDIR/g" >> config/prepmake.sed
print -r "1,\$s/\?LIBDIR/$LIBDIR/g" >> config/prepmake.sed
print -r "1,\$s/\?TERMDIR/$TERMDIR/g" >> config/prepmake.sed
print -r "1,\$s/\?TOOLDIR/$TOOLDIR/g" >> config/prepmake.sed
print -r "1,\$s/\?RCFSDIR/$RCFSDIR/g" >> config/prepmake.sed
print -r "1,\$s/\?DCC_HOME/$DCC_HOME/g" >> config/prepmake.sed
print -r "1,\$s/\?DJAUDIR/$DJAUDIR/g" >> config/prepmake.sed
print -r "1,\$s/\?DLIBDIR/$DLIBDIR/g" >> config/prepmake.sed
print -r "1,\$s/\?DTERMDIR/$DTERMDIR/g" >> config/prepmake.sed
print -r "1,\$s/\?DTOOLDIR/$DTOOLDIR/g" >> config/prepmake.sed
print -r "1,\$s/\?DRCFSDIR/$DRCFSDIR/g" >> config/prepmake.sed

cat config.in/objects.in > config/objects.lst

# create the temporaery makefile
# and the final makefile
#
awk '$1~/.*/ { print $1 ".?OBJTYPE \\" }' config/objects.lst > config/objects.tmp
sed '/OBJ =/r config/objects.tmp' config.in/makefile.in > config/makefile.tmp
sed -f config/prepmake.sed config/makefile.tmp > $MKFILE

# create the temporaery linkfile
# and the final linkfile
#
if [ $bc_flag = "y" ]
then
	awk '$1~/.*/ { print $1 ".?OBJTYPE+" }' config/objects.lst > config/link.tmp
else
	awk '$1~/.*/ { print $1 ".?OBJTYPE" }' config/objects.lst > config/link.tmp
fi
if [ $bc_flag = "y" -a -e config.in/bc.lnk ]
then
	cat config.in/bc.lnk >> config/link.tmp
elif [ -e config.in/ansi.lnk ]
then
	cat config.in/ansi.lnk >> config/link.tmp
fi
sed -f config/prepmake.sed config/link.tmp > $LNKFILE
  

# create misc-stuff
#
for i in config.in/*.bat
do
	sed -f config/prepmake.sed $i > $(basename $i)
done
sed -f config/prepmake.sed config.in/pmake.ksh > $BATFILE.ksh
sed -f config/prepmake.sed config.in/pmake.bat > $BATFILE.bat
sed -f config/prepmake.sed config.in/bc.cfg > bc.cfg

echo -----------------------------------------------------
echo
print -n "COMPILER : "
if [ $bc_flag = "y" ]
then
  echo BORLAND
else
  echo ANSI-C
fi
echo
print -n "MEMORY MODEL : "
if [ mem_model -eq 1 ]
then
  echo LARGE
elif [ mem_model -eq 2 ]
then
  echo HUGE
else
  echo GENERIC
fi
echo
if [ debug -eq 1 ]
then
  echo "NO-DEBUG FLAGS IGNORED ( EQUAL DEBUG FLAGS )"
else
  echo "NO-DEBUG ACTIVE"
fi
echo
echo -----------------------------------------------------
echo
echo "MAKEFILE : $MKFILE"
echo "LINKFILE : $LNKFILE"
echo "BATCH-DO : $BATFILE.BAT, $BATFILE.KSH"
echo
echo -----------------------------------------------------
echo


