#!/bin/sh

version=`gnuplot --version | gawk '{print $2}'`
major=${version%.*}
minor=${version#*.}

if [ $major -lt 4 ]; then
	echo "your gnuplot version is too old."
	exit
fi

if [ -z "$1" ]; then
	echo "USAGE: $0 <num of variables> { <lambda> [x1:x2] [y1:y2] }"
	exit
fi

export n=`echo $1-1 | bc`

echo "set terminal postscript eps enhanced color" >| tmp.gnuplot
echo "set output \"out.eps\"" >> tmp.gnuplot
echo "unset key" >> tmp.gnuplot

echo -n "set title \"Trace Plot of solution path by Lasso, Elastic net" >> tmp.gnuplot
if [ -n "$2" ]; then
	echo -n " ({/Symbol a} = $2)"  >> tmp.gnuplot
fi
echo "\""  >> tmp.gnuplot

echo "set xlabel \"{/Symbol S}_j|{/Symbol b}_j|\"" >> tmp.gnuplot
echo "set ylabel \"{/Symbol b}\"" >> tmp.gnuplot

echo -n "plot $3 $4 0 linetype 0 linewidth 2, " >> tmp.gnuplot

fn="beta_path.data"
j=3
for i in `seq -f %03g 0 $n`; do

	echo -n "\"$fn\" using 2:$j with line linewidth 1" >> tmp.gnuplot

	if [ $minor -ge 2 ]; then
		echo -n " linetype 1 linecolor $j" >> tmp.gnuplot
	else
		echo -n " linetype $j" >> tmp.gnuplot
	fi

	if [ $i -lt $n ]; then
		echo -n ", " >> tmp.gnuplot
	fi

	j=$((j+1))
done

gnuplot tmp.gnuplot
rm -f tmp.gnuplot

evince out.eps &
