#!/bin/bash

AXIOM_PATH="$HOME/.axiom"
source "$AXIOM_PATH/interact/includes/vars.sh"
source "$AXIOM_PATH/interact/includes/functions.sh"

instances=""
cache=false
args=""
custom_config=""
ssh_conf=""

i=0

function help() {
        echo -e "${Red}Usage of axiom-scp copying files between fleets${Color_Off}"
       	echo -e "--cache (optional)"
	      echo -e "    Do not regenerate SSH config"
        echo -e "-F (string)"
        echo -e "    Path of custom SSH config file to use"
        echo -e "Example Usage: ${Blue}axiom-scp 'myfleet*':/home/op/myfile.txt '~/local/folder/\$name.txt' --cache${Color_Off}"
        echo -e "    Copy a remote file from all instances in myfleet to local folder. Do no generate SSH config"
        echo -e "    \$name must be wrapped single quotes. \$name is a required literal string and gets interpolated from the instance name."
        echo -e "Example Usage: ${Blue}axiom-scp myfleet05:/home/op/myfile-from-myfleet05.txt '~/local/folder/myfile-from-myfleet05.txt' ${Color_Off}"
        echo -e "    Copy a remote file from one instance 'myfleet05' to a local file"
        echo -e "Example Usage: ${Blue}axiom-scp myfile.txt 'myfleet*':/home/op/myfile.txt ${Color_Off}" 
        echo -e "    Copy a local file to all instances in myfleet"
        echo -e "Example Usage: ${Blue}axiom-scp mytemplates/ 'myfleet*':/home/op/custom-templates ${Color_Off}"
        echo -e "    Copy a local folder to all instances in myfleet"
} 

if [[ "$@" == "--help" ]] || [[ "$@" == "-h" ]] || [[ "$@" == "" ]]; then
help
exit
fi

for var in "$@"
do
    set=false
    if [[ "$var" == "--cache" ]]; then
		cache=true
        set=true
    fi

    if [[ "$var" =~ "-F=" ]]; then
        custom_config="$(echo "$var" | cut -d '=' -f 2)"
        cache=true
        set=true
    fi

    if [[ "$set" == "false" ]]; then
		args="$args $var"
    fi
	i=$((i+1))
done

if [ $cache != "true" ]
then
	generate_sshconfig
fi

if [[ "$custom_config" != "" ]]; then
    if [[ -f "$custom_config" ]]; then
        ssh_conf="$custom_config"
    else
        echo -e "${Red}Error: Config '$custom_config' not found... Reverting to default...${Color_Off}"
        ssh_conf="$AXIOM_PATH/.sshconfig"
    fi
else
    ssh_conf="$AXIOM_PATH/.sshconfig"
fi

echo -n "" > $AXIOM_PATH/tmp/cmds.txt

for var in $args
do
	host=$(echo $var | cut -d ":" -f 1)
	if [[ "$host" =~ "*" ]]
	then
		instances=$(query_instances_cache "-F=$ssh_conf" "$host")
		full=$(echo "$var" | sed "s/\$name/$instance/g")
		if [[ "$str" ]]
		then
			str="$str $full"
		else
			str="$full"
		fi

	else
		if [[ "$str" ]]
		then
			str="$str $var"
		else
			str="$var"
		fi
	fi
done

if [ "$instances" ]; then
    for instance in $instances
    do
        line=""
    for var in $args
    do
	    host=$(echo $var | cut -d ":" -f 1)
	    str=""
	    if [[ "$host" =~ "*" ]]
	    then
		    full=$(echo "$var" | sed "s/$host/$instance/g"| sed 's/\*//g')
		    if [[ "$str" ]]
		    then
			    str="$str $full"
		    else
			    str="$full"
		    fi
	    else
		    if [[ "$str" ]]
		    then
			    str="$str $var"
		    else
			    str="$var"
		    fi
        fi

	    str=$(echo $str | sed "s/\$name/$instance/g")

	    if [ "$line" ]
	    then
		    line="$line $str"
	    else
		    line="$str"
	    fi
    done

    cmd="rsync -e 'ssh -o StrictHostKeyChecking=no -F $ssh_conf' -azr --progress $line"
    echo "$cmd" >> $AXIOM_PATH/tmp/cmds.txt
    #scp -r -o StrictHostKeyChecking=no -F $AXIOM_PATH/.sshconfig $line
    done
    #sleep 1
    echo -e "${BGreen}Running parallel file transfer...${Color_Off}"
    interlace --silent -t NULL -cL $AXIOM_PATH/tmp/cmds.txt
else
    rsync -e "ssh -o StrictHostKeyChecking=no -F $ssh_conf"  -avzr --progress $args --timeout=10
fi
