#!/bin/sh
# configure wmii


# Patch S.Tibus
tagsmenu() {
	tags=`wmiir read /tags | sort | wmiimenu`
	test -n "$tags" && xwrite $@ "$tags"
}

jointags() {
	join1=`wmiir read /tags | sort | wmiimenu`
	test -n "$join1" && join2=`wmiir read /tags | sort | wmiimenu`
	test -n "$join2" && xwrite $@ "$join1"+"$join2"
}

xwrite() {
	file="$1"; shift
	echo -n "$@" | wmiir write "$file"
}

proglist() {
	ls -lL `echo "$@" | tr : ' '` 2>/dev/null |
        awk 'NF>2 && $1 ~ /^[^d].*x/ {print $NF}' | sort -u
}
MODKEY=Alt
WMII_FONT='-*-smooth-*-*-*-*-13-*-*-*-*-*-*-*' export WMII_FONT
#WMII_FONT='-*-unifont-*-*-*-*-16-*-*-*-*-*-*-*' export WMII_FONT

# Theme Orange/gris violet/blanc
#WMII_NORMCOLORS='#DEDEDE #5D5D5D #222222' export WMII_NORMCOLORS
#WMII_SELCOLORS='#222222 #FF7F00 #FF7F00' export WMII_SELCOLORS
#WMII_NEWCOLORS='#222222 #eeeeee #666666' export WMII_NEWCOLORS
#WMII_SENDCOLORS='#FFFFEA #414171 #666699' export WMII_SENDCOLORS

# Theme
# Bleu/Violet
WMII_NORMCOLORS='#797979 #1A1A1A #414171' export WMII_NORMCOLORS
WMII_SELCOLORS='#FFFFEA #1A1A1A #4186BE' export WMII_SELCOLORS
WMII_NEWCOLORS='#222222 #eeeeee #666666' export WMII_NEWCOLORS
WMII_SENDCOLORS='#FFFFEA #414171 #666699' export WMII_SENDCOLORS

# give wmiiwm a chance to start
while :
do
        echo Start wmiirc | wmiir write /event >/dev/null 2>&1 && break
	sleep 1
done

# WM CONFIGURATION
xwrite /def/border 2
xwrite /def/font $WMII_FONT
xwrite /def/selcolors $WMII_SELCOLORS
xwrite /def/normcolors $WMII_NORMCOLORS

# TAGGING RULES
cat ~/.wmii-3/rules | wmiir write /def/rules

# MISC
xsetroot -solid '#0b1014'
qiv -x ~/images/m42_hst_f.jpg&
status &
checkmail &
statnet &
proglist $OLD_PATH >/tmp/ns.$USER.$DISPLAY/progs &

# SHORTCUTS
wmiir write /def/keys <<EOF
$MODKEY-Control-c
$MODKEY-Control-w,y
$MODKEY-Control-q,y
$MODKEY-Control-p
$MODKEY-Control-a
$MODKEY-oe
$MODKEY-x
$MODKEY-Shift-Up
$MODKEY-Shift-Down
$MODKEY-Shift-Right
$MODKEY-Shift-Left
$MODKEY-a,w
$MODKEY-a,a
$MODKEY-a,f
$MODKEY-a,Delete
$MODKEY-g
$MODKEY-t
$MODKEY-f
$MODKEY-Shift-f
$MODKEY-j
$MODKEY-Left
$MODKEY-Right
$MODKEY-Down
$MODKEY-Up
$MODKEY-Shift-Left
$MODKEY-Shift-Right
$MODKEY-Shift-Up
$MODKEY-Shift-Down
$MODKEY-u
$MODKEY-n
$MODKEY-m
$MODKEY-Control-m
$MODKEY-s
$MODKEY-e
$MODKEY-9,t
$MODKEY-9,a
$MODKEY-9,s
EOF

# EVENT LOOP
wmiir read /event 2>/dev/null |
while read event
do
	set -- $event
	type="$1"; shift
	case "$type" in
	Start)
		if test wmiirc = "$1"
		then
			exit
		fi
		;;

	LabelClick)
		xwrite /ctl view $1;;
	Key)
		case "$1" in
		$MODKEY-Control-c)
			xwrite /view/sel/sel/ctl kill;;
		$MODKEY-Control-w,y)
#			pkill status&
#			pkill statnet&
#			pkill checkmail&
			wmiirc &
			exit 0;;
		$MODKEY-Control-q,y)
			xwrite /ctl quit;;
		$MODKEY-Control-p)
			extern `wmiimenu </tmp/ns.$USER.$DISPLAY/progs` &;;
		$MODKEY-Control-a)
			`proglist /usr/local/etc/wmii-3:$HOME/.wmii-3 | wmiimenu` &;;
		$MODKEY-t)
			extern uxterm&;;
                $MODKEY-u)
                        extern uxterm -e vi ~/.wmii-3/wmiirc&;;
		$MODKEY-a,w)
			echo `wmiir read /tags | wmiimenu` > ~/.wmii-3/tmp
			ws=`cat ~/.wmii-3/tmp`
			test -n "$ws" && echo '/'`wmiir read /view/sel/sel/class`'/ ->' $ws >> ~/.wmii-3/rules && cat ~/.wmii-3/rules | wmiir write /def/rules;;
                $MODKEY-a,f)
                        echo '/'`wmiir read /view/sel/sel/class`'/ -> ~' >> ~/.wmii-3/rules
                        cat ~/.wmii-3/rules | wmiir write /def/rules;;
		$MODKEY-a,a)
			echo `wmiir read /tags | wmiimenu` > ~/.wmii-3/tmp
                        ws=`cat ~/.wmii-3/tmp`
                        test -n "$ws" && echo '/'`wmiir read /view/sel/sel/class`'/ ->' '~+'$ws >> ~/.wmii-3/rules && cat ~/.wmii-3/rules | wmiir write /def/rules;;
                $MODKEY-a,Delete)
			class=`wmiir read /view/sel/sel/class`
			sed /$class/d ~/.wmii-3/rules > ~/.wmii-3/tmp
			cat ~/.wmii-3/tmp > ~/.wmii-3/rules
			cat ~/.wmii-3/rules | wmiir write /def/rules;;
		$MODKEY-m)
			xwrite /view/sel/mode max;;
		$MODKEY-Control-m)
			extern fetchmail -q&
			extern fetchmail&;;
		$MODKEY-s)
			xwrite /view/sel/mode stack;;
		$MODKEY-e)
			xwrite /view/sel/mode equal;;
                $MODKEY-9,t)
                        extern 9termz&;;
                $MODKEY-9,s)
                        extern samz&;;
                $MODKEY-9,a)
                        extern acmez&;;
		$MODKEY-n)
			xwrite /view/sel/sel/ctl sendto new;;
		$MODKEY-f)
			xwrite /view/ctl select toggle;;
		$MODKEY-Shift-f)
			xwrite /view/sel/sel/ctl sendto toggle;;
		$MODKEY-Left)
			xwrite /view/ctl select prev;;
		$MODKEY-Right)
			xwrite /view/ctl select next;;
		$MODKEY-Down)
			xwrite /view/sel/ctl select next;;
		$MODKEY-Up)
			xwrite /view/sel/ctl select prev;;
                $MODKEY-Shift-Left)
                        xwrite /view/sel/sel/ctl sendto prev;;
                $MODKEY-Shift-Right)
                        xwrite /view/sel/sel/ctl sendto next;;
                $MODKEY-Shift-Down)
                        xwrite /view/sel/sel/ctl restack down;;
                $MODKEY-Shift-Up)
                        xwrite /view/sel/sel/ctl restack up;;
		$MODKEY-j)
	        	jointags /ctl select &;;
		$MODKEY-g)
                        tagsmenu /view/sel/sel/tags &;;
		$MODKEY-oe)
			tagsmenu /ctl view &;;
		esac;;
	esac
done &

