#!/bin/sh -eu

usage()
{
    echo "usage: jsmerge [-o output] file ..." >&2
    exit 2
}

output=
while getopts "o:" opt; do
    case "$opt" in
    o)
        output="$OPTARG"
        ;;
    *)
        usage
        ;;
    esac
done

shift $(expr $OPTIND - 1)

if [ -n "$output" ]; then
    exec > $output.tmp
fi

first="yes"
while [ $# -gt 0 ]; do
    if [ "$first" != "yes" ]; then
        echo # new line
    fi
    first="no"
    cat $1
    shift
done

if [ -n "$output" ]; then
    mv -f $output.tmp $output
fi
