######################################################################
#!/bin/csh
#
# shell-script for opening 3 viewers
#


#check pipes
if ((! -w pipe1.fifo) || (! -w pipe2.fifo) || (! -w pipe3.fifo)|| (! -w pipe4.fifo)) then
	if (-o "./") then
		echo "Creating named-pipes..."
		mknod pipe1.fifo p
		mknod pipe2.fifo p
		mknod pipe3.fifo p
		mknod pipe4.fifo p
		chmod 666 pipe1.fifo
		chmod 666 pipe2.fifo
		chmod 666 pipe3.fifo
		chmod 666 pipe4.fifo
	else
		echo "There are no named-pipes, and you do not own this"
		echo "directory. Rerun this as owner."
		exit
	endif
endif
@ i=1

set f1=""
set f2=""
set f3=""
set pfx="_"
if ($?REMOTEHOST) then
  set rh=$REMOTEHOST
else
  set rh=""
endif

foreach arg ($*)
  if ("$arg" == "-full") then
	set _FULL   
	set argv[$i]=""

  else if ("$arg" == "-nos3d") then
	set f1=" -f inner_nos3d.iv " 
	set f2=" -f inner_nos3d.iv " 
	set f3=" -f inner_nos3d.iv " 

  else if ("$arg" == "-kill") then
	set _KILL 

  else if ("$arg" == "-swap") then
	set _SWAP 

  else if ("$arg" == "-second") then
	if ("$f1"=="") then
		set f1=" -f _inner.iv " 
		set f2=" -f _outer1.iv " 
		set f3=" -f _outer2.iv " 
	endif
	set _SECOND
	set pfx="_"
  endif
  @ i++
end

#do we have to kill ?
if ($?_KILL) then
killall ${pfx}viewer
exit
endif


# if we have full-screen open on 3 displays
if ($?_FULL) then
@ w=1280
@ h=1024
@ x1=0
@ x2=0
@ x3=0
set d1=$rh":0.0"
if ($?_SWAP) then #swap display 0.1 and 0.2
  set d2=$rh":0.1"
  set d3=$rh":0.2"
else
  set d2=$rh":0.2"
  set d3=$rh":0.1"
endif

#otherwise open small
else
@ w=400
@ h=400
@ x1=$w
@ x2=0
@ x3=$w * 2
set d1=$rh":0.0"
set d2=$rh":0.0"
set d3=$rh":0.0"

endif

${pfx}viewer
${pfx}viewer 1 $* -geometry  $f1 $w"x"$h"+"$x1"+0" -display $d1 &
sleep 1
${pfx}viewer 2 $* -geometry  $f2 $w"x"$h"+"$x2"+0" -display $d2 &
${pfx}viewer 3 $* -geometry  $f3 $w"x"$h"+"$x3"+0" -display $d3 &
