#!/bin/bash

usage()
{
    case "$LANG" in
        "ja_JP.UTF-8")
            cat <<EOF
wbar_gen : wbar 設定ファイル生成
    テンプレートから実行可能アイテムのみを抽出し、
    ユーザー設定ファイル($HOME/.wbar)を生成します。
使い方: wbar_gen [オプション] ... [オプション]
  オプション:
    -h , --help     : このヘルプを表示
    -n , --new      : 既存の wbar 設定ファイルの上書を許可
        省略時: 同名の wbar 設定ファイルがある時は実行を中止
    -j , --japanese : システムの日本語テンプレートを使用
        -i /usr/share/wbar/dot.wbar.jp 相当
    -s , --simple   : システムの英語テンプレートを使用
        -i /usr/share/wbar/dot.wbar 相当
    -i file, --ifile file
                    : 入力ファイル(テンプレート)指定
        例: wbar_gen -i ~/dot.wbar
        指定入力ファイルが存在しない時は実行を中止
        省略時: 入力ファイルは /usr/share/wbar/dot.wbar
    -o file , --ofile file
                    : 出力ファイル(wbar 用個人設定ファイル)指定
        例: wbar_gen -o ~/.wbar2
        -n 指定が無く、かつ指定出力ファイルが既存の時は実行を中止
        省略時: 出力ファイルは $HOME/.wbar

テンプレート・ファイルを作る時は、/usr/share/wbar/dot.wbar と
/usr/share/wbar/dot.wbar.jp を参考にしてください。
EOF
            ;;
        *)
            cat <<EOF
wbar_gen : .wbar file generator
    Only an executable item is extracted from the template file, 
    and generate the user configuration file for wbar.
Usage wbar_gen [opttion] ... [opttion]
Options:
    -h or --help     : Display this message.
    -n or --new      : generate new ".wbar" from template.
    -j or --japanese : using Japanese template
        same to "-i /usr/share/wbar/dot.wbar.jp"
    -s or --simple   : using default template
        same to "-i /usr/share/wbar/dot.wbar"
    -i file or --ifile file : set input file
        eg. wbar_gen -i ~/dot.wbar
        default - $HOME/.wbar
    -o file or --ofile file : set output file
        eg. wbar_gen -o ~/.wbar2
        default - /usr/share/wbar/dot.wbar
EOF
            ;;
    esac
}

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

ofile=$HOME/.wbar
ifile=/usr/share/wbar/dot.wbar
args=$#
arg2=""
remv=0
allopt="$@"

while [ $args != 0 ]; do
    arg="$1"
    [ ! x"$arg2" == "x" ] && arg="$arg2"; arg2=""
    case $arg in
        "-h"|"--help")
            usage
            exit 1;;
        "-n"*|"--new")
	    remv=1
	    if [ ! "$arg" == "-n" ]; then
		arg2=`echo "$arg" | sed 's/n//'`
		continue
	    fi
	    ;;
        "-o"|"--ofile")
            shift
            ofile=$1
            args=`expr $args \- 1`
            ;;
        "-i"|"--ifile")
            shift
            ifile=$1
            args=`expr $args \- 1`
            ;;
	"-j"*|"--japanese")
	    ifile=/usr/share/wbar/dot.wbar.jp
	    if [ ! "$arg" == "-j" ] && [ ! "$arg" == "--japanese" ]; then
		arg2=`echo $arg | sed 's/j//'`
		continue
	    fi
	    ;;
	"-s"*|"--simple")
	    ifile=/usr/share/wbar/dot.wbar
	    if [ ! "$arg" == "-s" ] && [ ! "$arg" == "--simple" ]; then
		arg2=`echo $arg | sed 's/s//'`
		continue
	    fi
	    ;;
        *)
            echo $arg : unrecognized option
            usage
            exit 1
            ;;
    esac
    shift
    args=`expr $args \- 1`
done

if [ ! -f "$ifile" ]; then
    echo Inputput file ["$ifile"] not exists.
    echo Does nothing.
    exit 1
fi

[ $remv == 1 ] && rm $HOME/.wbar 2> /dev/null
if [ -f "$ofile" ]; then
    echo "$ofile" already exists. Does nothing.
    echo -e \(Try wbar_gen --new\)
    exit 1
fi

case "$LANG" in
    "ja_JP.UTF-8")
        echo 出力ファイル: $ofile
        echo 入力ファイル: $ifile
        ;;
    *)
        echo output file: $ofile
        echo input file : $ifile
        ;;
esac

cat <<EOF > "$ofile"
# The Bar && Font
i: /usr/share/wbar/iconpack/wbar.osx/osxbarback.png
c:
t: /usr/share/wbar/iconpack/wbar.osx/font/12

EOF

iset=0
cset=0
tset=0
while read line; do
    case `echo "$line" | cut -d ":" -f1` in
        i)
            info="$line"
	    iset=1
	    ;;
        c)
	    cmd="$line"
            cset=1
	    ;;
        t)
            title="$line"
	    tset=1
	    ;;
    esac
    if [ "$iset$cset$tset" == "111" ]; then
	command=`echo "$cmd" | cut -d ":" -f2 | cut -d" " -f2`
	if [ -n "$command" ]; then
            hash "$command" 2> /dev/null
            if [ $? == 0 ]; then
		echo -e $info\\n$cmd\\n$title\\n >> "$ofile"
		info=""
		cmd=""
		title=""
            fi
	fi
	iset=0
	cset=0
	tset=0
    fi
done < "$ifile"

case "$LANG" in
    "ja_JP.UTF-8")
        echo "$ofile" 生成完了。
        ;;
    *)
        echo Generating of "$ofile" was completed.
esac

exit 0
