VERSION=$(node -p "require('./package.json').dependencies.vaxe")
RED='\033[0;31m'
GREEN='\033[0;32m'
B_GREEN='\033[42m'
B_BLACK='\033[40m'
NC='\033[0m'
if [ "$1" == 'help' ]
then
  if [ "$2" == '' ]
  then
    echo 'vaxe <command>'
    echo
    echo List:
    echo
    echo -e "${B_BLACK}help <cmd>${NC} Search for help on <cmd>"
    echo -e "${B_BLACK}version${NC}    Current version and updates"
  elif [ "$2" == 'version' ]
  then
    echo 'Shows your current version of the package. If there is a new version and you are still on the old version, you will be offered the option to download the latest version automatically.'
  else
    echo -e "${RED}Error:${NC} Vaxe command does not exist."
  fi
elif [ "$1" == 'version' ]
then
  NOW_VERSION=`node -pe 'JSON.parse(process.argv[1]).version' "$(curl -s https://registry.npmjs.org/vaxe/latest)"`
  echo $VERSION
  if [ $VERSION != "^$NOW_VERSION" ]
  then
    echo -e 'There is a new version!' "${RED}$VERSION${NC} -> ${GREEN}$NOW_VERSION${NC}"
    echo 'Would you like to update your version? (y/n)'
    read -n1 input
    echo
    if [ "$input" == 'y' ]
    then
      bash -lic "npm i vaxe"
      clear
      echo -e "${B_GREEN}Success:${NC} The version has been updated." "${GREEN}$NOW_VERSION${NC}"
    elif [ "$input" == 'n' ]
    then
      echo The package remains at version $VERSION.
    else
      echo No suitable input received.
      echo The package remains at version $VERSION.
    fi
  fi
elif [ "$2" == 'download-example' ]
then
  bash -lic "git clone https://github.com/orielhaim/vaxe-example.git"
else
  echo -e "${RED}Error:${NC} Vaxe command does not exist."
  echo -e "Execute ${B_BLACK}vaxe help${NC} for help."
fi