Wednesday, October 9, 2013

Hooks to adapt a notebook to different conditions

So, I use Ubuntu 12.04 on my notebook.

Here a list of (shell) hooks I implemented to make the system adapt to "the environment". Only the minimum relevant code

  • Open/close lid -> internal screen on/off
  • grep -q closed /proc/acpi/button/lid/*/state
    xrandr -display $DISPLAY --output $internal --off

  • When (un)docked -> external monitor on/off.  un/docking detected by AC online. No other way found (with Dell latitude e*)


    if [ $(cat /sys/class/power_supply/AC/online) = "1" ]; then
    DISPLAY=:0 xrandr --output DP-1 --auto --rotate left 
  • Turn audio off/on  when on office network/at home:
    /etc/dhcp/dhclient-enter-hooks.d/  does not work with NM (see )
    So /etc/network/if-up.d/mute-in-office

     if [ ${DHCP4_DOMAIN_NAME-} = '' ];
       amixer set Master mute
     elif [ ${CONNECTION_ID-} = "home-ssid" ];
            amixer set Master unmute
            amixer set Front unmute
            amixer set Headphone unmute

  • prevent Skype from animating (in gnome-panel) when offline
    skype without any dbus  access?

    sleep 10s
    killall -STOP skype

    killall -CONT skype
  • keep cpu at minimum
    case "$1" in
            resume | thaw)
    sudo cpufreq-set --governor powersave
    foreach dir (/sys/devices/system/cpu/cpu*/cpufreq/) {
       echo powersave | sudo tee $dir/scaling_governor
       echo 1199000 | sudo tee $dir/scaling_min_freq
    #sudo hdparm -Y /dev/sdb

Friday, May 17, 2013

Shell skeletons

Shell skeletons --- a tool to start using a sequence of commands

When I started to build debian packages from Git, I adopted git-buildpackage.
The steps I used were (at least) 7:

  • git-dch  --release "--debian-tag=michal/%(version)s"
  • git add debian/changelog; 
  • git commit -m release
  • git-buildpackage --git-tag "--git-debian-tag=michal/%(version)s" 
  • debi --debs-dir ../build-area/
  • git-buildpackage -S
  • debrelease -S  --debs-dir=../build-area --dput

I had this recipe in a file/editor, and pasted line by line into shell, possibly adapting the text, and then invoked.
In case of error I could reinvoke the command, with some modification.

Then I learned that with Zsh, I can programmatically use that feature which implements C-q (push-line):  there is a stack which I can pre-populate, and then
ZLE will  prefill the command line by popping the strings from the stack.

So I came up with this function:

zload-file () {
        setopt nomonitor
        coproc tac $file &|
        while read line
                builtin print -z $line
        done <&p
        setopt monitor

Sure, I have to put the lines in reverser order -- the last one to be pushed as first, so that it ends at the bottom.

PS: After a while I learned/realized all the arguments of those commands, and made scripts (release & snapshot) which hide all the details, and expose different set of options-- those which I care about.