#!/bin/csh -f
unalias rm

set running = ".running.$$.`hostname`.`date +%d%m%H%M%S`"
echo $$ >$running
onintr clear
alias error	'echo ">>> ($name) \!* -> exit"; goto error'

set name	= $0		#full name of script-file
set bin		= $name:h	#default directory for WIEN-executables
if !(-d $bin) set bin = .
set name	= $name:t

set def 	= def		#def-file
set tmp		= ( )		#tmp-files, to be deleted before exit
set log		= :log		#log-file
set t		= time		#
set updn			# spinpolarization switch
set dnup	= 'dn'		# spinpolarization switch
set sc				# semicore-switch
set so				# spinorbit-switch
set sodum			# spinorbit-switch
set cmplx
set para                        # parallel execution 
set dopara                        # parallel execution 
set sel                         # standard vector file in lapw7
set band1                       # regular run in lapw1
unset orb                      # LDA+U in lapw1
unset iter                      # iterative diagonalization
unset nohns                     # without HNS in lapw1
unset qtl                       # regular run in lapw2
unset band                       # regular run in lapw2
unset fermi                     # regular run in lapw2
unset efg                       # regular run in lapw2
unset command			#command
unset file			#file-head
unset deffile_only		#create just def file
set scratch =                   #eg: /scr1/pblaha/   for vectors, help-files,

if ( $?SCRATCH ) then
  set scratch=`echo $SCRATCH  | sed -e 's/\/$//'`/ # we are afraid
				# different settings in different 
				# computing centers
			        #use global variable for scratch if set
endif

set argv1=($argv)

while ($#argv)
  switch ($1)
  case -f:
    shift; set file = $1
    shift; breaksw
  case -h:
  case -H: 
    set help
    shift; breaksw
  case -[t|T]:
    set t
    shift; breaksw
  case -up:
    set updn = 'up'
    set dnup = 'dn'
    shift; breaksw
  case -dn:
    set updn = 'dn'
    set dnup = 'up'
    shift; breaksw
  case -du:
    set du = 'du'
    set updn = 'up'
    shift; breaksw
  case -sc:
    set sc = 's'
    shift; breaksw
  case -d:
    set deffile_only
    shift; breaksw
  case -c:
    set cmplx = c
    shift; breaksw
  case -[p|P]
    set para = para
    shift; breaksw
  case -it:
    set iter
    shift; breaksw	
  case -orb:
    set orb
    shift; breaksw	
  case -nohns:
    set nohns
    shift; breaksw	
  case -so:
    set so = 'so'
    set sodum = 'dum'
    set cmplx = c
    shift; breaksw
  case -band:
    set band
    set band1='_band'
    shift; breaksw
  case -qtl:
    set qtl
    shift; breaksw
  case -efg:
    set efg
    shift; breaksw
  case -sel:
    set sel = f
    shift; breaksw
  case -fermi:
    set fermi
    shift; breaksw  
  default:
    set command = $1
    shift; breaksw
  endsw
end

if !($?file) then 
set file    = `pwd`
set file    = $file:t		#tail of file-names
endif


if ( $?help ) goto help

if !($?command) then
  error no command
endif

echo "`date`> ($name) $command $argv1[2-]" >> $log

set def	= $updn$command$sc.def
#touch $def

switch ($command)

case BoltzTraP:
set exe = BoltzTraP
cat << theend > $def
5,'$file.intrans',      'old',    'formatted',0
6,'$file.outputtrans',      'unknown',    'formatted',0
20,'$file.struct',         'old',    'formatted',0
10,'$file.energy$so$updn',         'old',    'formatted',0
theend
if (-e $file.vv) then
cat << vvend >> $def
15,'$file.vv',         'old',    'formatted',0
vvend
endif
cat << theend >> $def
-1,'$file', 'old','formatted',0
theend
breaksw

#48,'$file.engre',         'unknown',    'unformatted',0
#49,'$file.transdos',        'unknown',    'formatted',0
#50,'$file.sigxx',        'unknown',    'formatted',0
#51,'$file.sigxxx',        'unknown',    'formatted',0
#21,'$file.trace',           'unknown',    'formatted',0
#22,'$file.condtens',           'unknown',    'formatted',0
#24,'$file.halltens',           'unknown',    'formatted',0
#30,'${file}_BZ.cube',           'unknown',    'formatted',0
#35,'${file}_band.dat',           'unknown',    'formatted',0
#36,'${file}_band.gpl',           'unknown',    'formatted',0

default:
error	command $command does not exist
breaksw

endsw

if ($?deffile_only) then
  rm $running
  exit(0)
endif

if ( $para == para && $dopara) then
   if ( $updn == up ) then
      set exe = ($exe -up)
   else if ($updn == dn ) then
      set exe = ($exe -dn)
   endif
   if ($cmplx == c ) then
      set exe = ($exe -c)
   endif
   if ($so == so) then
      set exe = ($exe -so)
   endif
endif  	

echo $exe >>$running
$t $bin/$exe $def 
# rm $def $tmp
clear:
#cleanup
if ($?qtl || $?band || $?fermi || $?efg) then
  if( -e .oldin2 ) mv .oldin2 $file.in2$cmplx
#  if( -e .oldin1 ) mv .oldin1 $file.in1$cmplx
endif
if (-f $running) rm $running
exit(0)

error:
rm $running

help:					#help exit 
cat << theend 

USAGE:	$0 PROGRAMNAME [flags] 

PURPOSE: makes def files for BoltzTraP

FLAGS:
-f FILEHEAD ->	FILEHEAD for path of struct & input-files
-t/-T ->	suppress output of running time
-h/-H ->	help
-d    ->	create only the def-file 
-up   ->	runs up-spin
-dn   ->	runs dn-spin
-so   ->	runs lapw2/optic/spaghetti with def-file for spin-orbit calc.
theend
if !($?command) then
  set command
endif

switch ($command)

default:
echo 'USE: x -h PROGRAMNAME   for valid flags for a specific program'
breaksw

endsw
if (-e $running) rm $running

exit(1)
