Jump to content
Not connected, Your IP: 13.59.112.169
Sign in to follow this  
luxc0r0

Sharing my goldcrest terminal script (bash)

Recommended Posts

Good evening!

I've kept meaning to clean this up and improve it so that I could share it with people here, but since it's doing what I need it to, I haven't done any work so I might as well share what I came up with. It might be useful to someone trying to accomplish the same thing or use it as a starting point for something with potentially a lot more features.

The script is launched from outside a terminal; it opens a terminal and runs goldcrest inside of it; the terminal output is duplicated so that notifications can appear when certain strings are matched in the terminal output. Originally the terminal was iconified to the systray via kdocker but I found my approach to be buggy for kdocker so I just let my window manager deal with it now; the window is always retrievable in order to hit Control+C to disconnect. 

It was designed for my use to do what I needed it to do, nothing more. Someone could find it handy. Sorry I never developed it further. It should be basically portable with minor modifications, i.e. the preferred terminal emulator, sounds, icons

 

#!/usr/bin/env bash

# Control interface with notification daemon for goldcrest.
# by luxc0r0 with help from ~rts/oarion7

# THANKS TO AIR VPN STAFF: 
# https://airvpn.org/forums/topic/52600-bash-attempting-to-direct-output-of-goldcrest-to-named-pipe-in-script/
# The problem is related to tee when it processes buffered fast output (Goldcrest and Bluetit are very fast). See also: https://askubuntu.com/questions/639877/tee-doesnt-get-whole-output-from-the-pipe
# By changing the initial standard out stream to "unbuffered" you will resolve the problem. Example:
# stdbuf -o0 ./goldcrest --air-connect | tee /tmpfs/gc.log

# Designed to run from a launcher, i.e. without a parent terminal;
# a child terminal pops up automatically and runs main functions 
# Output of goldcrest is duplicated into a pipe and filtered to 
# notify-send. Terminal can then be hidden or minimized. 

# User can restore terminal as needed; two pipes are used so that
# Ctrl+C can be run on goldcrest rather than the container script.

# TO DO: Kdocker has been disabled; occasionally it was duplicating 
# processes and maxing out the CPU. Letting window manager take over 
# handling the extraneous terminal window and calling it when needed

popup_terminal="st" #tested: st, xfce4-terminal
term_title="Goldcrest Interface"
systray_icon="$HOME/Icons/Mine/goldcrest.png"  #no longer used
notify_icon="$HOME/Icons/Mine/goldcrest_tr.png"
connect_sound="$HOME/Audio/scifisounds/PremiumBeat_0013_cursor_click_11.wav"
notify_timeout="10" #seconds

deps=( xdotool notify-send goldcrest $popup_terminal ) # kdocker and alltray not reliable anymore
for i in "${deps[@]}"; do command -v "$i" >/dev/null || 
{ notify-send "$0" "Dependency required: $i"; exit 1; }
done

tmp_dir='/mnt/ramdisk' ; [[ ! -e "$tmp_dir" ]] && tmp_dir='/tmp' 

# naughty is my bash script connected to AwesomeWM API; in place by 
# default for color theming; use regular notify-send if not present

notify() {
	"$HOME/scripts/./naughty" "$term_title" "$1" "$2" "$3" \
    "$notify_icon" "$notify_timeout" & 2>/dev/null
}

[[ ! -f "$HOME/scripts/./naughty" ]] && {
    notify() {
        notify-send "$term_title" "$1" -i "$notify_icon" \
        -t "$(( "$notify_timeout" * 1000 ))" & 2>/dev/null
        }
    }

popup_wrapper() { 

    [[ "$popup_terminal" == "st" ]] && {
    setsid "$popup_terminal" -c GoldCrest -t "$term_title" -e bash -c "$0 --child; bash" ;
    return
    }

    setsid "$popup_terminal" -T "$term_title" -e "bash -c \"$0 --child; bash\"" 
}

pipe="${tmp_dir}/.goldcrest_fifo1"
int="${tmp_dir}/.goldcrest_fifo2"

if [[ "$1" == "--child" ]] ; then

    [[ ! -e "$pipe" ]] && { mkfifo "$pipe"; }
    [[ ! -e "$int" ]] && { mkfifo "$int"; }

    #launch notification daemon if not already running
	[[ "$(ps -aux | grep "$0 --notif[y]")" ]] || { "$0" "--notify" & >/dev/null; }

    #minimize terminal to systray
    #kdocker -d -q -w "$(xdotool search --name "$term_title")" -i "$systray_icon" & >/dev/null
    #inconsistent results, consider other ways to do this

    tee -i "$pipe" < "$int" &
    stdbuf -o0 goldcrest --air-connect > "$int" 

    #https://askubuntu.com/questions/639877/tee-doesnt-get-whole-output-from-the-pipe
    #https://airvpn.org/forums/topic/52600-bash-attempting-to-direct-output-of-goldcrest-to-named-pipe-in-script/

    echo "${0}: Exiting."
    sleep 2

elif [[ "$1" == "--notify" ]] ; then

    [[ ! -e "$pipe" ]] && { mkfifo "$pipe"; }
    [[ ! -e "$int" ]] && { mkfifo "$int"; }

    while read info
    do
        if echo "$info" | grep "Connected to AirVPN server" >/dev/null ; then

            [[ -z "$server" ]] && server=$(echo "$info" | sed 's/[0-9:\-]//g; s/^  //; s/United States of America/US/' )
            [[ -n "$server" ]] && [[ "$server" != "Done." ]] && notify "$server" "#FFB31E" "#000000"
            server="Done."

        elif echo "$info" | grep "EVENT: CONNECTING" >/dev/null ; then

            notify "Connecting..." 

        elif echo "$info" | grep "EVENT: RECONNECTING" >/dev/null ; then

            notify "Connecting..." 

        elif echo "$info" | grep "EVENT: CONNECTED" >/dev/null ; then

            notify "Connected!" "#FFB31E" "#000000"
            [[ -f "$connect_sound" ]] && command -v paplay >/dev/null && paplay "$connect_sound" &

        elif echo "$info" | grep "EVENT: DISCONNECTED" >/dev/null ; then
            notify "Disconnected." 
        fi
    done < "$pipe"

else

    popup_wrapper

fi

 

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Security Check
    Play CAPTCHA Audio
    Refresh Image
Sign in to follow this  

×
×
  • Create New...