PW_GLOBAL=""
USR_GLOBAL=""
get_creds(){
  # if PW_GLOBAL or USR_GLOBAL is empty
  if [ -z $PW_GLOBAL ] || [ -z $USR_GLOBAL ]
  then
    echo -e "Username: \c"
    read USR_GLOBAL
    # hide via ANSI just in case
    echo -e "Password: $HIDDEN\c"
    # read pw silently
    read -s PW_GLOBAL # read pw silently
    echo -e "$RESET"
  fi
}
reset_creds(){
  PW_GLOBAL=""
  USR_GLOBAL=""
}

upload_bash(){
  get_creds
  curl -T ~/.bashrc ftp://"$SERVER_NAME" --user $USR_GLOBAL:$PW_GLOBAL
}
upload_vim(){
  get_creds
  curl -T ~/.vimrc ftp://"$SERVER_NAME" --user $USR_GLOBAL:$PW_GLOBAL
}
upload_tmux(){
  get_creds
  curl -T ~/.tmux.conf ftp://"$SERVER_NAME" --user $USR_GLOBAL:$PW_GLOBAL
  # cd $TMUX_LOC/../
  # upload_dir .tmux misc/
 }
upload_dir(){
  get_creds
  local dir=$1
  local srv_path=$2
  if [[ ! $srv_path == *"/"* ]]; then
      srv_path=$srv_path/
  fi
  echo -e "\nUploading contents of $dir...\n"
  find $dir -type f -exec curl -u $USR_GLOBAL:$PW_GLOBAL --ftp-create-dirs -T {} ftp://$SERVER_NAME/$srv_path{} \; 
}
upload_misc(){
  get_creds
  curl -T $1 ftp://"$SERVER_NAME"/files/misc/ --user $USR_GLOBAL:$PW_GLOBAL
}
upload_private(){
  get_creds
  curl -T $1 ftp://"$SERVER_NAME"/misc_private/ --user $USR_GLOBAL:$PW_GLOBAL
}
upload_profile(){
  get_creds
  curl -T ~/.profile ftp://"$SERVER_NAME" --user $USR_GLOBAL:$PW_GLOBAL
}
upload_terminator(){
  get_creds
  curl -T ~/.config/terminator/config ftp://"$SERVER_NAME"/misc/terminator_config --user $USR_GLOBAL:$PW_GLOBAL
}
upload_alacritty(){
  get_creds
  curl -T $ALACRITTY_LOC/alacritty.yml ftp://"$SERVER_NAME"/misc/alacritty.yml --user $USR_GLOBAL:$PW_GLOBAL
}
upload_scripts(){
  # get_creds
  cd $SCRIPTS_LOC;
  git add -A;
  git commit -m "Updated scripts";
  git push origin master;
  # g_push "Updated scripts"


  # update scripts on server public dir
  # ssh $SERVER_USR@$SERVER_NAME "cd ~/.config/scripts; git pull origin master"
  ssh -p $GIT_PORT $SERVER_USR@$SERVER_NAME "cd ~/.config/scripts;\
    git fetch --all;\
    git reset --hard origin/master;\
    git pull origin master"

  # cd $SCRIPTS_LOC/../;
  # upload_dir scripts/ .config/


	# for file in $SCRIPTS_LOC/*
	# do
		# echo -e "\nUploading ${file}...\n"
    # curl -T $file ftp://$SERVER_NAME/.config/scripts/ --user $USR_GLOBAL:$PW_GLOBAL
	# done

}

upload_all(){
  get_creds 
	echo "Uploading ~/.bashrc..."
	upload_bash
  echo "Done."
	echo "Uploading ~/.vimrc..."
	upload_vim
	echo "Done."
  echo "Uploading ~/.tmux.conf..."
  upload_tmux
  echo "Done."
	echo "Uploading ~/.profile..."
	upload_profile
	echo "Done!"
  echo "Uploading ~/.config/terminator/config/..."
  upload_terminator
  echo "Done!"
  upload_alacritty
  echo "Done!"
	upload_scripts
	echo "Done!"
}

upload_key(){
  mkdir -p ~/.ssh;
  local dir=$(pwd);
  cd ~/.ssh;
  if [ ! -f ~/.ssh/id_rsa ]; then
    echo Generating new key...
    ssh-keygen -b 4096 -t rsa -N "" -f ~/.ssh/id_rsa;
    echo Generated new key
  fi
  if [ -z $1 ]; then
    echo Uploading key to home server...
    ssh-copy-id -i ~/.ssh/id_rsa.pub -p $GIT_PORT $SERVER_USR@$SERVER_NAME;
  else
    echo Copying key to $1
    ssh-copy-id -i ~/.ssh/id_rsa.pub $1;
  fi
  cd $dir;
}
remove_key(){
  if [ -z "$1" ]; then
    echo "Please enter a host"
    exit 1
  fi
  ssh-keygen -f "~/.ssh/known_hosts" -R "$1"
}
