#!/bin/sh

if [ $# -eq 0 ]; then
  echo "Usage: gate project-file"
  exit 1
fi

echo Generating Ada files...

dir=`dirname $1`
file=`cd $dir; pwd`/`basename $1`
info=`gate-in.exe -p -s -x $file`
stat=$?

if [ $stat != 0 ]; then
  if [ $stat = 2 ]; then
     echo $info
  fi

  echo "Couldn't parse $file Exiting."
  exit 1
fi

set $info

if [ "$1" = "<no_name>" ]; then
   prj=default
else
   prj=$1
fi

if [ "$2" = "<no_name>" ]; then
   srcdir=.
   psrcdir="the current directory"
else
   srcdir=$2
   psrcdir=$srcdir
fi

pixdir="$3"
# Copy any pixmap files from pixdir to srcdir

mkdir -p $dir/$srcdir

if [ "$pixdir" != "<no_name>" -a -d $dir/$pixdir ]; then
   cp $dir/$pixdir/*xpm $dir/$srcdir > /dev/null 2>&1
fi

owd=`pwd`
cd $dir/$srcdir

if [ $? != 0 ]; then
  echo "Couldn't change to $dir/$srcdir, aborting."
  exit 1
fi

gt=".gate/$prj"
mkdir -p $gt > /dev/null 2>&1
tmp=$gt/tmp
/bin/rm -rf $tmp
mkdir $tmp
wd=`pwd`
gate-in.exe $file > $tmp/gate.ada

if [ $? != 0 ]; then
  echo "Couldn't generate Ada code. Exiting."
  exit 1
fi

cd $tmp
if gnatchop gate.ada > /dev/null 2>&1; then
  true
else
  echo "Warning: gnatchop returned non zero status."
fi 

/bin/rm -f gate.ada
files=`echo *`
cd $wd

if [ "@MERGE_AVAIL@" = "True" ]; then
  conflicts=0
  echo "Merge of some changes failed. It usually means that some modified code
is obsolete in the current project file.
Conflicts have been kept in the following files to help merging manually:
" > $gt/conflicts.txt

  for j in $files; do
    if [ -f $j ]; then
      @MERGE@ $j $gt/$j $tmp/$j 2>/dev/null
    else
      cp $tmp/$j $j
    fi

    if [ $? = 1 ]; then
      conflicts=1
      echo "$j" >> $gt/conflicts.txt
    fi
  done

  echo "The following files have been created/updated in $psrcdir:"

  for j in $files; do
    echo "  "$j
  done

  if [ $conflicts = 1 ]; then
    cat $gt/conflicts.txt | gdialog error justify_left
  fi
  
  echo done.

else

  /bin/rm -f $gt/gate.difs

  for j in $files; do
    @DIFF@ $gt/$j $j >> $gt/gate.difs 2>/dev/null
  done

  /bin/cp -f $tmp/* .
  /bin/rm -f *.rej *.orig

  if cat $gt/gate.difs | @PATCH@ > $gt/patch.out 2>&1; then
    echo "The following files have been created/updated in $psrcdir:"

    for j in $files; do
      echo "  "$j
    done

    /bin/rm -f *.orig
  else
    echo "The following files have been updated in $psrcdir:"

    for j in $files; do
      echo "  "$j
    done

    cat << EOF | gdialog error justify_left
Merge of some changes failed. It usually means that some modified code
is obsolete in the current project file.
.rej files have been generated to help merging manually if needed.
EOF
  fi

  echo done.
fi

/bin/mv -f $tmp/* $gt/
