#!/bin/bash
#
# Takes n input gcode files (from the gcodetools_to_FSE.sh utility)
# and concatenates them.
#
# TODO: Allow input on pipe, file ... but how?
#
# Matti Kariluoma Apr 14 2011 <matti.kariluoma@gmail.com>
DATA=""

if readlink /proc/$$/fd/0 | grep -q "^pipe:"; then
  # echo "Pipe input:"
  echo "Piping not yet implemented."
  exit 1
elif file $( readlink /proc/$$/fd/0 ) | grep -q "character special"; then
  # echo "Standard input:"
  if  [ $# -lt 1 ]; then
    echo "usage: $0 1st_gcode_file [2nd_gcode_file 3rd_gcode_file ...]"
    exit 1
  fi
else
  # echo "File input:"
  echo "File input not yet implemented."
  exit 1
fi

HEADER='%
'
HEADER+=$(cat $1 | sed -n '/Head/,/G21/ p')

FOOTER=$(cat $1 | sed -n '/Foot/,/%/ p')

for i in $@; do
  if ! [ -e $i ]; then
    echo "File \"$1\" does not exist!"
    exit 1
  fi
  DATA+=$(cat $i | sed -e '/Head/,/G21/ d' -e '/Foot/,/%/ d' -e '/%/ d')
done
 

echo "$HEADER"
echo "$DATA"
echo "$FOOTER"


