#!/usr/bin/env bash

AXIOM_PATH="$HOME/.axiom"
DOCTL_CONFIG_PATH="$HOME/.config/doctl/config.yaml"
source "$AXIOM_PATH/interact/includes/vars.sh"
source "$AXIOM_PATH/interact/includes/functions.sh"
source "$AXIOM_PATH/interact/includes/system-notification.sh"
account_path=$(ls -la $AXIOM_PATH/axiom.json | rev | cut -d " " -f 1 | rev)
region="$(jq -r '.region' "$AXIOM_PATH"/axiom.json)"
snapshot_name=axiom-$region-$(date +%s)

BASEOS="$(uname)"
provider="$(jq -r ".provider" "$AXIOM_PATH/axiom.json")"
imageid="$(jq -r ".imageid" "$AXIOM_PATH/axiom.json")"
linodeid=""
dropletid=""
vsiid=""

function help() {
	echo -e "${BWhite}Usage of axiom-backup${Color_Off}"
	echo -e "Description: ${Blue} Create a cloud hosted backup of an axiom instance to deploy future axiom instances/fleets with.${Color_Off}"
	echo -e "Example Usage: ${Blue}axiom-backup${Color_Off}"
}

for var in "$@"
do   
if [[ "$var" == "-h" ]] || [[ "$var" == "--help" ]];
 then
  help
  exit 0
fi
done


if [[ "$provider" == "linode" ]]; then
tput setaf 4; echo "Enter the Linode ID to use for axiom-init/axiom-fleet"
tput setaf 15;linode-cli linodes list
tput setaf 6; read linodeid
newimageid=$(linode-cli linodes disks-list "$linodeid" --text | grep axiom | tr '\t' ' ' | cut -d ' ' -f 1)
newimagelabel=$(linode-cli images create --disk_id "$newimageid" --text --label $snapshot_name | grep axiom | cut -f 2 | tr -d ' ') 
jq '.imageid="'$newimagelabel'"' <"$account_path">"$AXIOM_PATH"/tmp.json ; mv "$AXIOM_PATH"/tmp.json "$account_path"
tput setaf 4; echo "Created image out of Linode"
tput setaf 6; echo "Updated $account_path, replaced imageid $imageid with $snapshot_name"
tput setaf 4; echo "It can take a few minutes for the image to become active"
tput setaf 6; echo "Once the image is active run axiom-init or axiom-fleet to deploy using your custom image"

fi


if [[ "$provider" == "do" ]]; then
tput setaf 4; echo "Enter the Droplet ID to use for axiom-init/axiom-fleet"
tput setaf 15; doctl compute droplet list
tput setaf 6; read dropletid
newimageid=$(doctl compute droplet-action snapshot "$dropletid" --snapshot-name "$snapshot_name") 
jq '.imageid="'$snapshot_name'"' <"$account_path">"$AXIOM_PATH"/tmp.json ; mv "$AXIOM_PATH"/tmp.json "$account_path"
tput setaf 4; echo "Created image out of Droplet"
tput setaf 6; echo "Updated $account_path, replaced imageid $imageid with $snapshot_name"
tput setaf 4; echo "It can take a few minutes for the image to become active"
tput setaf 6; echo "Once the image is active run axiom-init or axiom-fleet to deploy using your custom image"

fi


if [[ "$provider" == "ibm" ]]; then
tput setaf 4; echo "Enter the VSI ID to use for axiom-init/axiom-fleet"
tput setaf 15; ibmcloud sl vs list
tput setaf 6; read vsiid
ibmcloud sl vs capture "$vsiid" --name $snapshot_name
jq '.imageid="'$snapshot_name'"' <"$account_path">"$AXIOM_PATH"/tmp.json ; mv "$AXIOM_PATH"/tmp.json "$account_path"
tput setaf 4; echo "Created image out of VSI"
tput setaf 6; echo "Updated "$account_path" , replaced imageid $imageid with $snapshot_name"
tput setaf 4; echo "It can take a few minutes for the image to become active"
tput setaf 6; echo "Once the image is active run axiom-init or axiom-fleet to deploy using your custom image"

fi


if [[ "$provider" == "aws" ]]; then
tput setaf 4; echo "Enter the instance ID to use for axiom-init/axiom-fleet"
tput setaf 15; aws ec2 describe-instances --query 'Reservations[*].Instances[*].{Instance_ID:InstanceId,Zone:Placement.AvailabilityZone,State:State.Name,Name:Tags[?Key==`Name`]|[0].Value}' --output table
tput setaf 6; read awsid
aws ec2 create-image --instance-id "$awsid" --name $snapshot_name
jq '.imageid="'$snapshot_name'"' <"$account_path">"$AXIOM_PATH"/tmp.json ; mv "$AXIOM_PATH"/tmp.json "$account_path"
tput setaf 4; echo "Created image out of EC2 instance"
tput setaf 6; echo "Updated "$account_path" , replaced imageid $imageid with $snapshot_name"
tput setaf 4; echo "It can take a few minutes for the image to become active"
tput setaf 6; echo "Once the image is active run axiom-init or axiom-fleet to deploy using your custom image"

fi
