#!/bin/bash

#
# in the same vein/with use of code from
# QE espresso driver program
# 12/8/2009  Jesse Noffsinger
#
PREFIX=`cd ../.. ; pwd`
BIN_DIR=$PREFIX/bin
TMP_DIR=$HOME/tmp_ex
#PARA_PREFIX="mpirun -np 2"
PARA_POSTFIX=

# run from directory where this script is
cd `echo $0 | sed 's/\(.*\)\/.*/\1/'` # extract pathname
EXAMPLE_DIR=`pwd`

# check whether echo has the -e option
if test "`echo -e`" = "-e" ; then ECHO=echo ; else ECHO="echo -e" ; fi

$ECHO "1) Calculate phonons"
$ECHO "2) Run EPW"
$ECHO "3) Both"

read opts

if [ $opts -eq 1 ]; then
 phons="1"
 epw="0"
fi
if [ $opts  -eq 2 ]; then
 phons="0"
 epw="1"
fi
if [ $opts  -eq 3 ]; then
 phons="1"
 epw="1"
fi
# function to test the exit status of a job
#@jn. ../check_failure.sh

$ECHO
$ECHO "$EXAMPLE_DIR : starting"
$ECHO
$ECHO "This example ..."

# set the needed environment variables
#. ../environment_variables

# required executables and pseudopotentials
BIN_LIST="pw.x ph.x"
PSEUDO_LIST="Pb.pz-bhs.UPF"
PSEUDO_DIR=$EXAMPLE_DIR/pp
BIN_DIR=$EXAMPLE_DIR/../../../bin
$ECHO
$ECHO "  executables directory: $BIN_DIR"
$ECHO "  pseudo directory:      $PSEUDO_DIR"
$ECHO "  temporary directory:   $TMP_DIR"
$ECHO "  checking that needed directories and files exist...\c"

# check for directories
for DIR in "$BIN_DIR" "$PSEUDO_DIR" ; do
    if test ! -d $DIR ; then
        $ECHO
        $ECHO "ERROR: $DIR not existent or not a directory"
        $ECHO "Aborting"
        exit 1
    fi
done
cd $EXAMPLE_DIR/epw/tmp

# check for executables
for FILE in $BIN_LIST; do
    if test ! -x $BIN_DIR/$FILE ; then
        $ECHO
        $ECHO "ERROR: $BIN_DIR/$FILE not existent or not executable"
        $ECHO "Aborting"
        exit 1
    fi
done
    if test ! -x $BIN_DIR/../EPW/bin/epw.x; then
        $ECHO
        $ECHO "ERROR: $BIN_DIR/$FILE not existent or not executable"
        $ECHO "Aborting"
        exit 1
    fi

# check for pseudopotentials
for FILE in $PSEUDO_LIST ; do
    if test ! -r $PSEUDO_DIR/$FILE ; then
        $ECHO
        $ECHO "ERROR: $PSEUDO_DIR/$FILE not existent or not readable"
        $ECHO "Aborting"
        exit 1
    fi
done
$ECHO " done"

# how to run executables
PW_COMMAND="$PARA_PREFIX $BIN_DIR/pw.x $PARA_POSTFIX"
PH_COMMAND="$PARA_PREFIX $BIN_DIR/ph.x $PARA_POSTFIX"
EPW_COMMAND="$PARA_PREFIX $BIN_DIR/../EPW/bin/epw.x $PARA_POSTFIX"
$ECHO
$ECHO "  running pw.x  as: $PW_COMMAND"
$ECHO "  running ph.x  as: $PH_COMMAND"
$ECHO "  running epw.x as: $EPW_COMMAND"
#$ECHO
#
#for diago in david cg ; do
#
    # clean TMP_DIR
    $ECHO "  cleaning $TMP_DIR...\c"
    rm -rf $TMP_DIR/*
    $ECHO " done"
if [ "$phons" == "1" ]; then
    #phonon
    for  Q in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16; do
	if test ! -d $EXAMPLE_DIR/phonon/tmp/q_${Q}; then
	    mkdir $EXAMPLE_DIR/phonon/tmp/q_${Q}
	fi
	if test ! -d $EXAMPLE_DIR/phonon/tmp/q_${Q}/tmp; then
	    mkdir $EXAMPLE_DIR/phonon/tmp/q_${Q}/tmp
	fi
	cd    $EXAMPLE_DIR/phonon/tmp/q_${Q}/tmp
	Q1=$(head ../../../qlist.dat -n${Q} | tail -n1 | awk '{ print $6 }')
	Q2=$(head ../../../qlist.dat -n${Q} | tail -n1 | awk '{ print $7 }')
	Q3=$(head ../../../qlist.dat -n${Q} | tail -n1 | awk '{ print $8 }')
	cat ../../../inp/nscf.in   | sed -e "s/XQ1/$Q1/g"    \
                            | sed -e "s/XQ2/$Q2/g"    \
                            | sed -e "s/XQ3/$Q3/g"    \
                            > nscf.in
	cat ../../../inp/ph.in   | sed -e "s/XQ1/$Q1/g"    \
                            | sed -e "s/XQ2/$Q2/g"    \
                            | sed -e "s/XQ3/$Q3/g"    \
                            > ph.in
    $ECHO "  Calculating phonon $Q1 $Q2 $Q3 for Pb..."
    $ECHO "  running the scf calculation for Pb..."
    $PW_COMMAND  < $EXAMPLE_DIR/phonon/inp/scf.in  > ../scf.out
    $ECHO "  running pw.x..."
    $PW_COMMAND  < nscf.in   > ../nscf.out
    $ECHO "  running ph.x..."
    $PH_COMMAND  < ph.in   > ../ph.out
#    check_failure $?
    $ECHO " Copying dvscf and dyn for q_${Q}"
    cp Pb.dvscf ../../../save/Pb.dvscf_q${Q}
    cp Pb.dyn ../../../save/Pb.dyn_q${Q}
    $ECHO " Cleaning up after calculation"
    cd ../
#    rm -r tmp
    $ECHO
    done
    $ECHO " done"
fi
if [ "$epw" == "1" ]; then
    # epw
    cd $EXAMPLE_DIR/epw/tmp
    $ECHO "  running the scf calculation for epw/Pb..."
    $PW_COMMAND  < $EXAMPLE_DIR/epw/inp/scf.in  > $EXAMPLE_DIR/epw/out/scf.out
    $ECHO "  running the nscf calculation for epw/Pb..."
    $PW_COMMAND  < $EXAMPLE_DIR/epw/inp/nscf.in > $EXAMPLE_DIR/epw/out/nscf.out
    $ECHO "  running epw on Pb..."
    $EPW_COMMAND < $EXAMPLE_DIR/epw/inp/epw.in  > $EXAMPLE_DIR/epw/out/epw.out
#    check_failure $?
    $ECHO " done"
#
fi
$ECHO
$ECHO "$EXAMPLE_DIR : done"