ARGS_COUNT=$#
help () {
  echo "CLI Tool used to initialize the database for vintagecoding.net."
  echo "With no arguments, it creates the application user in MariaDB,"
  echo "and creates the following tables:"
  echo "- roles (populated);"
  echo "- tags (populated);"
  echo "- user;"
  echo "- articles;"
  echo "- article_tags;"
  echo ""
  echo "The following flags are accepted:"
  echo "    init_db.sh { --help | -h }"
  echo "    init_db.sh { --delete | -d } { --verbose | -v }"
}

if [[ $ARGS_COUNT > 0 ]]; then
  if [[ "$1" == "--help" || "$1" == "-h" ]]; then
    help
    exit
  fi

  # Delete the existing database and start over.
  if [[ "$1" == "--delete" || "$1" == "-d" ]]; then
    if [[ "$2" == "--verbose" || "$2" == "-v" ]]; then
      echo "Deleting the existing database vintagecoding..."
    fi
    echo "vintage user pw"
    mariadb -u vintage -p -e "DROP DATABASE vintagecoding;"

    if [[ "$2" == "--verbose" || "$2" == "-v" ]]; then
      echo "Deleting the vintage user..."
    fi
    echo "root user pw"
    mariadb -u root -p -e "DROP USER vintage@localhost;"

    if [[ "$2" == "--verbose" || "$2" == "-v" ]]; then
      echo "Creating the vintage user..."
    fi
    echo "root user pw"
    mariadb -u root -p < setup.sql;

    if [[ "$2" == "--verbose" || "$2" == "-v" ]]; then
      echo "Creating (and populating enum/static data) tables..."
    fi
    echo "vintage user pw"
    mariadb -u vintage -p < init.sql;

    exit
  fi
fi

#mariadb -u root -p < setup.sql;
mariadb -u vintage -p < init.sql;
