#!/bin/bash
# info: add ssh key
# options: USER [FORMAT]
#
# Lists $user/.ssh/authorized_keys

#----------------------------------------------------------#
#                Variables & Functions                     #
#----------------------------------------------------------#

# Argument definition
user=$1

format=${2-shell}

# Includes
# shellcheck source=/etc/hestiacp/hestia.conf
source /etc/hestiacp/hestia.conf
# shellcheck source=/usr/local/hestia/func/main.sh
source $HESTIA/func/main.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"

# JSON list function
json_list() {
	IFS=$'\n'
	i=1
	objects=$(echo "$keys" | wc -l)
	echo "{"
	for str in $keys; do
		KEY=$(echo $str | awk '{print $(NF-2)}')
		ID=$(echo $str | awk '{print $(NF-1)}')
		echo -n '    "'$ID'": {
        "ID": "'$ID'",
        "KEY": "'$KEY'"
        }'
		if [ "$i" -lt "$objects" ]; then
			echo ','
		fi
		((i++))
	done
	echo "}"
}

shell_list() {
	IFS=$'\n'
	echo "ID~KEY"
	echo "----~----~---"
	for str in $keys; do
		KEY=$(echo $str | awk '{print $(NF-2)}')
		ID=$(echo $str | awk '{print $(NF-1)}')
		echo "$ID~$KEY"
	done
}

# PLAIN list function
plain_list() {
	IFS=$'\n'
	for str in $keys; do
		KEY=$(echo $str | awk '{print $(NF-2)}')
		ID=$(echo $str | awk '{print $(NF-1)}')
		echo -e "$ID\t$KEY"
	done
}

# CSV list function
csv_list() {
	IFS=$'\n'
	echo "ID,KEY"
	for str in $keys; do
		KEY=$(echo $str | awk '{print $(NF-2)}')
		ID=$(echo $str | awk '{print $(NF-1)}')
		echo "\"$ID\",\"$KEY\""
	done
}

#----------------------------------------------------------#
#                    Verifications                         #
#----------------------------------------------------------#

check_args '1' "$#" 'USER'
is_format_valid 'user'
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"

#----------------------------------------------------------#
#                       Action                             #
#----------------------------------------------------------#

AUTHKEY_FILE="$HOMEDIR/$user/.ssh/authorized_keys"
[ -z "$(readlink -f "$AUTHKEY_FILE" | egrep "^$HOMEDIR/$user/.ssh/")" ] && check_result $E_FORBIDEN "Invalid authorized keys path"

#check if file exsists
if [ ! -f "$AUTHKEY_FILE" ]; then
	exit
fi

first_loop=1
while read l; do
	sshkey=$([[ -n $l && ${l###} = $l ]] && ssh-keygen -l -f <(echo $l))
	if [ -n "$sshkey" ]; then
		(($first_loop)) && keys="$sshkey" || keys="$keys"$'\n'"$sshkey"
		unset first_loop
	fi
done < "$AUTHKEY_FILE"

# Listing data
case $format in
	json) json_list ;;
	plain) plain_list ;;
	csv) csv_list ;;
	shell) shell_list | column -t ;;
esac

#----------------------------------------------------------#
#                       Hestia                             #
#----------------------------------------------------------#

# Logging

exit
