#!/bin/bash
echo "zusätzlich zu OPAL liegen nützliche Dateien in https://gitlab.mn.tu-dresden.de/wwalter/PROG-material-public"
echo "gedit settings:"
gsettings set org.gnome.gedit.preferences.editor tabs-size 2
echo "tabs-size"
gsettings set org.gnome.gedit.preferences.editor insert-spaces true
echo "insert-spaces"
gsettings set org.gnome.gedit.preferences.editor display-right-margin true
echo "display margin"
gsettings set org.gnome.gedit.preferences.editor right-margin-position 70
echo "margin position"
gsettings set org.gnome.gedit.preferences.editor auto-indent true
echo "auto indent"
gsettings set org.gnome.gedit.preferences.editor display-line-numbers true
echo "display l nr"


echo "frun <Programm> <Eingabe> <noetige Module> ist ein neuer Befehl, der ein Fortran-Programm kompiliert und ausfuehrt. Es steht zur Verfügung, sobald du eine neue Shell startest, z.B. mit Ctrl+Alt+T."
mkdir --parents ~/zih_home/bin
echo 'PATH="$PATH:~/zih_home/bin"' >> ~/.bashrc
echo '#! /bin/sh
if test -z "$1"; then
  echo "Dieses kleine Programm kompiliert ein f95-Programm (der Form <Programm> [<Eingabe>] [<Module>]) mit mehreren hilfreichen Optionen und führt es aus"
  exit 1
fi

# Remove suffix.
# Also works when main program is given without the .f95 ending.
# This is done for backwards compatability reasons.
case "$1" in
  *f95 ) outfile="${1%.*}"
         ;;
  * ) outfile="$1"
    ;;
esac
programm="$outfile.f95"

if (( $# > 1 )) && test "$2" == *f95; then
  shift
  module="$@"
  echo "genutzte Module: "$module
  echo "Fortran-Hauptprogramm: "$programm
  echo "Maschinenprogramm: "$outfile
  f95 $module "$programm" -o "$outfile" -std=f95 -Wall -fmax-errors=3 -fcheck=all && "./$outfile"
elif (( $# > 1)); then
  eingabe="$2"
  shift
  shift
  module="$@"
  echo "genutzte Module: "$module
  echo "Fortran-Hauptprogramm: "$programm
  echo "Maschinenprogramm: "$outfile
  f95 $module "$programm" -o "$outfile" -std=f95 -Wall -fmax-errors=3 -fcheck=all && echo "$eingabe" | "./$outfile"
else
  echo "Fortran-Hauptprogramm: "$programm
  echo "Maschinenprogramm: "$outfile
  f95 $module "$programm" -o "$outfile" -std=f95 -Wall -fmax-errors=3 -fcheck=all && "./$outfile"
fi' > ~/zih_home/bin/frun
chmod a+rwx ~/zih_home/bin/frun