#!/bin/bash

# style 旧書式への互換性を有効/無効切り替

notice(){
    echo
    if [ $1 = "enable" ] ; then
        echo 有効に設定されました。 | nkf $NKFOPT
    else
        echo 無効に設定されました。 | nkf $NKFOPT
    fi
    echo 設定を完全に有効にするには、fluxboxをメニューから | nkf $NKFOPT
    echo 再起動してください。 | nkf $NKFOPT
    echo -n "                 (Enterで終了します) " | nkf $NKFOPT
    read tmp
}

# style旧書式への互換性を有効に
enable_back_compati(){
    cat ~/.fluxbox/init | sed -e '/root[Cc]ommand/d' > ~/.fluxbox/init.r_cmd.tmp
    echo -e "$rootCommand\t$enable_string">> ~/.fluxbox/init.r_cmd.tmp
    cp ~/.fluxbox/init.r_cmd.tmp ~/.fluxbox/init
    rm ~/.fluxbox/init.r_cmd.tmp
}

# style旧書式への互換性を無効に
disable_back_compati(){
    cat ~/.fluxbox/init | sed -e '/root[Cc]ommand/d' > ~/.fluxbox/init.r_cmd.tmp
    echo -e "$rootCommand">> ~/.fluxbox/init.r_cmd.tmp
    cp ~/.fluxbox/init.r_cmd.tmp ~/.fluxbox/init
    rm ~/.fluxbox/init.r_cmd.tmp
}

# style旧書式への互換性を有効/無効の問い合わせ
set_old_style(){
    comat=""
    while [ -z $compat ]; do
        echo -n "style旧書式への互換性を有効にしますか ? y / n : " | nkf $NKFOPT
        read compat
        case $compat in
            y|Y)
                compat="1";;
            n|N)
                compat="0";;
            *)
                compat="";
        esac
    done
}

# start

case $LANG in
    ja_JP.eucJP)
        NKFOPT="-e"
        ;;
    ja_JP.UTF-8)
        NKFOPT="-w"
        ;;
    *)
esac
enable_string="if [ -x ~/.fluxbox/change-rootcmd ]; then ~/.fluxbox/change-rootcmd; else /usr/share/fluxbox/change-rootcmd; fi"

rootCommand=`cat "$HOME"/.fluxbox/init | grep -i rootCommand | cut -d : -f 1`
if [ -z "$rootCommand" ]; then
    rootCommand="session.screen0.rootCommand"
fi
rootCommand=`echo $rootCommand:`

rootCommand_body=`cat "$HOME"/.fluxbox/init | grep -i rootCommand | cut -d : -f 2`
rootCommand_body=`echo $rootCommand_body`

case "$rootCommand_body" in
    "$enable_string")
        echo -n "(現在互換性は有効です) " | nkf $NKFOPT
        ;;
    '')
        echo -n "(現在互換性は無効です) " | nkf $NKFOPT
        ;;
    *)
        echo rootCommand は既に別用途に使われています。 | nkf $NKFOPT
        while true; do
            echo -n "rootCommand を後方互換用に書き換えますか -  y | n : " | nkf $NKFOPT
            read ans
            case $ans in
                n|N)
                    echo -n "変更せずに終了します。(Enterで終了) " | nkf $NKFOPT
                    read ans
                    exit
                    ;;
                y|Y)
                    break
                    ;;
                *)
                    ;;
            esac
        done
        ;;
esac
comat=""

echo 再設定を行います。 | nkf $NKFOPT
echo 'はい'の場合は 'y'、'いいえ'の場合は 'n' を押してください。 | nkf $NKFOPT
set_old_style

case $compat in
    1)
        enable_back_compati
        notice enable
        ;;
    0)
        disable_back_compati
        notice disable
        exit
        ;;
esac



