#!/bin/bash

function check_env_variables {
  for var in \
    "SWAGGER_NAMESPACE_ID" \
    "GITLAB_PRIVATE_TOKEN" \
    "GITLAB_AUTH_TOKEN" \
    "NPM_TOKEN" \
    "RUNNER_ID" \
  ; do
    if [ "${!var}" == "" ]; then
      echo "$var is not defined. Check the README for more details."
      exit -1
    fi
  done
}
function prompt {
  if [ "$1" == "" ]; then
    echo "Type the package name"
    read NAME
  else
    NAME=$1
  fi

  if [ "$2" == "" ]; then
    echo "Type the package type <path|operation|definition|model|helper>"
    read TYPE
  else
    TYPE=$2
  fi

  if [ "$AUTHOR_NAME" == "" ]; then
    if [ "$3" == "" ]; then
      echo "Type author name"
      read AUTHOR_NAME
    else
      AUTHOR_NAME=$3
    fi
  fi

  if [ "$AUTHOR_EMAIL" == "" ]; then
    if [ "$4" == "" ]; then
      echo "Type author email"
      read AUTHOR_EMAIL
    else
      AUTHOR_EMAIL=$4
    fi
  fi

  echo "Type the package description"
  read DESCRIPTION
}
function set_variables {
  FILE=$1
  CLASS=$(echo swg-$NAME-$TYPE | sed -r 's/(^|-)([a-z])/\U\2/g')
  FUNCTION=$(echo ${CLASS,})
  NAME_CLASS=$(echo $NAME | sed -r 's/(^|-)([a-z])/\U\2/g')
  NAME_FUNCTION=$(echo ${NAME_CLASS,})
  sed -i \
    -e "s/<NAME>/$NAME/g" \
    -e "s/<TYPE>/$TYPE/g" \
    -e "s/<AUTHOR_NAME>/$AUTHOR_NAME/g" \
    -e "s/<AUTHOR_EMAIL>/$AUTHOR_EMAIL/g" \
    -e "s/<DESCRIPTION>/$DESCRIPTION/g" \
    -e "s/<CLASS>/$CLASS/g" \
    -e "s/<FUNCTION>/$FUNCTION/g" \
    -e "s/<NAME_CLASS>/$NAME_CLASS/g" \
    -e "s/<NAME_FUNCTION>/$NAME_FUNCTION/g" \
    ./swg-$NAME-$TYPE/$FILE
}
function create_folders {
  for folder in \
    "swg-$NAME-$TYPE" \
  ; do
    echo "create the folder $folder"
    mkdir -p $folder
  done
  if [ "$TYPE" == "path" ] || [ "$TYPE" == "operation" ]; then
    for folder in \
      "swg-$NAME-$TYPE/controllers" \
      "swg-$NAME-$TYPE/mocks" \
    ; do
      echo "create the folder $folder"
      mkdir -p $folder
    done
  fi
}
function simple_copies {
  for file in \
    ".gitignore" \
    ".gitlab-ci.yml" \
    "LICENSE" \
    "package.json" \
  ; do
    echo "copy $file"
    cp ./swg-template/template/$file ./swg-$NAME-$TYPE/$file
    set_variables $file
  done
}
function simple_type_copies {
  for file in \
    "README.md" \
  ; do
    echo "copy $file"
    cp ./swg-template/template/$file.$TYPE ./swg-$NAME-$TYPE/$file
    set_variables $file
  done
  if [ "$TYPE" != "helper" ]; then
    for file in \
      "swagger.yaml" \
    ; do
      echo "copy $file"
      cp ./swg-template/template/$file.$TYPE ./swg-$NAME-$TYPE/$file
      set_variables $file
    done
  fi
  if [ "$TYPE" == "path" ] || [ "$TYPE" == "operation" ]; then
    for file in \
      "swagger.yaml" \
      "index.spec.js" \
    ; do
      echo "copy $file"
      cp ./swg-template/template/$file.$TYPE ./swg-$NAME-$TYPE/$file
      set_variables $file
    done
  fi
  if [ "$TYPE" == "helper" ]; then
    for file in \
      "index.js" \
      "index.spec.js" \
    ; do
      echo "copy $file"
      cp ./swg-template/template/$file.$TYPE ./swg-$NAME-$TYPE/$file
      set_variables $file
    done
  fi
  if [ "$TYPE" == "path" ] || [ "$TYPE" == "operation" ]; then
    echo "create the file ./controllers/swg-$NAME-$TYPE.js"
    cp ./swg-template/template/index.js.$TYPE ./swg-$NAME-$TYPE/controllers/swg-$NAME-$TYPE.js
    set_variables controllers/swg-$NAME-$TYPE.js

    echo "create the file ./mocks/swg-$NAME-$TYPE.js"
    cp ./swg-template/template/index.js.$TYPE ./swg-$NAME-$TYPE/mocks/swg-$NAME-$TYPE.js
    set_variables mocks/swg-$NAME-$TYPE.js
  fi
}
function change_directory {
  echo "change directory to ./swg-$NAME-$TYPE"
  cd ./swg-$NAME-$TYPE
}
function create_git_repository {
  echo "create gitlab repository"
  curl -X POST --header "PRIVATE-TOKEN: $GITLAB_PRIVATE_TOKEN" \
    --form "namespace_id=$SWAGGER_NAMESPACE_ID" \
    --form "visibility=public" \
    --form "path=swg-$NAME-$TYPE" \
    --form "description=$DESCRIPTION" \
    https://gitlab.com/api/v4/projects > /dev/null
}
function add_secret_variables {
  for var in \
    "GITLAB_AUTH_TOKEN" \
    "NPM_TOKEN" \
  ; do
    echo "add $var variable"
    curl -X POST --header "PRIVATE-TOKEN: $GITLAB_PRIVATE_TOKEN" \
      --form "key=$var" \
      --form "value=${!var}" \
      --form "protected=false" \
      https://gitlab.com/api/v4/projects/4geit%2Fswagger%2Fswg-$NAME-$TYPE/variables > /dev/null
  done
}
function enable_runner {
  echo "enable the CI runner $RUNNER_ID"
  curl -X POST --header "PRIVATE-TOKEN: $GITLAB_PRIVATE_TOKEN" \
    --form "runner_id=$RUNNER_ID" \
    https://gitlab.com/api/v4/projects/4geit%2Fswagger%2Fswg-$NAME-$TYPE/runners > /dev/null
}
function git_actions {
  echo "git init"
  git init > /dev/null

  echo "add remote repository URL"
  git remote add origin git@gitlab.com:4geit/swagger/swg-$NAME-$TYPE.git

  echo "git add"
  git add .

  echo "git commit"
  git commit -m "feat(init) new repository" > /dev/null

  echo "git push"
  git push -u origin master > /dev/null
}
function yarn_actions {
  echo "yarn link"
  yarn link
}
function usage {
  echo
  echo
  echo "The new repository has been properly generated and is available at https://gitlab.com/4geit/swagger/swg-$NAME-$TYPE"
  echo
  echo "You can clone the repository with the following command:"
  echo
  echo "git clone git@gitlab.com:4geit/swagger/swg-$NAME-$TYPE.git"
  echo
  echo "Or just get access to the locally generated folder:"
  echo
  echo "cd swg-$NAME-$TYPE"
  echo
}

check_env_variables
prompt $*
create_folders
simple_copies
simple_type_copies
change_directory
create_git_repository
add_secret_variables
enable_runner
git_actions
yarn_actions
usage
