#!/usr/bin/env bash

RELSCRIPTPATH="$(realpath "$0")"
SCRIPTPATH="$(
  cd "$(dirname "${RELSCRIPTPATH}")" >/dev/null 2>&1
  pwd -P
)"

USAGE="$0 applicationName"

if [[ -z $1 ]]; then
  echo "${USAGE}"
  exit 1
fi

LIBNAME=$1

if [[ -d "${LIBNAME}" ]]; then
  echo "! Directory already exists. Stopping!"
  exit 2
fi
if [[ -f "${LIBNAME}" ]]; then
  echo "! A file already exists with that name, cannot create a Directory. Stopping!"
  exit 3
fi

echo "- Creating Library ${LIBNAME}"

mkdir "${LIBNAME}"
mkdir "${LIBNAME}/src" "${LIBNAME}/css" "${LIBNAME}/assets"

if [[ -d "${CURRENTTEMPLATE}/src.user" ]]; then
  cp -a "${CURRENTTEMPLATE}/src.user/" "${APPNAME}/" 2>/dev/null || {
    echo "- No populated user files."
  }
fi

echo "export TEMPLATE=${TEMPLATE}" >"${LIBNAME}/.template"
echo "export TEMPTYPE=lib" >>"${LIBNAME}/.template"
