#!/usr/bin/env bash

USAGE="$0 sourcefilelist destfile"
SOURCE=$1
DESTINATION=$2

if [[ -z $SOURCE ]]; then
  echo $USAGE
  exit 1
fi

if [[ -z $DESTINATION ]]; then
  echo $USAGE
  exit 1
fi

echo "" > $DESTINATION

if [[ ! -f ${SOURCE} ]]; then
  echo "$0: Nothing to do, source not found"
  exit 0
fi

files=$(cat $SOURCE)
echo $files

for file in ${files}; do
  if [[ -n $file ]]; then
    {
      echo "/** start $file */";
      if [[ $file == http* ]]; then
        curl -L $file 2>/dev/null;
      elif [[ -f $file ]]; then
        cat $file;
      fi;
      echo "/** end $file */";
    } >> $DESTINATION
  fi
done
