#! /bin/bash
#
# If anyone spots any bugs or flaws or has an improvement to suggest, please email details to robinj@learningtree.com. Thanks.
# I realize layout is not optimal, but bash scripting is not one of my strong points! This is just a case of what works goes.
#
# REQUIRES CURL, ZENITY AND JQ TO BE INSTALLED
# sudo apt install curl zenity jq -f
#
# This tool tries to pull the Class-ID from metadata and the make an API call to retrieve Class Info an then ultimately the OpenAI key
# Basic operation
# 1. Pull class-id and OpenAI key from the metadata
# 2. Verify key is good
# 3. Replace/Insert key in appropriate files
# 4. Quit

# OPTIONAL ITEMS
# Scroll down and some optional items you can comment in or out with #
# Option to disable info popups for silent operation
if [[ "$1" == *"-s"* || "$1" == *"/s"* ]]; then
    SETPOPUPS=
else
    SETPOPUPS=YES
fi

logfile=$(dirname "$0")"/OpenAI_Import.log"
log() {
  echo "$1"
  echo "$1">>$logfile
}
echo Starting OpenAI_Import V2.0 -- $(date)>$logfile

# Check to see if what we need is installed

# Check if curl is installed
if ! command -v curl >/dev/null 2>&1; then
    log "curl is not installed. Please install it first."
    exit 1
fi

# Check if jq is installed
if ! command -v jq >/dev/null 2>&1; then
    log "jq is not installed. Please install it first."
    exit 1
fi

# Check if zenity is installed
if ! command -v zenity &> /dev/null; then
    log "zenity is not installed. Please install it first."
    exit 1
fi

# Pull the course-sequence combo from the freely available metadata
# ENVNAME=$(/usr/bin/curl -s -X GET "https://metadata.cloudshare.com/api/v3/unauthenticated/metadata" | jq -r '."env-name"')
# ENV=$(echo $ENVNAME| grep -oE '\b[A-Za-z0-9]{3,4}-[A-Za-z0-9]{8}\b')
CLASSID=$(/usr/bin/curl -s -X GET "https://metadata.cloudshare.com/api/v3/unauthenticated/metadata" | jq -r '."class-id"')
STATUS=$?
if [ $STATUS -ne 0 ]; then
  log "Error: metadata unavailable (exit code $STATUS). Likely a timeout or network issue."
  exit 1
fi

log;log "Debug values extracted from Cloudshare Metadata";log
# log ": The course-sequence number pulled is: $ENV"
log ": The class-id pulled is: $CLASSID"

if [ -z "$CLASSID" ] || [ "$CLASSID" == "null" ]; then
  log;log "Error: No classid found in metadata, exiting."
  # SHOULD PROBABLY WAIT HERE INCASE IT IS NOT INSTANTLY THERE
  exit 1
fi

OPENAI=$(/usr/bin/curl -s -X GET "https://metadata.cloudshare.com/api/v3/unauthenticated/metadata" | jq -r '.["custom-properties"].openAIKey')
if [ -z "$OPENAI" ] || [ "$OPENAI" == "null" ]; then
  log ": No OpenAI key was provided in metadata - Is this a valid deployment?"
  exit 1
else
  log ": OpenAI key pulled from metadata custom-properties: ${OPENAI}"
fi

# Check we have an OpenAI key first
if [ -z "$OPENAI" ]; then
  log "No key provided in VM metadata"
  exit 1
fi

# OPTIONAL CODE THAT PUTS UP GUI WHEN RUN ON BOOT
if ! [ -z ${SETPOPUPS} ]; then 
  zenity --question --title="OpenAI Key" --text="Do you wish to install the course OpenAI key?\n\n    (times out to YES after 120 seconds)" --timeout=120 2>/dev/null
  case $? in
      1)
      	log;log "Exited without installing the key"
        exit
      ;;
  esac
fi

# Should have the params we need from skytap now, lets find the correct file and add the Key

#1216		/home/student/.config/shell_gpt/.sgptrc					Boleslav Sykora
#*1258		/home/.bashsrc (hidden file) - Replace API Key in the last line.	Imran Ahmad - *Anup tested and confirmed that NO API KEY IS REQUIRED for 1258!
#1293		/home/student/keys/key.txt	WAS - /home/.bashsrc (hidden file) - Replace API Key in the last line.	Imran Ahmad
#1296		/home/student/keys/key.txt							Alastair Brown, Imran Ahmad
#1297		/home/student/Keys/Key.txt (copied from 1296)					Alastair Brown, Anup Kumar
#4703		/home/.bashsrc (hidden file) - Replace API Key in the last line.	Imran Ahmad
#526A (1293)	/home/student/keys/key.txt	WAS - /home/.bashsrc (hidden file) - Replace API Key in the last line.	Joshua Sigal
#536R (1293)	/home/student/keys/key.txt	WAS - /home/.bashsrc (hidden file) - Replace API Key in the last line.	Imran Ahmad
#1376           /home/student/keys/key.txt and /home/student/labs/.env - Replace API Key in the first line of .env file

#List of files that will be looked for and edited if found...
#Search and replace line with OPENAI_API_KEY= in it with new version
FILES="/home/student/.config/shell_gpt/.sgptrc
/home/student/labs/.env"
for f in $FILES
do
# Check if "$f" FILE exists and is a regular file and then edit it
  if [ -f "$f" ]
  then
    sed -i 's/OPENAI_API_KEY=.*/OPENAI_API_KEY='$OPENAI'/' "$f"
    log ": Edited OpenAI key into file $f"
    EDITED=TRUE
  else
    log ": Did not locate file $f"
  fi
done

FILES="/home/.bashsrc
/home/student/.bashrc"
for f in $FILES
do
# Check if "$f" FILE exists and is a regular file and then edit it
  if [ -f "$f" ]
  then
    sed -i 's/OPENAI_API_KEY=.*/OPENAI_API_KEY="'$OPENAI'"/' "$f"
    log ": Edited OpenAI key into file $f"
    EDITED=TRUE
  else
    log ": Did not locate file $f"
  fi
done

#Recreate these files so they just contain the key
FILES="/home/student/keys/key.txt
/home/student/Keys/Key.txt
/home/keys/key.txt"
for f in $FILES
do
# Check if "$f" FILE exists and is a regular file and then replace it
  if [ -f "$f" ]
  then
    echo $OPENAI > "$f"
    log ": Inserted OpenAI key into file $f"
    EDITED=TRUE
  else
    log ": Did not locate file $f"
  fi
done

# OPTIONAL CODE THAT PUTS UP GUI WHEN RUN ON BOOT
#if [ ! [ -z ${SETPOPUPS} ] &&  ! [ -z ${EDITED}] ]
if [[ -n "$SETPOPUPS" && -n $EDITED ]]
then 
  zenity --info --title="OpenAI Key" --text="The OpenAI key has been updated" --timeout=120 2>/dev/null
fi

log;log "All listed files have been edited if found, Exiting"

exit


