#! /bin/ksh

# Old path (CentOS)
#export PATH=/home/linuxbin/emacsimdir/bin:$PATH

export PATH=/home/ubuntu16/emacsimdir/bin:$PATH
CS255DIR=.
PARSER=.:

if [[ $# = 0 ]]
then
   echo "Usage: $0 prog1 [prog2]"
   exit 1
fi

if [[ $# = 1 ]]
then
   if test -f $1.s 
   then

      x=`java -cp $PARSER Parser < $1.s`;
      if [[ $x != "0" ]]
      then
         echo "Illegal instruction at line $x";
         echo "Possible causes:";
         echo "  1. The cmp instruction MUST use a data register";
	 exit 1;
      fi

      $CS255DIR/as255-pp < $1.s > ....255.$1.s ;
      m68k-elf-as -a=$1.list -o $1.o -M -m68000 -c ....255.$1.s ;
# -M = use MRI (Microtec Research) compatibility mode)
      /bin/rm ....255.$1.s ;

      if test -f $1.o
      then
         m68k-elf-ld -o $1 -e Start -Ttext=0x1600 $1.o $CS255DIR/cs255lib.o
      else
         echo 
         echo Program did not compile because of errors
         echo Check the error messages above
         echo +++ the number reported is the line number in your program file
         exit 1
      fi
   elif test -f $1
   then
      echo "Error: don't specify the extension .s !"
      exit 1
   else 
      echo "Error: source file $1.s not found"
      exit 1
   fi
fi


if [[ $# = 2 ]]
then
   if test -f $1.s 
   then

      x=`java -cp $PARSER Parser < $1.s`;
      if [[ $x != "0" ]]
      then
         echo "Illegal instruction at line $x";
         echo "The cmp instruction MUST use a data register !";
	 exit 1;
      fi

      $CS255DIR/as255-pp < $1.s > ...x255.$1.s ;
      m68k-elf-as -o $1.o -M -m68000 -c ...x255.$1.s ;
# -M = use MRI (Microtec Research) compatibility mode)
      /bin/rm ...x255.$1.s ;
   elif test -f $1
   then
      echo "Error: don't specify the extension .s !"
      exit 1
   else 
      echo "Error: source file $1.s not found"
      exit 1
   fi


   if test -f $2.s 
   then

      x=`java -cp $PARSER Parser < $2.s`;
      if [[ $x != "0" ]]
      then
         echo "Illegal instruction at line $x";
         echo "The cmp instruction MUST use a data register !";
	 exit 1;
      fi

      $CS255DIR/as255-pp < $2.s > ...x255.$2.s ;
      m68k-elf-as -o $2.o -M -m68000 -c ...x255.$2.s ;
# -M = use MRI (Microtec Research) compatibility mode)
      /bin/rm ...x255.$2.s ;
   elif test -f $1
   then
      echo "Error: don't specify the extension .s !"
      exit 1
   else 
      echo "Error: source file $1.s not found"
      exit 1
   fi


   m68k-elf-ld -o $1 -e Start -Ttext=0x1600 $1.o $2.o $CS255DIR/cs255lib.o

fi


