#!/bin/sh
# Script to compile NUT-0 for a new case 
# This script MUST be run inside the nut0.1 folder.
#
# Usage: nut0_install [casename fshape]
#   ***** The file nut0_casename_geom.dat must exist here *****
#   ***** The file nut0_fshape_fs.f must exist here *****
#   These two files define your new case "casename"
#   This script first makes a new folder "casename"/,
#   then moves your files, if they exist, to "casename"/,
#   then moves the necessary files to "casename"/,
#   and makes a new executable nut0_"casename" there.
#
# Example: nut0_install tftr cyl
#          needs nut0_tftr_geom.dat and nut0_cyl_fs.f
#          executable nut0_tftr will be made inside a new folder "tftr"
#
# casename [optional]: is the name of your case.
#   This will be used to label the new folder, ALL executables,
#          and other nut0 files
#   ***** The file nut0_casename_geom.dat must exist here *****
#   If this file is not here, then this script will ask you 
#      if you wish to create a demo version of nut,      
#      in a new folder called demo, 
#      using dat/nut0_demo_dim.dat that came with nut.tar.
#   The demo data file contains geometrical and dimension info
#   You can copy it and change the parameter values to create 
#   your own startup data file for Nut0.
#
# fshape [optional]: is the name of your flux surface shape routine.
#   the routine MUST be in a file named:
#      nut0_fshape_fs.f in this directory.
#   If it is not here, I copy the demo fs routine fs/nut0_cyl_fs.f
#   into "casename"/nut0_fshape_fs.f and exit.
#   To make your own fshape routine, look at one of the three examples
#   in the directory fshape that came with nut0.tar
#
# Note about making new custom 'rules' for your own machine:
#   On each machine,  the rules for compiling are different
#   Currently supported machines are: linux, alpha, sun
#   If your machine is not in this list, you need to do the following:
#   1. Run the uname -a command on your machine to see its name
#   2. Find out what your machine+compiler like.
#      To do this:
#      a. start with the rules/rules_alpha file as an example
#      b. copy it to rules/rules_mymachine
#      c. edit rules/rules_mymachine
#   3. Then in this file, add a line for your machine in next section
#      uname -a | grep mymachinename    && computer=mymachine
#      You can run the uname -a command on your machine to see its name
#
#  Now you are eady to try it on your own machine
#
#--------------------------------------------------------------
# First find what machine you are on
# Following machines are already implemented and tried
computer=generic
uname -a | grep Linux  && computer=linux
uname -a | grep alpha  && computer=alpha
uname -a | grep SunOS  && computer=sun
#
# add your own machine here
# uname -a | grep mymachinename    && computer=mymachine
#
# Following versions will be implemented later
uname -a | grep lonestar && computer=lonestar
uname -a | grep AIX    && computer=risc
uname -a | grep ULTRIX && computer=dec
uname -a | grep IRIX   && computer=ccf
uname -a | grep IRIX64 && computer=orig
uname -a | grep OSF1   && computer=osf1
uname -a | grep HP-UX  && computer=uscws
uname -a | grep R4000  && computer=nifs
uname -a | grep SX-5   && computer=sx5
#
rulesfile=rules/rules_$computer
if test ! -f "$rulesfile"; then
  echo 'Sorry, '"$rulesfile"' does not exist, I cannot proceed'
  echo 'I am making a sample rules file for you'
  echo 'You need to edit it to match  your compiler'
  echo 'Else, contact pvalanju@mail.utexas.edu for help'
  cp rules/rules_alpha $rulesfile
  exit
fi
#
machineid=`uname -n`
echo 'Making new nut0.1 on ' "$machineid"
#
#--------------------------------------------------------------
# First find the new "casename"
if [ "$1" != "" ]; then
 ext=$1
else
 echo 'Do you wish to create a demo version of Nut0? y/n?'
 read ans
 if [ "$ans" != "y" ]; then exit;fi
 ext=demo
fi
#--------------------------------------------------------------
# See if directory "casename" already exists
# If not, make a new directory "casename"
if test -d "$ext"; then
 echo 'Directory '"$ext"' already exists'
 echo 'Shall I overwrite files in it? y/n?'
 read ans
 if [ "$ans" != "y" ]; then exit;fi
else
 mkdir "$ext"
fi
# 
#--------------------------------------------------------------
# **** The file nut0_casename_geom.dat must exist here *****
# Make sure the startup geometry file exists here, 
# and copy it to "casename"/nut0_new_geom.dat
# else use the default dat/nut0_demo_geom.dat
if test -f "nut0_"$ext"_geom.dat"; then 
 cp "nut0_"$ext"_geom.dat" "$ext"/nut0_new_geom.dat
else
 cp dat/nut0_demo_geom.dat "$ext"/nut0_new_geom.dat
fi
if test ! -f "$ext"/nut0_new_geom.dat; then 
 echo 'Error: Your starting geometry file is not here.'
 echo '       Look at dat/nut0_demo_geom.dat'
 echo '       Change it to make your own'
 echo '       And run nut0_install again'
 exit
fi
#---------------------------
# **** The file nut0_fs.f must exist here *****
# Make sure the specified fshape file exists here
# and copy it to "casename"/nut0_fs.f
fs=$2
# If no flux surface specified, use cylinder
if [ "$fs" = "" ]; then fs="cyl"; fi
#
fsf='nut0_'$fs'_fs.f'
if test -f $fsf ; then
 cp $fsf "$ext"/nut0_fs.f
else
 cp fs/$fsf "$ext"/nut0_fs.f
fi 
if test ! -f "$ext"/nut0_fs.f ; then
 echo 'Error: Your routine '$fsf' is not here.'
 echo '       Look at examples in subdirectory fs'
 echo '       Use one of these, or make your own'
 echo '       call it nut0_'$fs'_fs.f. Put it here and'
 echo '       run nut0_install again'
 cd fs
 exit
fi
#--------------------------------------------------------------
# Copy correct rules for this machine
cp "$rulesfile" "$ext"/rules
# Some machines need Makefile instead of makefile, so put both
cp nut0_make "$ext"/makefile
cp nut0_make "$ext"/Makefile
cp src/* "$ext"
# Copy demo input data files so user can use them as starters
cp dat/nut0_demo_in.dat "$ext"
cp dat/nut0_demo_diag_in.dat "$ext"
cp nutpl "$ext"
#--------------------------------------------------------------
# Now move to the new directory and see if all files exist here
echo 'All necessary files are here, moving to '"$ext"
cd "$ext"
#---------------------------
# Now a new copy of nutdimen is compiled and run:
echo 'Welcome to nut_0.1: the resurrected version of old NUT'
echo '1. Making new geometry for '"$ext"
make geom
#---------------------------
echo '2. Running new nut0_geom for '"$ext"
nut0_new_geom
#---------------------------
# Compile a new copy of Nut
echo '3. Making new executable nut0_'"$ext"
make exe
mv Nut0 nut0_"$ext"
#-----------------------------------------
# Build a library of nut0 routines for later use
echo '4. Making new library of executables nut0_'"$ext"'.a'
make lib
mv nut0.a nut0_"$ext".a
#-----------------------------------------
echo 'Succesfully recompiled nut0 for '$ext
echo 'Now you can run it by typing nut0_'$ext
echo 'Or use compiled nut0 routines from library nut0_'$ext'.a'
echo '-----'
echo 'Do you wish to run nut0? y/n?'
read ans
if [ "$ans" = "y" ]; then nut0_"$ext";fi
#----------------------------------------
