#!/bin/bash

usage() {
    echo
    echo "Script file for repeating the PEG_water_simulation.sh script which runs a single simulation."
    echo "See J. Chem. Theory Comput. 2018, 14, 11, 5959–5968 for why we need repetitions"
    echo
    echo "The script will copy the files below and create as many folders as needed. Thus, you only "
    echo "need to include the files in the working directory once and let the script do copying for you."
    echo
    echo "NOTE: This script ONLY runs repeated INITIAL runs (i.e. goes through insert-molecules and energy min)"
    echo
    echo "Usage:"
    echo "  -b [string]     PEG basename/molecule's acronym (e.g. DEG) (double quotes around string will work, but not necessary unless there are spaces)"
    echo "                  For pure water, type 'water'"
    echo "  -m [string]     model to simulate (enter 'bulk' for bulk PEG, 'spce' for spc/e water, or 'tip4p2005' for tip4p/2005 water)"
    echo "  -t [value]      temperature in K (use underscore instead of decimal if necessary (e.g. 298_15)"
    echo "  -d [value]      density in g/cm^3 at desired temperature"
    echo "  -n [value]      total number of molecules in system"
    echo "  -p [value]      how many number of molecules are PEG"
    echo "  -w [value]      how many number of molecules are water"	
    echo "  -r [value]      how many total simulations are desired"
    echo
    echo "Require following files in working directory:"
    echo "  - [BASENAME].itp (PEG itp file) [for bulk PEG and PEG/water mixture simulations only]"
    echo "  - [MODEL].itp (water itp file) [for PEG/water mixture and pure water simulations only]"
    echo "  - top_[BASENAME]_bulk.top [for bulk PEG only] or top_[BASENAME]_water.top [for PEG/water mixtures only] or top_pure_water.top [for pure water only]"
    echo "  - single_[BASENAME].gro [for bulk PEG and PEG/water mixture simulations only]"
    echo "  - single_[MODEL].gro [for PEG/water mixture and pure water simulations only]"
    echo "  - em.mdp"
    echo "  - eq_NPT_[TEMP]K.mdp"
    echo "  - md_NVT_[TEMP]K.mdp"
    echo "  - PEG_water_simulation.sh"
    echo "  - This shell file"
    echo
    echo "To start the script, use either one of the following:"
    echo "  - pass no arguments to command line (user input will be asked for)"
    echo "  - pass all 8 flags to command line"
    echo
    exit 1
}

WORKDIR="$(dirname "$(readlink -f "$0")")"


##### Getting parameters and checking that everything is alright to go
# passing arguments to command line
while getopts ":b:m:t:d:n:p:w:r:h" options; do
    case ${options} in
        b)
            BASENAME=$OPTARG ;;
        m)
            MODEL=$OPTARG ;;
        t)
            TEMP=$OPTARG ;;
        d)
            DENSITY=$OPTARG ;;
        n)	
            TOTALNMOL=$OPTARG ;;
        p)
            PEGNMOL=$OPTARG ;;
        w)
            WATERNMOL=$OPTARG ;;
        r)
            TOTALRUN=$OPTARG ;;
        h) 
            usage ;;
        \?)
            echo "Invalid input. Unknown flag. Refer to the help with -h"
            exit 1 ;;
        :)
            echo "Invalid input: -$OPTARG requires an argument. Refer to the help with -h"
            exit 1 ;;
    esac
done

# if nothing passed to command line, go for user input
if [ $OPTIND -eq 1 ]; then	
    read -p "Molecule's acronym: " BASENAME
    read -p "Model to simulate (enter 'bulk' for bulk PEG, 'spce' for spc/e water, or 'tip4p2005' for tip4p/2005 water): " MODEL
    read -p "Temperature (in K) (Use underscore instead of decimal if necessary, e.g. 298_15): " TEMP
    read -p "${BASENAME}'s density at ${TEMP}K in g/cm^3 (if unknown, choose something reasonable): " DENSITY
    read -p "Number of total molecules in system: " TOTALNMOL
    read -p "How many of those molecules are PEG: " PEGNMOL
    read -p "How many of those molecules are water: " WATERNMOL
    read -p "Number of simulations to run: " TOTALRUN
fi

# check to see, if user used command line, that all parameters are defined
if [ $OPTIND -gt 1 ]; then
    # forgot basename
    if [ -z $BASENAME ]; then
        echo "You forgot to set the basename. Either use no flags, or use -b"
        exit 1 
    fi 
    # forgot water model to use
    if [ -z $MODEL ]; then
        echo "You forgot to choose a model to simulate. Either use no flags, or use -m"
        exit 1 
    fi 
    # typed an unsupported model
    if [ $MODEL != "bulk" ] && [ $MODEL != "spce" ] && [ $MODEL != "tip4p2005" ]; then
        echo "You have chosen an unsupported model. Please correct -m input"
        exit 1 
    fi			
    # forgot temperature
    if [ -z $TEMP ]; then
        echo "You forgot to set the temperature. Either use no flags, or use -t"
        exit 1 
    fi 
    # forgot density
    if [ -z $DENSITY ]; then
        echo "You forgot to set the density. Either use no flags, or use -d"
        exit 1 
    fi 
    # forgot to specify total number of molecules
    if [ -z $TOTALNMOL ]; then
        echo "You forgot to set the total number of molecules. Either use no flags, or use -n"
        exit 1 
    fi 
    # forgot to specify number of PEG molecules
    if [ -z $PEGNMOL ]; then
        echo "You forgot to set the number of PEG molecules. Either use no flags, or use -p"
        exit 1 
    fi 
    # forgot to specify number of water molecules
    if [ -z $WATERNMOL ]; then
        echo "You forgot to set the number of water molecules. Either use no flags, or use -w"
        exit 1 
    fi 
    # forgot to specify total number of simulations
    if [ -z $TOTALRUN ]; then
        echo "You forgot to set the number of simulations to run. Either use no flags, or use -r"
        exit 1 
    fi	
fi
	
# if the number of PEG and water molecules do not sum to total molecules in system, then stop script
if [ $(( $PEGNMOL + $WATERNMOL )) -ne $TOTALNMOL ]; then
    echo "Sum of PEG and water molecules is not equal to total number of molecules specified. Please correct input."
    exit 1 
fi
##### END STEP

##### Creating as many folders as number of desired simulations w/ files. Comment this section out if continuing set
mkdir "$WORKDIR"/run1
mv "$WORKDIR"/*.itp "$WORKDIR"/run1
mv "$WORKDIR"/*.top "$WORKDIR"/run1
mv "$WORKDIR"/*.gro "$WORKDIR"/run1
mv "$WORKDIR"/*.mdp "$WORKDIR"/run1
mv "$WORKDIR"/PEG_water_simulation.sh "$WORKDIR"/run1
RUN=2
while [ $RUN -lt $(($TOTALRUN + 1)) ]; do
    cp -r "$WORKDIR"/run1 "$WORKDIR"/run${RUN}
    let RUN=$RUN+1
done
##### END STEP

##### Running the simulations as many times as desired. If continuing set, set RUN and re-run script
RUN=1
while [ $RUN -lt $(($TOTALRUN + 1)) ]; do
    bash "$WORKDIR"/run${RUN}/PEG_water_simulation.sh -b $BASENAME -m $MODEL -t $TEMP -d $DENSITY -n $TOTALNMOL -p $PEGNMOL -w $WATERNMOL -i
    # mv "$WORKDIR"/energy.xvg "$WORKDIR"/run${RUN}/3_prod_NVT_${TEMP}K/analysis
    # mv "$WORKDIR"/enecorr.xvg "$WORKDIR"/run${RUN}/3_prod_NVT_${TEMP}K/analysis/enecorrbulkvisc.xvg
    # mv "$WORKDIR"/"#enecorr.xvg.1#" "$WORKDIR"/run${RUN}/3_prod_NVT_${TEMP}K/analysis/enecorrshearvisc.xvg
    let RUN=$RUN+1
done
##### END STEP


