| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 | #!/bin/bash
. $(ffoom path)
ffoo import pretty
ffoo import recon
#
# self help
#
available_commands() {
    echo afk
    echo at
    echo back
    echo bbl
    echo bbldone
    echo deaf
    echo deafnomore
    echo gone
    echo listening
    echo moving
    echo ooo
    echo wfh
    echo writing
}
usage() {
    cmd_hint=$(
        available_commands \
            | head -c -1 \
            | tr '\n' '|'
    )
    usage_is "$cmd_hint"
}
#
# querying
#
familiars_at() {
    #
    # daemons specific to $1 location plus the rest
    #
    test -n "$1" || usagef "location"
    saturnin conf -p iam.running.at.$1
    saturnin conf -p iam.running.at.ANYPLACE
}
where_i_am() {
    #
    # what is my physical location?
    #
    if=$(saturnin conf -p iam.using.if)
    gwmac=$(arp | grep "^gateway\\>.*\\<$if\$" | tr ' ' '\n' | grep :)
    saturnin conf -p iam.seeing.gw.$gwmac || echo OUT
}
start_familiar() {
    #
    # start familiar as indicated by conf file (unless running)
    #
    local f=$1
    pids_matching $f && return 0
    local like=$(saturnin conf -p iam.starting.like.this.$f)
    debug -v f like
    if test -n  "$like";
        then
            $like &
        else $f &
    fi
}
set_status() {
    #
    # set public status $1 by command in conf file
    #
    local what=$1
    saturnin conf -p iam.saying.like.this.$what | bash
}
sound_is_muted() {
    #
    # At least one playback channel is muted
    #
    local master_status="$(amixer get Master)"
    echo "$master_status" \
      | grep "Playback channels" \
      | sed -e 's/.*: //; s/ - /\n/' \
      | while read chname
        do
            grep -e "^ *$chname" <<<"$master_status"
        done \
      | grep -qse '\[off\]$'
}
#
# subcommand handlers
#
i_am_afk() {
    #
    # away from keyboard; blocks until i'm back again
    #
    mocp --pause
    set_status "afk"
    i_am_bbldone    # make sure to set lang to default before locking
    slock
    set_status "atk"
}
i_am_at() {
    #
    # Just say where I am
    #
    where_i_am
}
i_am_back() {
    #
    # returning to work (should be called by other subcommands)
    #
    local f
    for f in $(familiars_at $(where_i_am));
    do
        start_familiar $f
    done
    set_status "back"
    klist || urxvt -e kinit
}
i_am_bbl() {
    #
    # changing language
    #
    local cur=$(setxkbmap -v | grep '^symbols: ' | cut -d+ -f2)
    local all="$(saturnin conf    -p iam.typing.lang)"
    local def="$(saturnin conf -1 -p iam.typing.lang)"
    local nxt=$(
        echo -e "$all\n$all" \
          | grep -m 1 -A 1 $cur \
          | tail -1
    )
    test -z "$nxt" && nxt=$def
    test -z "$nxt" && nxt=us
    debug -v all def cur nxt
    setxkbmap "$nxt"
}
i_am_bbldone() {
    #
    # changing language back to normal
    #
    setxkbmap "$(saturnin conf -1 -p iam.typing.lang)"
}
i_am_deaf() {
    amixer -q sset Master 5%+
}
i_am_deafnomore() {
    amixer -q sset Master 5%-
}
i_am_gone() {
    #
    # gone fishin'
    #
    kdestroy
    ssh-add -D
    set_status "gone"
    i_am_afk
    i_am_back
}
i_am_listening() {
    if sound_is_muted;
    then
        amixer -q sset Master unmute;
    else
        amixer -q sset Master mute;
    fi
}
i_am_moving() {
    #
    # to other train or dreamland, but localhost must sleep now
    #
    die "not implemented"   # hint: use zleep
}
i_am_ooo() {
    #
    # too dangerous to implement
    #
    die "not implemented"
}
i_am_wfh() {
    #
    # too dangerous to implement
    #
    die "not implemented"
}
i_am_writing() {
    #
    # writing in that language
    #
    set -x
    DISPLAY=:0 setxkbmap $1
}
cmd=$1
test -n "$cmd" || usage
available_commands | grep -qse ^$1 || usage
shift
i_am_$cmd $1
 |