#!/bin/bash

#First argument is directory with customize.py
#Second is choices file, with absolute path name.
#Third is path to where the grammar should be stored,
#which should take the form .../gmcs/regression_tests/grammars/language-name.

cd $1/..

# set the appropriate Python version
python_cmd='python'
if ! echo $( $python_cmd -V 2>&1 ) | grep -q "Python 2\.[5,6,7]"; then
  echo "Default Python version incompatible. Attempting to find another..." >&2
  if which python2.5 >/dev/null; then
    python_cmd='python2.5'
  elif which python2.6 >/dev/null; then
    python_cmd='python2.6'
  elif which python2.7 >/dev/null; then
    python_cmd='python2.7'
  else
    echo "No compatible Python version found. Exiting."
    exit 1
  fi
  echo "  Found $( $python_cmd -V 2>&1 ). Continuing." >&2
fi

if [ -e datestamp ]
then
    rm datestamp
fi
date > datestamp

if [ -e $3 ]; then
    echo "Move $3, it is in the way"
    return=17
elif [ ! -e $2 ]; then
    echo "No choices file at path $2"
    return=18
else
    mkdir -p $3
    cp $2 $3/choices
    $python_cmd $1/../matrix.py c $3
    return=$?
fi

rm datestamp

#Set return value so that calling script can tell
#if there were errors here or not.

exit $return
