#!/bin/bash

# style 񼰤ؤθߴͭ/̵ڤ

notice(){
    echo
    if [ $1 = "enable" ] ; then
        echo ͭꤵޤ
    else
        echo ̵ꤵޤ
    fi
    echo ͭˤˤϡfluxbox˥塼
    echo ƵưƤ
    read -p"                 (Enterǽλޤ)"
}

# 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
        read -p "style񼰤ؤθߴͭˤޤ ? y / n : " compat
        case $compat in
	    y|Y)
		compat="1";;
	    n|N)
		compat="0";;
	    *)
		compat="";
	esac
    done
}

# start

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 \(߸ߴͭǤ\)
	;;
    '')
	echo -n \(߸ߴ̵Ǥ\)
        ;;
    *)
        echo rootCommand ϴӤ˻ȤƤޤ
        while true; do
            read -p"rootCommand ߴѤ˽񤭴ޤ -  y | n : " ans
	    case $ans in
		n|N)
                    read -p"ѹ˽λޤ(Enterǽλ)"
		    exit
		    ;;
		y|Y)
                    break
		    ;;
		*)
		    ;;
	    esac
        done
        ;;
esac
comat=""

echo Ԥޤ
echo 'Ϥ'ξ 'y'''ξ 'n' 򲡤Ƥ
set_old_style

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






