#!/bin/bash
#
# Plot multiple files (by wildcard) in one diagram.
# Usage (in this directory as example files):
#
#   ./plot-multiple.sh  a*
#
# Generates gnuplot script and executes it.
#
# -- PD, Sven Koeppel, 20.05.13

# $@ are the shellscripts 
FILE_PATTERN=$@

# some magic to convert a file listing like
# a b c to "a","b","c"
# 1. globbing the files and mapping them with double quotes
files=(`ls ${FILE_PATTERN} | xargs -n1 -Ix echo '"x"'`)
IFS=","
# joining the array with commas for gnuplot
FILES_AS_LIST="${files[*]}"

# now write a gnuplot script
gnuplot_script=`mktemp`
cat > $gnuplot_script <<EOF
# Generiertes Gnuplot-Script
set terminal pdf
set output "graph.pdf"
plot $FILES_AS_LIST
EOF

echo "Generiertes Gnuplot-Script:"
echo
cat $gnuplot_script
echo

gnuplot $gnuplot_script
