#!/bin/zsh -f

# Feb 3rd, 2006

# Use an x-11-based browser when logged in remotely via ssh
# Use an OS X-based browser (or not) when using the OS X GUI interface locally.

# set -x  # Uncomment to debug

if [[ -n $SSH_TTY ]];then

    use_xbrowser='true'
    if [[ -n $CNS_REMOTE_BROWSER ]];then
        CNS_BROWSER=( $CNS_REMOTE_BROWSER )
    elif [[ -x $(which firefox | head -n 1) ]]; then
        CNS_BROWSER=(firefox)
    elif [[ -x $(which konqueror | head -n 1) ]]; then
        CNS_BROWSER=(konqueror)
    else
        print "Suitable X-windows-based browser not found"  
        print 'Please set the variable $CNS_REMOTE_BROWSER to your desired browser, eg: '
        print ''
        print '     export CNS_BROWSER=(konqueror)'
        print ''
        print 'to use the konqueror Browser'
        return 10        
    fi
    
else

    function FindaBrowser {
      if [[ -x /usr/bin/mdfind ]];then
        fullpath=$(mdfind -onlyin /Applications "kMDItemContentType == 'com.apple.application-*'" | grep "$OSXBROWSER" | head -n 1 )
         if [[ -d $fullpath ]];then
            basename "$fullpath"
         else
            print ""
         fi

      else
        print "Suitable browser not found"  
        print 'Please set the variable $CNS_BROWSER to your desired browser, eg: '
        print ''
        print '     export CNS_BROWSER=(open -a Camino)'
        print ''
        print 'to use the Camino.app Browser'
        return 11 
      fi
    }

    #  IF the OS X browser is not defined with $CNS_BROWSER, try to find a suitable one
    #  This goes in order of my personal preference based on functional utility
    
    
    
    
    foreach OSXBROWSER  (  Camino.app Mozilla.app Netscape.app Safari.app Firefox.app )
    
        if [[ -n $(FindaBrowser) ]];then
            CNS_BROWSER=(open -a $OSXBROWSER )
            export CNS_BROWSER
            break
        else
            :
        fi
    end
      
    
    if [[ -z $CNS_BROWSER ]];then
        print "Suitable browser not found"  
        print 'Please set the variable $CNS_BROWSER to your desired browser, eg: '
        print ''
        print '     export CNS_BROWSER=(open -a Camino)'
        print ''
        print 'to use the Camino.app Browser'
        return 1
    fi
    
fi

if [[ $(basename $CNS_SOLVE) ==  cns_solve_1.21 ]];then 
   $CNS_BROWSER  http://cns.csb.yale.edu/v1.21  >/dev/null 2>/dev/null
else
   $CNS_BROWSER  http://cns.csb.yale.edu/v1.2   >/dev/null 2>/dev/null
fi

#   if [[ -n $CNS_SOLVE/doc/html/cns_solve.html ]];then
#   
#       if [[ -f $CNS_SOLVE/doc/html/cns_solve.html ]];then
#       
#           $CNS_BROWSER $CNS_SOLVE/doc/html/cns_solve.html >/dev/null 2>/dev/null  
#           
#       else
#           print "The file  $CNS_SOLVE/doc/html/cns_solve.html is not present."
#           print "Please check the integrity of your cns installation."
#           return 21
#       fi
#       
#   else
#           print 'The variable $CNS_SOLVE must be defined for CNS to work correctly'
#           return 22
#   fi

 
