#!/usr/bin/env bash

###################################################################
# Title	: axiom-images                                                                                             
# Author : 0xtavian                                                
# Github : https://github.com/0xtavian                                           
###################################################################

AXIOM_PATH="$HOME/.axiom"
source "$AXIOM_PATH/interact/includes/vars.sh"
source "$AXIOM_PATH/interact/includes/functions.sh"
source "$AXIOM_PATH/interact/includes/system-notification.sh"
begin=$(date +%s)
start="$(pwd)"
BASEOS="$(uname)"
account_path=$(ls -la $AXIOM_PATH/axiom.json | rev | cut -d " " -f 1 | rev)
accounts=$(ls -l "$AXIOM_PATH/accounts/" | grep "json" | grep -v 'total ' | awk '{ print $9 }' | sed 's/\.json//g')
current=$(ls -lh ~/.axiom/axiom.json | awk '{ print $11 }' | tr '/' '\n' | grep json | sed 's/\.json//g') > /dev/null 2>&1


function usage() {
  cat << EOF
 Examples: axiom-images ls && axiom-images rm axiom-barebones-1634682130 && axiom-images use axiom-default-1634682131
 Usage:
 ls                                      List snapshots created
 get                       Display info about the current image
 use | set              Use snapshot for axiom-init/axiom-fleet
 rm                                Remove snapshot from account
 help | --help | -h                        Print this help menu
EOF
exit
}

###########################################################################################################
# Declare defaut variables
list=false
help=true
rm=false
use=false
# Parse command line arguments 
#
i=0
for arg in "$@"
do
    i=$((i+1))
    if [[  ! " ${pass[@]} " =~ " ${i} " ]]; then
        set=false
        if [[ "$i" == 1 ]]; then
            input="$1"
            set=true
            pass+=($i)
        fi
        if [[ "$arg" == "rm" ]]; then
            n=$((i+1))
            rm=true
            snapshot=$(echo ${!n})
            set=true
            pass+=($i)
            pass+=($n)
        fi
        if [[ "$arg" == "use" ]] || [[ "$arg" == "set" ]] ; then
            n=$((i+1))
            use=true
            snapshot=$(echo ${!n})
            set=true
            pass+=($i)
            pass+=($n)
        fi
        if [[ "$arg" == "ls" ]]; then
            n=$((i+1))
            list="true"
            set=true
            pass+=($i)
            pass+=($n)
        fi
        if [[ "$arg" == "--help" ]] || [[ "$arg" == "-h" ]] || [[ "$arg" == "help" ]]; then
            usage
            exit
            set=true
            pass+=($i)
        fi
        if [[ "$arg" == "get" ]] ; then
            get="true"
            set=true
            pass+=($i)
        fi
        if [[ "$arg" == "--debug" ]]; then
            debug="true"
            set=true
            pass+=($i)
        fi
        if  [[ "$set" != "true" ]]; then
            args="$args $arg"
        fi

    fi
done

# Display Help Menu
#
if [[ "$@" == "--help" ]] || [[ "$@" == "-h" ]] || [[ "$@" == "" ]] || [[ "$@" == "help" ]]; then
usage
exit
fi

# Get the image in /.axiom/accout/account.json
#
if [[ "$get" == "true" ]]; then
echo -e "${Green}Displaying Account Info from: $current${Color_Off}"
cat $(echo $account_path)
exit 1
fi


# List Snapshots
#
if [[ "$list" == "true" ]]; then
echo -e "${Green}Listing Available Snapshots in Account: $current${Color_Off}"
get_snapshots
exit 1
fi

# If snapshot name not supplied, exit
#
if [ -z ${snapshot:+x} ]; then echo -e "${Red}Snapshot name not supplied. To list all snapshots run axiom-images ls${Color_Off}" && exit 1; fi


# Delete Snapshot by its name
#
if [[ "$rm" == "true" ]]; then
echo -e "${Green}Deleting Snapshot by name: $(echo $snapshot)${Color_Off}"
delete_snapshot $snapshot || { echo -e "${Red}Failed to find snapshot with name: "$snapshot"... exiting${Color_Off}" ; exit 1; }
echo -e "${Green}Successfully deleted snapshot!${Color_Off}"
fi

# Use Snapshot by name. Run axiom-init or axiom-fleet to use after updating
#
if [[ "$use" == "true" ]]; then
old="$(cat "$AXIOM_PATH/axiom.json" | jq -r '.imageid')"
jq '.imageid="'$snapshot'"' <"$account_path">"$AXIOM_PATH"/tmp.json ; mv "$AXIOM_PATH"/tmp.json "$account_path"
get_snapshots | grep $snapshot || { echo -e "${Red}Failed to find snapshot with name: "$snapshot"... exiting${Color_Off}" ; exit 1; }
echo -e "${Green}Successfully updated imageid $old with new imageid in $snapshot in account: $current${Color_Off}"
echo -e "${Green}Run axiom-init or axiom-fleet to use it!${Color_Off}"
fi
