#!/bin/csh

set wrapfilename="vasp.wrap"
set maxdepth=5

while ( $#argv != 0 )
  switch ("$1")
    case "-h":
      cat - <<EOF
runstruct_vasp [-w file] [-d depth] [-p] [-lu] [-ng] [-nr] [-ex] cmdprefix
  where file is an optional alternate wrap file (default: vasp.wrap)
    If the wrap file is not found in the current directory,
    it searches in the parent directories, up to number specified by depth (default is 5).
    For a description of the syntax of the vasp.wrap file, type
     ezvasp -h
    and look for options under the '[INCAR]' section.

  -p means preserve vasp output file
  -lu means look up one directory for WAVECAR to use as starting point.
  -ng means do not generate a vasp.in file, use the existing one.
  -nr means do not run vasp, just generate input files
  -ex means do not generate vasp.in, do not run vasp, but extract info from vasp output file
  cmdprefix is the prefix needed for vasp to run on a remote machine,
            such as "node -s node2" 
EOF
      exit 1
      breaksw
    case "-w":
      set wrapfilename="$2"
      shift
    breaksw
    case "-d":
      set maxdepth="$2"
      shift
    breaksw
    case "-p":
      set preservefiles
    breaksw
    case "-lu":
      set lookup
    breaksw
    case "-ng":
      set notgenvaspin
    breaksw;
    case "-nr":
      set notrunvasp
    breaksw;
    case "-ex":
      set extractonly
    breaksw;
    default:
      break;
  endsw
  shift
end

if ( ! $?extractonly ) then 

  if ( ! -e ~/.ezvasp.rc ) then
    ezvasp
    exit
  else
    source ~/.ezvasp.rc
  endif

  if (! $?notgenvaspin ) then
    set strout="str_hint.out"
    if ( ! -e $strout ) then
      set strout="str.out"
    endif

    if (! -e $strout ) then
      echo str.out or str_hint.out does not exist
      echo NOTE: runstruct_vasp needs to be run within the subdirectory containing a structure.
      exit 1
    endif

    set wrapfile="$wrapfilename"
    while (! -e $wrapfile && $maxdepth > 0)
      set wrapfile="../$wrapfile"
      @ maxdepth--;
    end
    if (! -e $wrapfile) then
      echo You need a $wrapfilename file in one of the directories  ., .. , ../.. , etc.
      echo NOTE: runstruct_vasp needs to be run within the numbered subdirectory.
      exit 1
    endif

    cat $wrapfile >! vasp.in
    echo "" >>! vasp.in
    echo "[POSCAR]" >>! vasp.in
    echo "title" >>! vasp.in
    echo 1. >>! vasp.in
    cat $strout | cellcvrt -c -sig=9 | tail -n +4 | head -3 >>! vasp.in
    echo Cartesian >>! vasp.in
    cat $strout | cellcvrt -c -sig=9 | tail -n +7 >>! vasp.in
  endif

  if ( $?lookup ) then
    foreach file ( ../WAVECAR ../CHGCAR )
	if ( -e $file ) then
	    cp -f $file .
	endif
    end
  endif

  set notrunvaspswitch=""

  if ( $?notrunvasp ) then
    set notrunvaspswitch="-n"
  endif

  if ( $#argv == 0 ) then
    #single machine mode if no argument
    ezvasp $notrunvaspswitch vasp.in
  else
    #multiple machine mode if argument is remote command
    ezvasp $notrunvaspswitch -p "$* $VASPCMD" vasp.in
  endif

endif

if ( ! $?notrunvasp ) then
  extract_vasp
  if ( $? == 1 ) then
    echo "Error extracting info from vasp files"
    exit 1
  endif
  if ( $?lookup ) then
    foreach file ( WAVECAR CHGCAR )
	if ( ! -e ../$file ) then
          cp -f $file ../$file
	endif
    end
  endif
  if ( ! $?extractonly ) then
    if ( ( ! $?preservefiles ) && -e OUTCAR.static ) then
      cleanvasp -v
    endif
    if ( -e OUTCAR.static || -e OUTCAR.relax ) then
      gzip -f `ls -1 OUTCAR.* | grep -v '.gz$'` >& /dev/null
    endif

    checkerr_vasp -e
  endif
endif
