#!/bin/ksh -p # Initialise LOCATION_FILE=~/.ut_locations PROG=$0 # Pathname I=${0##*/} # Filename PATH=/opt/SUNWut/bin/:$PATH UTWHO=$(whence utwho) if [[ ! -x $UTWHO ]]; then print -u2 \ "Error: $I: $UTWHO Command not found!\nis this a Sun Ray client?" exit 1 fi unset WINDOWID # zenity could hang itself if parent window disappears. if [[ -x /usr/bin/pidgin ]]; then # Prefer pidgin. IM=/usr/bin/pidgin REMOTE=/usr/bin/purple-remote elif [[ -x /usr/bin/gaim ]]; then # Not tested! IM=/usr/bin/gaim REMOTE=/usr/bin/gaim-remote else print -u2 "Error: $I: gaim/pidgin not found" exit 1 fi function usage { print "usage: $I [-vxX][[-i]|[-d]|[-s]|[-t]|[-l]|[-c]|[-a [description]]] -a Add location, prompts for description if none supplied. Colon characters in description are substituted by space character. -l Display contents of location file. -i Initialise; starts utaction monitors for connect/disconnect Will also start IM client if none active for user. Kills current \"utaction $I\" scripts -c Update pidgin with current description: This option is intended to be used by \"utaction -c\" If description is unknown then a description dialogue is requested using zenity. If description contains \"conference\" then status is set to \"unavailable\". -d Update IM tool with away message (unless away status already set). This option is intended to be used by \"utaction -d\" -t Display current description. -v Text Verbose mode. -V Verbose information using zenity, not as much as text though. -x Debug -X Debug functions Without any options the appliance name and description are displayed" } function audio_wrap { ## see if [[ -z ${UTAUDIODEV} ]]; then exec ${1+"$@"} # Simply Run the application else AUDIODEV=$(/opt/SUNWut/bin/utaudio $$ "${1##*/}") trap 'pkill -f "utaudio.*$$"; exit 1' 0 1 2 3 15 LD_PRELOAD=libc_ut.so # In case the application opens /dev/audio ${1+"$@"} # Run the application pkill -f "utaudio.*$$" # This cleans up the utaudio process fi } function get_location { verbose "Looking for location in $LOCATION_FILE" if [[ -f $LOCATION_FILE ]]; then nawk -F: '$1 == "'$1'" { print $2 }' $LOCATION_FILE fi } #function get_saved_resolution { # verbose "Looking for resolution in $LOCATION_FILE" # if [[ -f $LOCATION_FILE ]]; then # nawk -F: '$1 == "'$1'" { print $3 }' $LOCATION_FILE # fi #} function save_location { verbose "Saving location to $LOCATION_FILE" typeset rest # Remove old entry if it exists if [[ -f $LOCATION_FILE ]]; then if grep -v "^${1}:" $LOCATION_FILE > $LOCATION_FILE.$$; then rest=$(grep "^${1}:" $LOCATION_FILE | cut -d: -f3-) mv $LOCATION_FILE.$$ $LOCATION_FILE fi fi # Append new entry: Device:Description[:rest] echo "${1}:${2}${rest:+:${rest}}" >> $LOCATION_FILE } function status_availability { if [[ ${1} == *conference* ]]; then $REMOTE \ 'setstatus?status=unavailable&message='"$1" else $REMOTE \ 'setstatus?status=available&message='"${1:-At Office}" fi } function status_disconnected { if $REMOTE getstatus | grep away > /dev/null; then verbose "Away status already set... leaving as is" return fi $REMOTE 'setstatus?status=away&message=Away from Sun Ray' } function switch_res { typeset xrandr=/usr/bin/X11/xrandr # Check for command and XrandR support. if [[ ! -e $xrandr ]] || ! $xrandr -v >/dev/null 2>&1 ; then verbose -z "xrandr not found or not supported" return fi utset=$(whence utset) utres=$(${utset} | grep '^Display Timing 1:' | cut -d\ -f4) # XxY@Hz # res=$(get_saved_resolution) xrres=$($xrandr -q | nawk '/^\*/ {print $2 "x" $4}') # XxY if [[ ${utres%@*} == ${xrres} ]]; then verbose -z "utset matches xrandr resolution (${utres%@*} $xrres)" return fi verbose -z "Switching from $xrres to $utres" /usr/bin/X11/xrandr -s ${utres%@*}& } function initialise_im { if ! pgrep -u $LOGNAME -x ${IM##*/} >/dev/null; then verbose "Starting new IM process: $IM" audio_wrap $IM& sleep 1 # Give application a chance to start... fi status_availability "$THIS_LOC" # Kill off older utaction scripts verbose "Reaping prior $I utaction processes and starting anew" pkill -fu $LOGNAME "utaction.*$I" # Start new utaction scripts. utaction -d "$PROG -d${VERBOSE_f}"& utaction -c "$PROG -c${VERBOSE_f}"& (pwait $(pgrep -u $LOGNAME -x ${IM##*/}); pkill -fu $LOGNAME "utaction.*$I")& } function verbose { if [[ $1 == "-z" ]]; then shift if [[ -n $VERBOSE_f ]]; then zenity --info --timeout 10 --title "$I" --text "$*"& fi fi [[ -n $verbose_f ]] && print -u2 "$I: $*" } function show_appliance { print "Appliance \"${THIS_APPLIANCE}\" is \"${THIS_LOC:-Unknown}\"" } # Main code starts here. THIS_APPLIANCE=$($UTWHO -c | nawk -v d=${DISPLAY#:} '$1 == d { print $5 }') THIS_LOC=$(get_location ${THIS_APPLIANCE}) while getopts :adchilrstvVxX c do case ${c} in d) disconnect_f=1;; # Disconnected, status away. a) add_f=1;; # Add location. i) init_f=1;; # Initialise pidgin & utaction. l) list_f=1;; # list location file. c) connect_f=1;; # Connected, status available s) switch_f=1;; # Not documented! t) tell_f=1;; # Print location v) verbose_f=1;; # Verbose mode. V) VERBOSE_f="$c";; # Zenity Verbose mode. X) typeset -ft $(typeset +f);; # ksh function debug x) set -x;; # ksh debug. h) usage; exit;; \?) print "$I: $c unknown option. Use \"$I -h\" for help." exit 1 ;; esac done shift OPTIND-1 if [[ -n $switch_f ]]; then switch_res exit elif [[ -n $tell_f ]]; then # simply print location, useful for other scripts print -- ${THIS_LOC} exit elif [[ -n $list_f ]]; then cat $LOCATION_FILE exit fi # Remaining options require IM client if [[ -x /usr/bin/pidgin ]]; then # Prefer pidgin. IM=/usr/bin/pidgin REMOTE=/usr/bin/purple-remote elif [[ -x /usr/bin/gaim ]]; then IM=/usr/bin/gaim REMOTE=/usr/bin/gaim-remote else print -u2 "Error: $I: gaim/pidgin not found" exit 1 fi if [[ -n $init_f ]]; then initialise_im elif [[ -n $disconnect_f ]]; then status_disconnected elif [[ -n $connect_f ]]; then # status_availability "$THIS_LOC" switch_res if [[ -z $THIS_LOC && -e /usr/bin/zenity && -n $DISPLAY ]]; then verbose "Requesting Sun Ray location via zenity" description=$(zenity --timeout 300 --title "$I" --entry \ --text "Enter the location for this Sun Ray device") description=$(echo "$description" | sed -e 's/:/ /g') if [[ $? == 0 && -n $description ]]; then save_location $THIS_APPLIANCE "$description" # Replace colons! status_availability "$description" fi fi elif [[ -n $add_f ]]; then # Update data file with location if [[ -z "$*" ]]; then read description?"Location description: " else description="$*" fi description=$(echo "$description" | sed -e 's/:/ /g') # Replace colons! if [[ -n $description ]]; then # Allow for EOF save_location $THIS_APPLIANCE "$description" status_availability "$description" else print "Location not specified; leaving as current setting:" show_appliance fi else show_appliance fi