#!/bin/zsh -f
 
#
# function ono to be run from Terminal or iTerm or xterm on OS X. Returns the
# focus to the terminal window after the program is launched.

# ono is run as a screen job so that it can be detached (backgrounded) with control-A d
# and resumed with screen -r

# The background sub-process returns focus to iTerm window after the application launches
# setopt no_notify prevents the notification from interfering with the application dialogue.
# The string "screen ono" is displayed in the iTerm tab or in the Terminal title bar as a
# reminder.

# Find the osx_ono executable:

if [[ -x /usr/local/ono/bin/osx_ono ]]; then
	ODAT=/usr/local/ono/data
	if [[ -x /usr/local/ono/bin/ono.app ]];then
		ono_exe=/usr/local/ono/bin/ono.app
	else
	    ono_exe=/usr/local/ono/bin/osx_ono
    fi
elif [[ -x /usr/local/ono/bin/osx86_ono ]];then
	ODAT=/usr/local/ono/data
	if [[ -x /usr/local/ono/bin/ono.app ]];then
		ono_exe=/usr/local/ono/bin/ono.app
	else
        ono_exe=/usr/local/ono/bin/osx86_ono
    fi
elif [[ -x /usr/local/xtal/ono/bin/osx_ono ]]; then
	ODAT=/usr/local/xtal/ono/data
	if [[ -x /usr/local/xtal/ono/bin/ono.app ]];then
		ono_exe=/usr/local/xtal/ono/bin/ono.app
	else
	    ono_exe=/usr/local/xtal/ono/bin/osx_ono
	fi
elif [[ -x /usr/local/xtal/ono/bin/osx86_ono ]];then
	ODAT=/usr/local/xtal/ono/data
	if [[ -x /usr/local/xtal/ono/bin/ono.app ]];then
		ono_exe=/usr/local/xtal/ono/bin/ono.app
	else
        ono_exe=/usr/local/xtal/ono/bin/osx86_ono
    fi
elif [[ -x $(which osx_ono) ]]; then
	ono_exe=$(which osx_ono)
	ODAT=$ono_exe:h:h/data
elif [[ -x $(which ono) ]];then
	ono_exe=$(which ono)
	ODAT=$ono_exe:h:h/data
elif [[ -x $(/usr/bin/locate osx_ono | head -n 1) ]];then
	ono_exe=$(/usr/bin/locate osx_ono | head -n 1)
	ODAT=$ono_exe:h:h/data
elif [[ -x $(/usr/bin/locate ono | head -n 1) ]];then
        ono_exe=$(/usr/bin/locate ono | head -n 1) 
		ODAT=$ono_exe:h:h/data
else
	print "Can\'t locate osx_ono."
	print "Terminating."
	return 1
fi
export ODAT

function ono {
  setopt no_notify
   function toodle_pip { 
	sleep 1
    if [[ $TERM_PROGRAM == iTerm.app ]]; then
        /usr/bin/open -a iTerm   
        # Returns focus to iTerm.app
        #
    elif [[ $TERM_PROGRAM == Apple_Terminal ]]; then
        /usr/bin/open -a Terminal    
        # Returns focus to Terminal.app
        #
    else
        /usr/bin/open -a X11  
        # Returns focus to xterm, i.e., X11.app
    fi 
  }
  toodle_pip &
  echo -ne "\e]1; screen ono $1 \a";  screen -S ono  $ono_exe  "$@"; settab
}


# run the function ono:

ono


##### end of file for function "ono"




