#!/bin/bash

# MaKe ROOTCoMmanDS vicarious script (for fluxbox styles)
# Copyright(C) 2006-2008 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 | nkf $NKFOPT
### 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

case $LANG in
    ja_JP.eucJP)
        NKFOPT="-e"
        ;;
    ja_JP.UTF-8)
        NKFOPT="-w"
        ;;
    *)
esac

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 echo "$style" | grep -e ~$ ; then
            # echo backup file
            continue
        fi
        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=""
        else
            echo "    \""`basename "$style"`\"\) >> "$outfile"
            echo "        exit 0 ;;"  >> "$outfile"
        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

