#!/bin/bash
#
# Copyright (C) 2017 by the authors of the ASPECT code.
#
# This file is part of ASPECT.
#
# ASPECT is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# ASPECT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ASPECT; see the file LICENSE.  If not see
# <http://www.gnu.org/licenses/>.


ASPECT_DIR=${ASPECT_DIR:-@CMAKE_BINARY_DIR@}
PARAMETER_GUI_EXECUTABLE=${PARAMETER_GUI_EXECUTABLE:-@PARAMETER_GUI_EXECUTABLE@}

if [ "$1" == "-h" ]; then
  echo "aspect-gui - launcher script for the deal.II parameter GUI"
  echo "  Usage: ./aspect-gui [parameterfile.prm]"
  exit 0
fi

if [ ! -f $ASPECT_DIR/aspect ]; then
  echo "ERROR: ASPECT_DIR not set or does not contain aspect: $ASPECT_DIR"
  exit 1
fi

if [ ! -f $PARAMETER_GUI_EXECUTABLE ]; then
  echo "ERROR: PARAMETER_GUI_EXECUTABLE not set or invalid: $PARAMETER_GUI_EXECUTABLE"
  exit 1
fi

if [ "$1" == "" ]; then
  OUTPUT_FILE=temp.xml
  echo " " |  $ASPECT_DIR/aspect --output-xml -- > $OUTPUT_FILE
else
  OUTPUT_FILE=`echo $1 | sed -e 's/\.prm$/\.xml/'`

  if [ "$OUTPUT_FILE" == "$1" ]; then
    # The input file has an unusual file ending. Append .xml instead of
    # trying to replace
    OUTPUT_FILE=${1}.xml
  fi

  $ASPECT_DIR/aspect --output-xml $1 > $OUTPUT_FILE
fi

$PARAMETER_GUI_EXECUTABLE $OUTPUT_FILE
rm $OUTPUT_FILE
