gnuplotのテンプレート生成をshell scriptで

gnuplot用のテンプレートを生成して標準出力にだしてくれるだけのもの。getoptsコマンドの練習がてら作ってみた。シェルで引数をパースするときは、外部コマンドとして存在する getopt と、ビルドイン関数として存在する getopts が使えるが、getoptsのほうがクオートを扱えたりして便利、らしい。

function str-or(){
    ret=$1
    while shift 2>/dev/null; do
	if ! [ -z "$ret" ]; then
	    echo $ret
	    return 0
	else
	    ret=$1
	fi
    done
    return 0
}

# refs:
#  http://www.geocities.jp/geo_sunisland/parameter.html
function tmp-gnuplot(){
    local opt_output="gnuplot-output"
    local opt_input
    local opt_type="x11"
    local opt_show_help

    local tmp_terminal=""
    local tmp_pause=""
    local tmp_input=""
    local tmp_output=""
    local tmp_ext=""
    local tmp_plot='plot [][0:] sin(x) title "sine curve" with lp ls 1'
    local tmp_plot_nodata='plot [][0:] sin(x) title "sine curve" with lp ls 1'
    name="foo"

    while getopts "hi:o:t:" flag
    do
	case ${flag} in
	    \?)
		opt_show_help=1
		;;
	    "h")
		opt_show_help=0
		;;
	    "i")
		opt_input="${OPTARG}"
		;;
	    "o")
		opt_output="${OPTARG}"
		;;
	    "t")
		opt_type="${OPTARG}"
		;;
	    *)
		echo "hoge" $flag
		;;
	esac
    done

    case ${opt_type} in
	x|x11|eps|latex|table)
	    echo "output type: ${opt_type}" > /dev/stderr
	    ;;
	*)
	    opt_show_help=1
	    echo "invalid output type: '${opt_type}'"
	    ;;
    esac

    if ! [ -z "${opt_show_help}" ]; then
	cat <<EOF
usage: ${0} [options]

options:
	-h		show this help
	-i INPUT_FILE	set input(data) filename to INPUT_FILE
	-o OUT_FILE	set output filename to OUT_FILE
	-t OUT_TYPE	set output type. types: x x11 eps latex table
EOF
	return opt_show_help
    fi

    case ${opt_type} in
	x)
	    tmp_pause="pause 100"
	    tmp_terminal="x11"
	    opt_output=""
	    ;;
	x11)
	    tmp_pause="pause 100"
	    tmp_terminal="x11"
	    opt_output=""
	    ;;
	eps)
	    tmp_terminal="postscript eps enhanced color"
	    tmp_ext="eps"
	    ;;
	latex)
	    tmp_terminal="latex"
	    tmp_ext="tex"
	    ;;
	table)
	    tmp_terminal="table"
	    tmp_ext="dat"
	    ;;
    esac

    tmp_output=$opt_output
    if ! [ -z "${tmp_output}" ]; then
	if ! (echo $tmp_output|egrep "\\.${tmp_ext}\$">/dev/null);then
	    tmp_output="${tmp_output}.${tmp_ext}"
	fi
    fi
    
    if ! [ -z "${tmp_output}" ]; then
	tmp_output="set output ${tmp_output}"
    fi

    tmp_input=$(str-or ${opt_input} "foobar")
    tmp_plot="plot [][0:] \"${tmp_input}\" using 1:2 ti \"${tmp_input}1\" wi lp ls 1, \"${tmp_input}\" using 1:3 ti \"${tmp_input}2\" wi lp ls 2"

    if [ -z "${opt_input}" ]; then
	tmp_plot="# ${tmp_plot}"
    else
	tmp_plot_nodata="# ${tmp_plot_nodata}"
    fi

    cat <<EOF
# generated at $(LANG=C date)
set terminal ${tmp_terminal}
${tmp_output}
set size 0.7,0.7
set key outside center top horizontal reverse Left
set grid ytics lw 0.1 lt -1
set key outside center top horizontal reverse Left samplen 2
set style line 1 lt 1 lc rgbcolor "#354E66" lw 5 pt 5 ps 2
set style line 2 lt 1 lc rgbcolor "#8D484A" lw 5 pt 4 ps 2
set xlabel "x axis label"
set ylabel "y axis label"

${tmp_plot}
${tmp_plot_nodata}
${tmp_pause}
EOF
}