#!/bin/csh -f
#
#  ---------------------- rmosaic ------------------------------------
#  Name: rmosaic
#
#  Purpose:  script to run mosiac job and optionally look up url keyword
#            allow site to share mosaic binaries on a single an X-host  
#	     for machines without a local copy.
#
#  Calling Sequence:
#     rmosaic			[just start mosaic, NCSA home page]
#     rmosaic KEYWORD		[match keyword in URL lookup table]
#     rmosaic URL		[pass explicit URL]
#
#  Site Customization:
#     setenv mosaic_host SITE_MOSAIC_HOST       (machine with mosaic)
#     setenv mosiac_path SITE_MOSIAC_PATH	(default /usr/local/bin/mosaic)
#
#  Restrictions:
#     caller must have rsh priviledge on mosaic host
#
#  TODO - allow personal url hotlist to supercede site url hotlist
#
#  SLF - 22-Jan-1995
#  SLF - 25-Jan-1995 (add check for SGI - `which mosaic` output different
#  SLF - 26-Jan-1995 (background it)
#  SLF - 26-Mar-1995 (OSF kludge - protect against $chklocal[1]  ** ?? **) 
#  SLF - 18-mar-1996 (make 'rnetscape' from 'rmosaic')
# -------------------------------------------------------------------
#

set url=""
if ($#argv > 0) then
# try look up in table 
   set url="$argv[1]"			   # pass unknown url through to mosaic
   set urltable=$ys/gen/data/url.solar
   if (-e $urltable) then		   # proto-yohkoh/solar url list
      if ($url == help) then
         echo "Calling Sequence:"
	 echo "   rmosaic		[just start mosaic, NCSA home page]"
	 echo "   rmosaic KEYWORD	[match keyword in URL lookup table]"
	 echo "   rmosaic URL		[pass explicit URL to mosaic]"
         echo " "
         echo ---------------- $urltable -------------------
         more $urltable
         echo ---------------------------------------------------------------
         exit
      else
         set lookup =(`grep -i $url $urltable`)     
         if ($#lookup > 0) then
            echo "Using URL $lookup"
            set url="$lookup[1]"     
         else
            set chkhttp=`echo $url | grep -i http:`
            if ("$chkhttp" == "") then
               echo "Could not lookup URL and not full http address..."
               echo "Try: % rmosaic help"
               exit
            endif
         endif
      endif
   endif
endif

set chklocal=`which netscape`

if ($#chklocal == 0) then
   set chklocal="netscape:"
endif

if (($chklocal[1] == "netscape:") || ($chklocal[1] == "no") || ($chklocal[1] == "netscape"))  then
   set chklocal=`which xmosaic`		# local name
   if ($#chklocal == 0) then
      set chklocal="xmosaic:"
   endif 
   if (($chklocal[1] == "xmosaic:") || ($chklocal[1] == "no") || ($chklocal[1] == "xmosaic"))  then
       echo "Cannot find local version, checking environmental: <mosaic_host>"
       if (!($?mosaic_host)) then 
          echo "UNDEFINED: Define environmental <mosaic_host> and retry..."
         exit
       else
          echo "Trying environmental <mosaic_host> "$mosaic_host
       endif        
   else
      setenv mosaic_host `hostname` 
      setenv mosaic_path $chklocal[1]
   endif
else
   setenv mosaic_host `hostname` 
   setenv mosaic_path $chklocal[1]
endif


if (!($?mosaic_path)) then 
   setenv mosaic_path /usr/local/bin/netscape
endif

if $?DISPLAY then 
   if ($DISPLAY == ":0.0") then
      set rdisp="`hostname`"
   else
      set rdisp="`printenv DISPLAY`"
   endif
else
   set rdisp="`hostname`"
endif

set chk0=`echo $rdisp | grep :0`

if ("$chk0" =="") then
   set rdisp="$rdisp":0
endif

set rmcmd="setenv DISPLAY $rdisp"

set host=`hostname`
if ($host == $mosaic_host) then 
    echo "...... Spawing Local Mosaic: "$mosaic_path" ..........."
    echo " "
    eval "setenv DISPLAY $rdisp; $mosaic_path $url "&
else 
#   check rsh priviledge
   if (-e /usr/bsd/rsh) then 
      set rpath=/usr/bsd
   else
      if (-e /usr/ucb/rsh) then
         set rpath=/usr/ucb
      else
         set chkrsh=`whereis rsh`
         if ($#chkrsh > 1) then
            set rpath=$chkrsh[2]:h
         else
            echo "Cannot find rsh on your machine..."
            exit
         endif
      endif
   endif
   set rtest="date | grep -i denied"
   set rcmd="$rpath/rsh $mosaic_host"
   set test="`$rcmd $rtest`" 

   if ("$test" != "") then
      echo "You do not have "RSH" priviledge to $mosaic_host..."
      exit
   endif
   echo "........ Spawning Remote Mosaic Job ..........."   
   echo " "
   $rpath/rsh $mosaic_host "setenv DISPLAY $rdisp; $mosaic_path $url "&
endif

exit
