#!/bin/bash

# Workaround to nullify yarn 2+ auto git init with master branch
git init --initial-branch=main
yarn init --yes

yarn set version berry --yarn-path

yarn add \
  @jig-software/sdk \
  @jig-software/trest-core \
  @jig-software/trest-client \
  @jig-software/zod-bson \
  @jig-software/logger \
  @jig-software/logger-winston \
  debug \
  bson \
  koa \
  koa-body \
  @koa/router \
  zod \
  mongodb \
  eventemitter3 \
  reflect-metadata

yarn add --dev \
  @types/koa \
  @types/koa__router \
  @types/debug \
  @types/node \
  commitizen cz-emoji \
  ts-node-dev \
  @typescript-eslint/eslint-plugin \
  @typescript-eslint/parser \
  eslint@8 \
  eslint-import-resolver-typescript \
  eslint-plugin-import \
  eslint-config-prettier \
  typescript \
 	prettier \
  mocha \
  @types/mocha \
  chai@^4.3.10 \
  @types/chai@^4.3.11 \
  nyc \
  ts-node \
  @types/node \
  rimraf

yarn dlx @yarnpkg/sdks vscode

# Format by prettier
yarn prettier . --write

# Set npm environment to skip confirmation prompt
orig_npm_config_yes=$npm_config_yes
export npm_config_yes=yes

# Add lint script and package config to the project
./patch-package-json.sh

# Enable .gitea/workflows
mv .gitea/workflows.template .gitea/workflows


# Restore the original env
export npm_config_yes=$orig_npm_config_yes

for (( ; ; )); do
  read -p "The initialization process is done. Do you want to remove this script file now? (Y/n): " remove
  remove=${remove:-y}
  echo $remove
  if [ "$remove" = "y" ] || [ "$remove" = "Y" ]; then
    rm ./package.patch.json
    rm ./patch-package-json.sh
    rm -- "$0"
    break
  else
    if [ "$remove" = "n" ] || [ "$remove" = "N" ]; then
      break
    fi
  fi
done
