#! /bin/bash
KEY_FILE1=".api-key1"
KEY_FILE2=".api-key2"
JACAMO="/usr/bin/jacamo"
NPM="/usr/bin/npm"
clear
if [[ ! -f "$JACAMO" ]] || [[ ! -f "$NPM" ]]
then
    echo "The computer hasn't JaCaMo or NPM!"
    echo "Installing dependencies..."
    sleep 3
    echo "deb [trusted=yes] http://packages.chon.group/ chonos main" | sudo tee /etc/apt/sources.list.d/chonos.list
    sudo apt update
    sudo apt install jacamo-cli npm
    
else
    echo "The computer has JaCaMo and NPM"
fi


if [ ! -f "$KEY_FILE1" ]; then
  echo "First you need to create an API_KEY at: https://twitterapi.io/dashboard"
  echo "After create, please press ENTER"
  read

  echo -n "Inform your Twitter API_KEY: "
  read API

  echo "$API" > "$KEY_FILE1"
else
  API=$(cat "$KEY_FILE1")
fi

echo "Using Twitter API-KEY=$API"
sed -i "s|TWITTER_API_KEY=.*|TWITTER_API_KEY=$API|" artifact-runtime-ts/.env


if [ ! -f "$KEY_FILE2" ]; then
  echo "Second you need to create an API_KEY at: https://aistudio.google.com/api-keys"
  echo "After create, please press ENTER"
  read

  echo -n "Inform your Google AI Studio API_KEY: "
  read API2

  echo "$API2" > "$KEY_FILE2"
else
  API2=$(cat "$KEY_FILE2")
fi

sed -i "s|GEMINI_API_KEY=.*|GEMINI_API_KEY=$API2|" artifact-runtime-ts/.env


echo "Iniciando o Artefato..."
cd artifact-runtime-ts/
npm install
setsid npm run dev &

cd ../jacamo-remote-artifact-bridge/
jacamo remote_artifact_bridge.jcm

cd ..
echo "Parando o Artefato..."
kill $(lsof -t -i:8080)

sed -i "s|TWITTER_API_KEY=.*|TWITTER_API_KEY=NONE|" artifact-runtime-ts/.env
sed -i "s|GEMINI_API_KEY=.*|GEMINI_API_KEY=NONE|" artifact-runtime-ts/.env