#!/bin/bash

# check if stdout is a terminal
if test -t 1; then

  # see if it supports colors
  ncolors=$(tput colors)

  if test -n "$ncolors" && test $ncolors -ge 2; then
    HILIGHT='\033[0;32m'
    COMMENT='\033[0;33m'
    INFO='\033[0;36m'
    ERROR='\033[1;31m'
    B='\033[1;38m'
    U='\033[4;37m'
    C='\033[0m' # Clear (no color)
  fi

  version=$(cat "$(dirname "$0")/../electro/installer/composer.json" | grep -e \"version\" | grep -Eo "[0-9.]+")

  printf "$INFO
  ┌──┐ │  ┌──┐┌──╴╶┼─ ┌──╴┌──┐
  ├──┘ │  ├──┘│    │  │   │  │
  └──╴ └─ └──╴└──╴ └─ ╵   └──┘
              ${C}Installer $version
"
fi

syntax()
{
  printf "
${B}SYNTAX$C
    ${B}electro$C ${U}command$C [${U}arguments$C] [${U}options$C]

${B}COMMANDS$C
    ${HILIGHT}${B}create$C  - Creates a new, blank project.

        ${B}SYNTAX$C
            ${B}electro create$C ${U}project name$C [${B}--version$C version] [${B}--unstable$C] [${B}--framework-dev$C] [${U}other Composer options$C]

        ${B}ARGUMENTS$C
            ${U}project name$C     A relative (or absolute) path to the folder that will be created for the project.
                             If it's a simple name, a folder with that name will be created on the current directory.

        ${B}OPTIONS$C
            ${B}--version$C        Installs the specified framework version (ex: 1.0.0)

            ${B}--unstable$C       Installs the latest development versions of all framework packages.
                             Use this if you want the bleeding edge features. Warning: your project MAY BREAK temporarily
                             whenever unstable/unfinished changes to the framework are published.

            ${B}--framework-dev$C  This is only for framework developers.
                             Installs all framework-related packages from source, to allow committing changes to their repos.
                             3rd-party packages are still installed in 'distribution' mode.

    ${HILIGHT}${B}versions$C  - Lists all available framework versions.

    ${HILIGHT}${B}self-update$C - Updates this tool to the latest version.

"
  exit 1
}

syntax-error ()
{
  echo -e "
${ERROR}Syntax error!$C"
  syntax
}

command=$1
shift
case "$command" in

  "create")
    prefer="dist"
    stabbool="true"
    stability="stable"
    version=""
    mode="distribution"

    if [[ -n "$1" && ! "$1" =~ ^-- ]]; then
      name="$1"
      shift
    else
      syntax-error
    fi

    while true; do
      case "$1" in
        "--framework-dev")
          prefer='{
      "electro/*": "source",
      "electro-modules/*": "source",
      "selenia/*": "source",
      "php-kit/*": "source",
      "*": "dist"
    }'
          mode="framework development"
          ;;
        "--unstable")
          stability="dev"
          version="dev-master"
          ;;
        "--version")
          shift
          version="$1"
          ;;
        *)
          break
      esac
      shift
    done

    printf "
Creating project:   ${HILIGHT}$name$C
         version:   ${HILIGHT}${version:-latest}$C
         stability: ${HILIGHT}$stability$C
         mode:      ${HILIGHT}$mode$C

"
    composer create-project --no-install -s $stability --keep-vcs electro/electro $name $version "$@" || exit $?
    cd $name
    rm -Rf .git || exit $?

    # Set minimum stability
    sed -i -e "$(echo 's/"minimum-stability": ".*"/"minimum-stability": "'$stability'"/')" composer.json

    # Set stability preference
    sed -i -e "$(echo 's/"prefer-stable": .*,/"prefer-stable": '$stabbool',/')" composer.json

    # Set installation type: source/dist
    if [ "$prefer" != "dist" ]; then
      input="$(cat composer.json)"; find='"preferred-install": "dist"'; replace='"preferred-install": '$prefer
      echo "$(echo "${input//$find/$replace}")" > composer.json
    fi

    # Update Composer config template
    cp composer.json composer.root.json

    composer install || exit $?
    workman init
    printf "%s

If you need to run additional commands on this project, don't forget to enter its directory first
by running this command: ${HILIGHT}cd $name$C

" "-------------------------------------------------------------------------------------------------"
    ;;
  "versions")
    echo -e "
${B}Available versions:$C
"
    git ls-remote --tags https://github.com/electro-framework/electro | grep -o '[0-9]*\.[0-9]*\.[0-9]*$'
    echo
    ;;
  "self-update")
    composer global update electro/installer
    ;;
  "--help"|"")
  printf "
${B}NAME$C
    ${B}electro$C -- command line tool to create new ${INFO}electro framework$C projects
"
    syntax
    ;;
  *)
    syntax-error
esac
