#!/bin/bash

# MaKe ROOTCoMmanDS vicarious script (for fluxbox styles)
# Copyright(C) 2006 m.kato <mkato@par.odn.ne.jp>

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

#-----------------------------------
help(){
    cat << EOF
### rootCommand vicarious execution script generator
Usage:  `basename $0` [opt] [path | oscript]
    style եǥ쥯ȥ꤫顢񼰤طꥳޥ(rootCommand)
    ޤ style 򸡺߻ style  طԤʤ
    ץȤϤޤ
     rootCommand طʳΥ륳ޥɤǤ⤫ޤޤ
ץ
    -p path [path2 ...] : set search path
        path 򸡺оݤȤޤ
        (path ʣǽ-p ϾάǽǤ)
    -o oscript      :
        oscript ȤץȤޤ
    -f | --fluxbox  : set search path (for fluxbox std dir)
        ~/.fluxbox/styles/ 򸡺оݤȤޤ
    -b | --blackbox : set search path (for blackbox dir)
        ~/.blackbox/styles/ 򸡺оݤȤޤ
    -h | --help     : Υإפɽޤ
# ץǻꤷʤ硢ѥ ~/.fluxbox/styles/
# ץȤ ~/.fluxbox/change-rootcmd Ǥ

:
    `basename $0` -o wpscript -f -b ~/stylepath
        ~/.fluxbox/styles/~/.blackbox/styles/~/stylepath  style
        ե򸡺wpscript ȤץȤޤ
EOF
}

#-----------------------------------
option_check(){
    searchdir=""

    while [ "$1" != "" ] ; do
        case $1 in
            -p)
                shift
                ;;
            -o)
                shift
                outfile="$1"
                shift
                ;;
            -f|--fluxbox)
                searchdir="$searchdir""$HOME/.fluxbox/styles "
                shift
                ;;
            -b|--blackbox)
                searchdir="$searchdir""$HOME/.blackbox/styles ""/usr/share/blackbox/styles/ "
                shift
                ;;
            -*)
                help
                exit 1
                ;;
            *)
                searchdir="$searchdir""$1"" "
                shift
                ;;
        esac
    done
    if [ -z "$outfile" ] ; then
        outfile="$HOME/.fluxbox/change-rootcmd"
    fi
    if [ -z "$searchdir" ] ; then
        searchdir="$HOME/.fluxbox/styles"
    fi
}

#------------------ start
option_check "$@"

# generate script
#     for 'get style name' 
cat << EOF > "$outfile"
#!/bin/bash

style=\`cat ~/.fluxbox/init | grep -i stylefile | cut -d : -f 2\`
style=\`basename \$style\`

EOF

#     for 'selection of command by style'
echo "case \$style in" >> "$outfile"
for styledir in $searchdir ; do
    echo "    ### {$styledir} ###" >> "$outfile"
    for style in "$styledir"/* ; do
        cfgfile="$style"
        if [ -d "$style" ] ; then
            cfgfile="$style"/theme.cfg
        fi
        wpcmd=`cat "$cfgfile" | grep -i -e ^rootcommand`
        if [ -n "$wpcmd" ] ; then
            echo "    \""`basename "$style"`\"\) >> "$outfile"
            wpcmd=`echo "$wpcmd" | sed 's/[Rr]oot[Cc]ommand://'` >> "$outfile"
            echo "        "$wpcmd" ;;" >> "$outfile"
            wpcmd=""
        fi
    done
done

#     for 'connect to system script'
cat << EOF >> "$outfile"
    *)
        /usr/share/fluxbox/change-rootcmd ;;
esac

exit 0
EOF

# set permission
chmod a+x "$outfile"

exit 0
