#!/bin/bash
registry=https://packagecloud.io/orchardit/npm/npm/

echo -e "\n"

if [ "$1" == "" ]; then
    echo "ERROR: Please specify the name of your new project."
    exit -1
fi

template=$2
if [ "$2" == "" ]; then
    template='jsx'
fi
echo "Using template '$template'"

mkdir $1

if cd $1; then
    echo "Creating frontend: '$1'"
else
    echo "ERROR: Could not set working directory to '$1'"
    exit -1
fi

if echo '{ "name": "'$1'", "version": "1.0.0", "private": true }' > package.json; then
    echo "Installing frontend-cli..."
    if yarn add @orchard/frontend-cli --registry $registry --dev --silent; then
        echo "Initializing project..."
        yarn frontend init -o -t $template
        echo "Installing project dependencies..."
        yarn install --registry $registry
        yarn add @orchard/frontend-cli --registry $registry --dev
    fi
fi
