#!/usr/bin/env bash

base_dir=$(dirname "$0")
cd "$base_dir"/../../ || exit 1
APPDIR=$(pwd)
FRAMEWORKDIR="$APPDIR"/vendor/aliemam/mirage_core/src
COMMAND=$1
APPNAME=$2

case $COMMAND in
  init)
    chmod 777 "$APPDIR/log"
    if [ ! -f "$APPDIR/app/constants/Err.php" ]; then
      \cp -f "$FRAMEWORKDIR/constants/Err.php" "$APPDIR/app/constants/"
    fi
    if [ ! -f "$APPDIR/app/constants/Msg.php" ]; then
      \cp -f "$FRAMEWORKDIR/constants/Msg.php" "$APPDIR/app/constants/"
    fi
    if [ ! -f "$APPDIR/app/constants/Ok.php" ]; then
      \cp -f "$FRAMEWORKDIR/constants/Ok.php" "$APPDIR/app/constants/"
    fi
    if [ ! -f "$APPDIR/app/constants/ReqMethod.php" ]; then
      \cp -f "$FRAMEWORKDIR/constants/ReqMethod.php" "$APPDIR/app/constants/"
    fi
    if [ ! -f "$APPDIR/app/constants/Services.php" ]; then
      \cp -f "$FRAMEWORKDIR/constants/Services.php" "$APPDIR/app/constants/"
    fi
    if [ ! -f "$APPDIR/lang/en.php" ]; then
      \cp -f "$FRAMEWORKDIR/lang/en.php" "$APPDIR/lang/"
    fi
    ;;
  appname)
    [ -z "$APPNAME" ] && { echo "Error: give me a name"; exit 1; }
    cd ..
    mv mirage_microservice "$APPNAME"
    # shellcheck disable=SC2164
    cd "$APPNAME"
    APPDIR=$(pwd)
    # TO DO
    ;;
  removegit)
    rm -rf .git
    ;;
  *)
    echo -e "mirage [appname project_name|removegit]"
esac
