#!/usr/bin/env bash

set -e

SCRIPTDIR=$(
  cd "$(dirname "$(realpath "$0")")" || exit 254
  pwd -P
)

FILE=$1
SOURCE=$2
DEST=$3
FILTER=$4

if [[ -z $FILE ]]; then
  echo "Usage: $0 sourcefolder listjson destfolde [filterId]"
  echo "No file"
  exit 255
fi

if [[ -z $DEST ]]; then
  DEST="./out";
fi

DEST=$(realpath $DEST)

if [[ -z $SOURCE || -z $DEST ]]; then
  echo "Usage: $0 sourcedir destdir"
  exit 1
fi

if [[ ! -d $DEST ]]; then
  echo "! $DEST does not exist"
  exit 255
fi

SOURCE=$(realpath "$SOURCE")

cd "${SOURCE}"/lists || exit 255

if [[ -f $1 ]]; then
	file=$1
  node "${SCRIPTDIR}/src/publisher-list.js" "${file}" "${SOURCE}" "${DEST}" "${FILTER}"
  # > "${DEST}/${file%.*}.html"
else
  echo "$1 does not exist"
fi
