#!/bin/csh -f
#
#moving case.vect* to *.old vector files; takes -p; -up/-dn
#copying case.vectorup* to case.vectordn*; takes -p; -up/-dn/-updn
# 
set useremote   = 1             # using remote shell to launch processes
set remote = ssh
set file=`pwd`
set file=$file:t		#tail of file-names

set name	= $0
set bin		= $name:h	#default directory for WIEN-executables
if (-e $bin/parallel_options) then
	source $bin/parallel_options
endif
if ( $?USE_REMOTE ) then
        set useremote = $USE_REMOTE
endif

if( "$1" == '-h' ) goto help

unset para
if( "$1" == '-p' ) then
  set para
  shift
set machines=`grep init .processes | cut -f2 -d: | cut -f1 -d ' ' | xargs`
endif
set copy
set updn
if ( "$1" == '-up' ) set updn=up
if ( "$1" == '-dn' ) set updn=dn
if ( "$1" == '-updn' ) then
   set copy=dn
   set updn=up
endif
if ( "$1" == '-dnup' ) then
   set copy=up
   set updn=dn
endif

  set scratch=./                   # set directory for vectors and help files
  if ($?SCRATCH) then              #if envronment SCRATCH is set
   set scratch=`echo $SCRATCH  | sed -e 's/\/$//'`/ #set $scratch to that value
  endif                          
  cd $scratch
  setenv PWD $cwd

if( ! $?para ) then
  foreach i ($file.vector${updn}*)
  set i1=`echo $i |grep -v -e '\.old'`
  if ( $i1 != '' ) then
    if (! -z $i ) then
     if( $copy == '') then
       mv $i1 $i1.old
       if($status == 0 ) echo "moving $i1 to $i1.old"
     else
       set target=`echo $i1 | sed s/vector${updn}/vector${copy}/`
       cp $i1 $target
       if($status == 0 ) echo "copy $i1 to $target"
     endif
    endif
  endif
  end
#----------------------------------------------------------

else 

set i=1
while ( $i <= $#machines )
  if( $copy == '') then
    if ($useremote == 1) then
       $remote $machines[$i] "cd $PWD;mv $file.vector${updn}_$i $file.vector${updn}_$i.old" 
       if($status == 0 ) echo moving vector on $machines[$i] to $file.vector${updn}_$i.old 
    else
       mv $file.vector${updn}_$i $file.vector${updn}_$i.old
       if($status == 0 ) echo moving vector to $file.vector${updn}_$i.old 
    endif
  else
    set target=`echo $file.vector${updn}_$i | sed s/vector${updn}/vector${copy}/`
    if ($useremote == 1) then
       $remote $machines[$i] "cd $PWD;cp $file.vector${updn}_$i $target" 
       if($status == 0 ) echo copying vector$updn on $machines[$i] to $file.vector${copy}_$i 
    else
       cp $file.vector${updn}_$i $target
       if($status == 0 ) echo copying vector$updn to $target
    endif
  endif
  @ i ++
end
endif

exit 0
help:
echo usage:    vec2old_lapw [-h -p -up/-dn/-updn] 
echo vec2old moves case.vect* to *.old; takes -p (using .processes); -up/-dn
echo vec2old -updn     copies case.vectorup* to case.vectordn*; takes -p; 
echo -p uses .processes file, requires commensurate k-points/proc-number
exit 1
