#!/bin/csh

# command xhf requires 2 or 3 arguments:
#
#  $1 - prefix of the input data file. The file name must be $1.data.
#
#       In the case of an abortive run use command xhf again with the first 
#       argument either 'rename' or 'remove'. In the former case the temporary input
#       and output files used by the program will get prefixes in accordance with
#       the second (or, if present, the third) argument. In the latter case
#       all temporary files will be deleted.

#  $2 - if this is the last argument then 
#         $2 is a prefix of output files produced ($2.orb, $2.coul, $2.exch, $2.fort.n, 
#         $2.fort.n+1, etc.)
#	else 
#	   $2 is a prefix of input files required by the program to
#	   continue calculations (files $2.orb, $2.coul, $2.exch, 
#          $2.fort.n, $2.fort.n+1, etc. are expected to exist in the
#	   working directory)
#          n is set to 31 (see FIRST_FORT_FILE variabl;e)

#  $3 - (if present) a prefix of output files produced by the program;
#       these can be used as input files for other runs

    set INPUT_TMP  = 2dhf_input
    set OUTPUT_TMP = 2dhf_output

#  see ./src/rrec for the choice of 31
    set FIRST_FORT_FILE = 31

    if ( ( $1 != 'rename' ) & ( $1 != 'remove' ) ) then
	if ( -e $2.exch & -e $2.fort.$FIRST_FORT_FILE ) then
	    echo Error: files $2.exch and $2.fort.$FIRST_FORT_FILE, ... exist
		exit
	endif

	if ( "${3}" != '' ) then
#
#	move $2.fort.* files into their fort.* ones
#
	    set tf="${2}.fort."
	    set fort = ` ls ${tf}* ` 
	    if ( "$fort" == '' & !( -e $2.exch ) ) then
		echo "Warning: files" "$2.fort.* are missing"
	    else
		set fort = `ls ${tf}*` 
		foreach file  ($fort)
		    if ( -z $file ) then
			echo Error: "file $file has zero bytes"
			exit 
		    else
			set ext = `echo $file | sed s/${2}'.fort.'// `
  			mv $file fort.$ext
		    endif	
		end
		set lsfort = `ls fort.*`
	    endif
#
#	move $2.* input files into their $INPUT_TMP.* counterparts
#
	    foreach file ($2.orb $2.coul $2.exch)
		if ( -e $file) then
		    set ext = `echo $file | sed s/${2}'.'// `
		    mv $file $INPUT_TMP.$ext  >& /dev/null
		else
		    if ( !( -e fort.$FIRST_FORT_FILE ) ) then
			set ext = `echo $file | sed s/${2}'.'// `
			echo "Warning: file "  $2.$ext is missing
			endif
		endif
	    end
	else
	    
#	move $2.fort.* files into their fort.* ones
#
	    set tf="${2}.fort."
	    set fort = ` ls ${tf}* ` 
	    if ( "$fort" == '' & !( -e $2.exch ) ) then
#               $2.fort and $2.exch files need not exist
#		echo "Warning: files" "$2.fort.* are missing"
	    else
		set fort = `ls ${tf}*` 
		foreach file  ($fort)
		    set ext = `echo $file | sed s/${2}'.fort.'// `
		    mv $file fort.$ext
		end
		set lsfort = `ls fort.*`
	    endif
#
#	move $2.* input files into their $OUTPUT_TMP.* counterparts
#
	    foreach file ($2.orb $2.coul $2.exch)
		if ( -e $file) then
		    set ext = `echo $file | sed s/${2}'.'// `
		    mv $file $OUTPUT_TMP.$ext  >& /dev/null
		endif
	    end
	endif
	echo '     '
#  envoke x2dhf program
	nice +19 ./x2dhf < $1.data  
    endif

    if ( $1 != 'remove' ) then
	if ( $1 == 'rename' ) then
	    echo '                              '
	    echo ' program interrupted ... wait ... renaming files ...'
	    echo '                              '
      	else
	    echo '                              '
	    echo ' wait ... renaming files ...'
	    echo '                              '
      	endif

     	if ( "${3}" == '' ) then
#
#	move $OUTPUT_TMP.* into $2.* files and fort.* into $2.fort.* 
#
	    foreach file ( $OUTPUT_TMP.orb  $OUTPUT_TMP.coul  $OUTPUT_TMP.exch )
		if ( -e $file &~ -z $file ) then
		    set ext = `echo $file | sed s/${OUTPUT_TMP}'.'// `
		    mv $file $2.$ext  >& /dev/null
		else		
		    if ( $file == $OUTPUT_TMP.exch ) then
			if ( !(-e fort.$FIRST_FORT_FILE &~ -z fort.$FIRST_FORT_FILE) ) then
			    set ext = `echo $file | sed s/${OUTPUT_TMP}'.'// `
			    echo Warning: file $OUTPUT_TMP.$ext is missing or empty 
			endif
		    endif
		endif
	    end

	    if ( !( -e $2.exch ) )  then
		if ( -e fort.$FIRST_FORT_FILE &~ -z fort.$FIRST_FORT_FILE ) then
		    set fort = `ls fort.*`
		    foreach file  ($fort)
			set ext = `echo $file | sed s/'fort.'// `
  			mv $file ${2}.fort.$ext
		    end
		else
		    echo "Warning: (some of) files fort.* are missing"
		endif
	    endif
     	else
#
#	move $OUTPUT_TMP.* into $3.* files and fort.* into $3.fort.* if $OUTPUT_TMP.orb
#       and $OUTPUT_TMP.coul are nonzero 
#
#
       	    foreach file ($OUTPUT_TMP.orb $OUTPUT_TMP.coul $OUTPUT_TMP.exch)
		if ( -e $file &~ -z $file ) then
		    set ext = `echo $file | sed s/${OUTPUT_TMP}'.'// `
			mv $file $3.$ext  >& /dev/null
		else
		    if ( $file == $OUTPUT_TMP.exch ) then
			if ( !(-e fort.$FIRST_FORT_FILE &~ -z fort.$FIRST_FORT_FILE) ) then
			    set ext = `echo $file | sed s/${OUTPUT_TMP}'.'// `
			    echo Warning: file $OUTPUT_TMP.$ext is missing or empty
			endif
		    endif
		endif
	    end

	    if ( -e fort.$FIRST_FORT_FILE &~ -z fort.$FIRST_FORT_FILE ) then
		set fort = `ls fort.*`
		foreach file  ($fort)
		    set ext = `echo $file | sed s/'fort.'// `
		    mv $file ${3}.fort.$ext >& /dev/null
		end
	    else
		echo "Warning: (some of) files fort.* are missing"
	    endif
#
#	move $INPUT_TMP.* back into $2.* files 
#

	    foreach file ($INPUT_TMP.orb $INPUT_TMP.coul $INPUT_TMP.exch)
		if ( -e $file &~ -z $file ) then
		    set ext = `echo $file | sed s/${INPUT_TMP}'.'// `
		    mv $file $2.$ext  >& /dev/null
		else
		    if ( ($file == $INPUT_TMP.exch ) ) then
			if ( !(-e $3.fort.$FIRST_FORT_FILE &~ -z $3.fort.$FIRST_FORT_FILE) ) then
			    set ext = `echo $file | sed s/${INPUT_TMP}'.'// `
			    echo Warning: "file $INPUT_TMP.$ext  is missing or empty"
			endif
		    endif
		endif
	    end
	endif
    else
#
#        remove all temporary files  ($INPUT_TMP.*, $OUTPUT_TMP and fort.*) 
#
	echo '                              '
	echo ' program interrupted: wait ... removing temporary files ...'
	echo '                              '

	foreach file ($INPUT_TMP.orb $INPUT_TMP.coul $INPUT_TMP.exch)
		rm $file  >& /dev/null
	end

	foreach file ($OUTPUT_TMP.orb $OUTPUT_TMP.coul $OUTPUT_TMP.exch)
	    rm $file  >& /dev/null
	end

	if ( -e fort.$FIRST_FORT_FILE &~ -z fort.$FIRST_FORT_FILE ) then
	    set fort = `ls fort.*`
	    foreach file  ($fort)
		set ext = `echo $file | sed s/'fort.'// `
		rm $file >& /dev/null
	    end
	    endif
    endif
#
# remove all zero byte files
#
    set fort = `ls *`
    foreach file  ($fort)
	if ( -z $file ) then
	    rm $file >& /dev/null 
	endif
    end




