#!/bin/csh -f
#
# newarch
#
# Syntax:  newarch <NEWARCH> [<OLDARCH>]
#
# Thomas Gauweiler
#
# Changes:
#
# 26-mar-94     first created                                   [TG]
#


#
# determine initial settings
#

#setenv MSC_HOME "_MSC_HOME_"
setenv MSC_HOME "/home/i41s11/gauweil/m2s/install"

#
# determine machine architecture (what type of machine we're on!)
# by examining return values of "uname", "arch", or "machine"
#

if (-x /bin/uname || -x /usr/bin/uname) then
    set SRC_ARCH=`uname -m`
else if (-x /bin/arch || -x /usr/bin/arch) then
    set SRC_ARCH=`arch`
else if (-x /bin/machine || -x /usr/bin/machine) then
    set SRC_ARCH=`machine`
endif

if (! $?SRC_ARCH) then                  # could architecture not be determined ?
    echo 'Error: could not determine source architecture'
    exit 2
else if ("$SRC_ARCH" =~ sun4*) then     # Sun SparcStation
    set SRC_ARCH="SUN4"
else if ("$SRC_ARCH" =~ sun3*) then     # Sun 3 with 680x0 CPU
    set SRC_ARCH="SUN3"
else if ("$SRC_ARCH" =~ i386*) then     # Intel 386/486
    set SRC_ARCH="I386"
    if (-x /bin/uname || -x /usr/bin/uname) then
        if ("`uname -s`" =~ *Linux*) then
            set SRC_ARCH="LINX"         # Linux
        endif
    endif	
else if ("$SRC_ARCH" =~ ksr1*) then     # KSR-1
    set SRC_ARCH="KSRS"
else if ("$SRC_ARCH" =~ RISC* || "$SRC_ARCH" =~ mips*) then
    if ("`uname -v`" =~ *MP*) then      # MasPar frontend if ..
        set SRC_ARCH="MASP"             # DECStation and version *MP*
    else
        set SRC_ARCH="MIPS"             # else plain DECStation
    endif
else
    echo 'Error: unknown source architecture "$SRC_ARCH"'
    exit 2
endif



#
# check first argument
#

if  ("$2" == "")  then
  set OLD_ARCH=$SRC_ARCH
else
  set OLD_ARCH=$2
endif

#
# Check for parameters
#
if ($1 == "") then
    echo "Need one parameters: Name of new architecture (four chars, eg. SUN4)"
    exit 1
else
  set NEW_ARCH=$1
endif

echo generate $NEW_ARCH from $OLD_ARCH

#
# determine foreign module list
#

cd $MSC_HOME/lib/msd
set FOREIGN_LIST=""

foreach sourcefile (*.msd)
    if ("`/bin/grep 'FOREIGN MODULE' $sourcefile`" == "") then
    else
      set FOREIGN_LIST="$FOREIGN_LIST `basename $sourcefile .msd`"
    endif
end


#
# determine master makefile list
#

cd $MSC_HOME/lib/mkf/$OLD_ARCH
set MKF_LIST=""

foreach sourcefile (*.mkf)
  set MKF_LIST="$MKF_LIST `basename $sourcefile .$OLD_ARCH.mkf`"
end


#
# make new architecture and transform files
#

cd $MSC_HOME/lib
msproj $NEW_ARCH
foreach sourcefile ($FOREIGN_LIST MSSYSTEM SYSTEM MSHEADERS)
  sed  -e "s|$OLD_ARCH|$NEW_ARCH|"		\
       <sym/$OLD_ARCH/$sourcefile.$OLD_ARCH.h	\
       >sym/$NEW_ARCH/$sourcefile.$NEW_ARCH.h
  echo @sourcefile.h transformed
end
foreach sourcefile ($FOREIGN_LIST MSSYSTEM SYSTEM)
  sed  -e "s|$OLD_ARCH|$NEW_ARCH|"		\
       <imp/$OLD_ARCH/$sourcefile.$OLD_ARCH.c	\
       >imp/$NEW_ARCH/$sourcefile.$NEW_ARCH.c
  echo @sourcefile.c transformed
end

foreach sourcefile ($MKF_LIST)
  sed  -e "s|$OLD_ARCH|$NEW_ARCH|"		\
       <mkf/$OLD_ARCH/$sourcefile.$OLD_ARCH.mkf	\
       >mkf/$NEW_ARCH/$sourcefile.$NEW_ARCH.mkf
  echo @sourcefile.mkf transformed
end

#
# add a new entry in architecture
#

cd $MSC_HOME
fgrep " $OLD_ARCH " architectures	> arch.tmp
sed  -e "s|$OLD_ARCH|$NEW_ARCH|" 	< arch.tmp	> arch.tmp2
mv architectures architectures.old
cat architectures.old arch.tmp2 >architectures
rm arch.tmp arch.tmp2
echo a new entry in architectures added


#
# a message
#

echo .
echo .
echo please change the options for the architecture $NEW_ARCH in
echo "$MSC_HOME/architectures and "
echo "$MSC_HOME/mkf/$NEW_ARCH/master* (options for c-compiler)"
echo .
echo Then try to compile the lib with:
echo "cd $MSC_HOME/lib"
echo "mm alllib:$NEW_ARCH -system"

