#!/usr/bin/env nix-shell
#! nix-shell -p openring -i bash

function help() {
cat << HELP
Simply creates an output from an openring template.

Usage: [options] [input_file] -- [openring_args]

Options:
  -h, --help    Help.
  -l, --limit   The number of posts to be shown.
  -d, --data    The file path of a JSON file containing an
                array of URLs (as strings).
  -o, --output  The output file path.
                If there's no output value given, the resulting
                output name will be '\$INPUT.out'.
  -i, --input   The input file path.

You can also pass arguments to openring as if executing the
program itself by prepending them with double dash.
Make sure, they are the last one.
HELP
}

# The default values.
LIMIT=5

INPUT_TEMPLATE="./assets/templates/openring-input.html"
input_file=$(basename "$INPUT_TEMPLATE")
input_file_ext="${input_file##*.}"
input_file="${input_file%.*}"

OUTPUT="$input_file.out.$input_file_ext"
DATA="data/blogs.json"

while [[ "$#" -gt 0 ]]; do
    case "$1" in
        -h|--help)
            help
            exit 0
            ;;
        -l|--limit)
            LIMIT="$2"
            shift 2
            ;;
        -d|--data)
            DATA="$2"
            shift 2
            ;;
        -o|--output)
            OUTPUT="$2"
            shift 2
            ;;
        -i|--input)
            INPUT_TEMPLATE="$2"
            shift 2
            ;;
        --)
            shift
            OPENRING_ARGS="$@"
            shift $#
            ;;
        *)
            INPUT_TEMPLATE="$1"
            shift
            ;;
    esac
done

OPENRING="openring $OPENRING_ARGS";

for feed in $(jq ".[]" "$DATA" | shuf --head-count $LIMIT); do
    OPENRING="$OPENRING -s $feed";
done

OPENRING="$OPENRING -n $LIMIT < $INPUT_TEMPLATE > $OUTPUT"

echo "$OPENRING"
eval $OPENRING
