#!/bin/csh -f
set bestbin = $besthome/bestbin
#
# dna says width  < 0.2 is unrealystic
set width=0.2 
#
set arg = ( $argv )
# pars the command line
set narg = 1
set ev = 1
set dose = 0
set minitime = d
set dnafile = /dev/null
while ( $narg <= $#arg )
   switch ( $arg[$narg] )
      case -h
         set ev = 0
         goto help
	 breaksw
      case -g
	 set graph
	 breaksw
      case -i
	 set intera
	 breaksw
      case -m
         @ narg++
         if ( $narg > $#arg ) goto help
         set multyread = $arg[$narg]
	 breaksw
      case -l
         cat $besthome/detector-inf.dat
	 exit
	 breaksw
      case -o
         @ narg++
         if ( $narg > $#arg ) goto help
         set graph
	 set mtvout = $arg[$narg]
	 breaksw
      case -f
         @ narg++
	 if ( $narg > $#arg ) goto help
         set format = $arg[$narg]
         breaksw
      case -t 
         @ narg ++
	 if ( $narg > $#arg ) goto help
	 set exptime = $arg[$narg] 
	 breaksw
      case -s 
         @ narg ++
	 if ( $narg > $#arg ) goto help
	 set insca = $arg[$narg]
	 breaksw
      case -r 
         @ narg ++
	 if ( $narg > $#arg ) goto help
	 set reso  = $arg[$narg]
	 breaksw
      case -e
         @ narg ++
	 if ( $narg > $#arg ) goto help
	 set phis  = $arg[$narg]
	 breaksw
      case -d
         @ narg ++
	 if ( $narg > $#arg ) goto help
	 set dose  = $arg[$narg]
	 breaksw
      case -q
	 set minitime = t 
	 breaksw
      case -p
	 set planonly 
	 breaksw
      case -v
         set verbose
	 set echo
	 breaksw
      case -3
      case -5
      case -10 
	 if ( $narg > $#arg ) goto help
	 set isigi  = `echo $arg[$narg] | tr -s '-' ' ' `
	 breaksw
      case -mosflm
      case -mosfl
      case -mosf
      case -mos
      case -mo
#           mosfilm, no image input
	 set mosflm
	 if ( $?best_exposure_timer ) unsetenv best_exposure_timer
	 if ( $?best_multyread ) unsetenv best_multyread
	 breaksw
      case -dna
         @ narg++
	 if ( $narg > $#arg ) goto help
         set dnafile = $arg[$narg]
	 breaksw
      default:
         set image = $arg[$narg] 
	 @ narg++
	 set dotx
	 set ndotx = 0
	 while ( $narg <= $#arg )
	     set dotxadd  = $arg[$narg]
	     @ ndotx++
	     set dotx = ( $dotx $dotxadd )
	     @ narg++
	 end
   endsw
   @ narg++
end
#
# verify if all the necessary input is input:
# format, image, .x file(s) and exposure time
if !( $?image     )then
   if ( $?mosflm ) then
      echo 'ERROR: no mosflm files specified' >& /dev/tty
      goto help
   endif	
   echo 'ERROR: no image file specified' >& /dev/tty
   goto help
endif
if !( $?ndotx     ) then
   echo 'ERROR: no x file specified' >& /dev/tty
   goto help
endif
if  ( $ndotx < 1  )then
   if ( $?mosflm ) then
      echo 'ERROR: not enough mosflm files' >& /dev/tty
      goto help
   endif	
   echo 'ERROR: no x file specified' >& /dev/tty
   goto help
endif
if  ( ( $?mosflm ) &&  ( $ndotx < 2 )  )then
   echo 'ERROR: not enough mosflm files' >& /dev/tty
   goto help
endif
if !( $?format    ) then 
    if ! ( $?best_default_detector ) then
         echo 'ERROR: no detector specified' >& /dev/tty
         goto help
    endif
    set format = $best_default_detector
    echo Default detector $best_default_detector assumed.        
    if ! ( $?multyread   ) then
        if ( $?best_multyread ) then
            set multyread = `$best_multyread $image`
            if $status exit 1
	    if ( $multyread != 1 ) then
	       echo Multyread 2
	    endif
        else
            set multyread = 1
        endif
   endif
endif
if ! ( $?exptime ) then
   if ( $?best_exposure_timer ) then
       set exptime = `$best_exposure_timer $image`
       if $status exit 1
       echo Exposure time = $exptime seconds.
   else
       echo 'ERROR: no exposure time specified' >& /dev/tty
       goto help
   endif
endif
if !( $?multyread ) set multyread = 1
#
if !( $?reso  ) set reso  = 0.1
if !( $?isigi ) set isigi = 3 
#
#
if !( $?SYMOP ) then
    setenv SYMOP ${CLIBD}/symop.lib
endif
#
if ( $?insca ) then
    if ( $?mosflm ) then
       echo 'ERROR: use either mosflm or scalepack' >& /dev/tty
       goto help
    endif		
    set ewtype = s
    set xsarg = ( $dotx[1] $insca )
else
    set ewtype = x
    set xsarg = ( $dotx )
endif
if ( $?mosflm ) set ewtype = m
# 
if ( $?graph ) then
    set ewmtv = ( $CCP4_SCR/wilson_err.mtv_$$ $CCP4_SCR/wilson.mtv_$$ )
    if $status exit 1
    set esmtv = ( $CCP4_SCR/phimax.mtv_$$ $CCP4_SCR/complit.mtv_$$)
    if $status exit 1
    onintr -
    set toclean = ( $ewmtv $esmtv ) 
else
    set ewmtv = ( /dev/null /dev/null )
    set esmtv = ( /dev/null /dev/null )
endif
#
#  
#
set command =  $bestbin 
set command = ( $command $besthome/detector-inf.dat )
set command = ( $command  $besthome/wils_id          )
set command = ( $command  $format  )         
set command = ( $command  $exptime )                 
set command = ( $command  $multyread )                 
set command = ( $command  $dose )                 
set command = ( $command  $minitime  )
set command = ( $command  $ewtype )                  
set command = ( $command  $ewmtv $esmtv )
set command = ( $command  $dnafile )              
set command = ( $command  $image )                   
set command = ( $command  $xsarg )                
if ( $?verbose ) echo $command
if ( $?intera ) then
#    cat | head -3 > /tmp/beststdin
#    $command < /tmp/beststdin
    $command
else 
   if( $?planonly ) then
       alias filt "awk '/plan table start/,/plan table finish/ {print}' | grep -v plan "
   else
       alias filt "grep -v 'Please give instruction (h - help)'"
   endif
   if ( $?phis ) then
      $command <<EOF |  filt
         width $width
         split $phis
         plan $reso $isigi
    	 stats
         end
EOF
   else 
      $command <<EOF |  filt
         width $width
         plan $reso $isigi
	 stats
	 end
EOF
   endif
endif
if $status goto cleenup
#
#
if ( $?graph )  then
   plotmtv  -nodate -geom 550x400+0+0 -pfg white -pbg black  $CCP4_SCR/*mtv_$$ >& /dev/null &
   sleep 2
endif
#
if ( $?mtvout ) then
    echo > $mtvout
    foreach x ( $toclean )
        if -rw $x cat $x | grep -v '\$ END' >> $mtvout
        if $status goto cleenup
    end
endif
#
set ev = 0
#
cleenup:
if ( $?toclean ) then
    foreach x ( $toclean )
        if -rw $x /bin/rm $x
        if $status exit 1
    end
endif
exit $ev
#
#
#
help:
echo usage:: `basename $0` "[ -r <n> -3|5|10 -d <n> -p <n> -q -h -g -i -l -m <n> -s file -o file -t <secs>  -f <detector>] <image file1.x {file2.x ...}> <-mo(sflm) file.dat file.par file0.hkl {file1.hkl ...}>"
echo
echo " -h  gives this help message"
echo " -r <aimed resolution in \0305 (Angstrom)>, default as for the image "
echo " -d <preset counts>, default time mode"
echo " -q minimize the  total time, default minimize the absorbed dose"
echo " -3|5|10, aimed I/SigI at highest resolution, default 3"
echo " -e <maximum number of plan entries>, default up to 36 "
echo " -i  activate interactive input. Ignores -rpe3|5|10. Default not."
echo " -t <exposure time in seconds>"
echo " -m {<number of detector read outs>}"
echo " -f <format> "
echo " -s <.sca file name>, default - use .x file(s)"
echo " -l show configured image formats" 
echo " -g  some usefull plots generated (plotmtv required), default not"
echo " -o <file> plots generated and stored in <file>"
echo " -p output data collection plan only"
echo " -dna <file> xml formatted data stored in <file> "
exit $ev
#










