#!/bin/sh

usage() {
    echo "usage: rc start|restart|stop" >&2
    echo "       rc reload [file]" >&2
    exit 1
}

getfile() {
    error=false

    if expr "$1" : '.*/' >/dev/null; then
        # $1 contains a slash
        if [ -r "$1" ]; then
            echo "$1"
        else
            error=true
        fi
    else
        # $1 contains no slash
        if [ -r "$HOME/.wmii/$1" ]; then
            echo "$HOME/.wmii/$1"
        elif [ -r "$WMII_CONFDIR/$1" ]; then
            echo "$WMII_CONFDIR/$1"
        else
            error=true
        fi
    fi

    if $error; then
        echo "`basename $0`: error: couldn't read $1" >&2
        exit 2
    fi
}

ixpmount() {
    $WRITE /ctl "bind /wm $WMIIWM_SOCKET"
    $WRITE /ctl "bind /bar $WMIBAR_SOCKET"
    $WRITE /ctl "bind /menu $WMIMENU_SOCKET"
    $WRITE /ctl "bind /keys $WMIKEYS_SOCKET"
}

util_setup() {
    for file in wm bar keys menu; do  # order matters
        . "`getfile \"$file\"`"
    done
}

check_socket() {
    if wmir read / >/dev/null 2>&1; then
        :
    else
        echo 'rc: error: $WMII_IDENT seems to be invalid' >&2
        exit 3
    fi
}

start() {
    # start wmii components:
    echo -n "Starting wmii utilities ..."
    wmifs -s "$WMIFS_SOCKET" &
    wmikeys -s "$WMIKEYS_SOCKET" "center,center,100,$BAR_HEIGHT" &
    wmibar -s "$WMIBAR_SOCKET" "0,south,east,$BAR_HEIGHT" &
    wmimenu -s "$WMIMENU_SOCKET" "0,south,east,`expr $BAR_HEIGHT + $BAR_HEIGHT`" &
    $WALLPAPER
    sleep 1
    ixpmount
    util_setup
    "`getfile status.sh`" &  # run status loop
    if [ -f "$HOME/.wmii/.firstrun" ]; then
        # this seems to be the first run (the file is created by wmii)
        rm "$HOME/.wmii/.firstrun"
        xmessage -file SHAREPREFIX/wmii/welcome.txt &
    fi
    echo "done."
}

reload() {
    check_socket
    echo -n "Reloading wmii configuration ..."
    case $1 in
        "")
            util_setup
            ;;
        *bar|*menu|*keys|*wm)
            . "`getfile \"$1\"`"
            ;;
        *)
            echo
            echo "error: $0: unknown file"
            ;;
    esac
    # update page label:
    $WRITE /bar/default/10_pager/label/text \
        "`$READ /wm/page/sel/name`" 2>/dev/null
    echo "done."
}

stopp() {
    echo -n "Stopping wmii utilities ..."
    if [ -f "$STATUS_PIDFILE" ]; then
        kill "`cat \"$STATUS_PIDFILE\"`" >/dev/null 2>&1
    fi
    $WRITE /bar/ctl quit
    $WRITE /menu/ctl quit
    $WRITE /keys/ctl quit
    $WRITE /ctl quit
    echo "done."
}

#---------------------------------------------------------------------

. "`getfile rc.conf`"

if [ $# -eq 1 ] || { [ $# -eq 2 ] && [ "$1" = reload ]; }; then
    case $1 in
        start)
            start
            ;;
        reload)
            reload "$2"
            ;;
        restart)
            stopp
            sleep 3  # wait for status.sh to exit
            start
            ;;
        stop)
            # the quit action causes wmii to execute "rc stop"
            stopp
            ;;
        *)
            usage
            ;;
    esac
else
    usage
fi
