#!/usr/local/bin/bash
#
# This script is an example of how to call the appropriate viewer based upon
# the URL method
#
# Created by:  Michael Elkins <me@cs.hmc.edu> on March 10, 1997
# Modified by: Liviu Daia <daia@stoilow.imar.ro>
# Last Edited: May 26, 1997
# Modified by: Brian Salter-Duke <b_duke@lacebark.ntu.edu.au>
# Last edited: 19 June, 1999
# Modified by: Pete Toscano <sigsegv@psinet.com>
# Last edited: 20 Aug, 1999
#

url=$1
method=`echo $1 | sed 's;\(^[^:]*\):.*;\1;'`

# Start with http://.., ftp://.. and mailto:xxx@..
case $method in
    ftp)
# Use ncftp if URL ends in / and ncftpget if URL does NOT end in /
# N.B. ncftpget will fail if URL is directory not file.
# If ncftp is not present in /usr/bin or /usr/local/bin will use ftp.
	temp=`echo $url | sed 's/.$//;'`
        if [ $temp/ = $url ]; then
               if [ -x /usr/bin/ncftp -o -x /usr/local/bin/ncftp ]; then
                       ncftp $url
               else
                       ftp `echo $url | sed 's;^.*://\([^/]*\)/*.*;\1;'`
               fi
        else
               if [ -x /usr/bin/ncftpget -o -x /usr/local/bin/ncftpget ]; then
                       ncftpget $url
               else
                       ftp `echo $url | sed 's;^.*://\([^/]*\)/*.*;\1;'`
               fi
        fi
	;;

    http)   
# Will use netscape if running XWindow and netscape available,
# otherwise lynx. 
	if test x$DISPLAY = x; then
	    lynx $url
	else
            netscape -remote "openURL($url)" || netscape $url || lynx $url
	fi
	;;

    mailto)
# Uses mutt if present (and if not - why not?) otherwise mail.
         if [ -x /usr/bin/mutt -o -x /usr/local/bin/mutt ]; then
                mutt `echo $url | sed 's;^[^:]*:\(.*\);\1;'`
         else
                mail `echo $url | sed 's;^[^:]*:\(.*\);\1;'`
         fi
	;;

    *)
# Now for url's that start www... or ftp.... or xxxx@..
    method=`echo $url | sed 's;\(^...\).*;\1;'`
    case $method in
    ftp)
      temp=`echo $url | sed 's/.$//;'`
      if [ $temp/ = $url ]; then
            if [ -x /usr/bin/ncftp -o -x /usr/local/bin/ncftp ]; then
                 ncftp "ftp://"$url
            else
                 ftp `echo "ftp://"$url | sed 's;^.*://\([^/]*\)/*.*;\1;'`
            fi
      else
            if [ -x /usr/bin/ncftpget -o -x /usr/local/bin/ncftpget ]; then
                  ncftpget "ftp://"$url
            else
                  ftp `echo "ftp://"$url | sed 's;^.*://\([^/]*\)/*.*;\1;'`
            fi
 	           ncftpget "ftp://"$url
      fi
      ;;

    www)
      target="http://"$url
      if test x$DISPLAY = x; then
	    lynx $target
      else
        netscape -remote "openURL($target)" || netscape $target || lynx $target
      fi
      ;;

    *)
      if [ -x /usr/bin/mutt -o -x /usr/local/bin/mutt ]; then
         mutt  $url 
      else
         mail $url 
      fi
      ;;
    esac
    ;;
esac

