#!/usr/bin/bash
#This is not generated, in case you want to know.

set -euo pipefail

#Setting VARs================================
a_shell_type=()

v_error_occurred=false

v_fish_file="${XDG_CONFIG_HOME:-$HOME/.config}/fish/config.fish"
v_zsh_file="${ZDOTDIR:-$HOME}/.zshrc"
v_bash_file="$HOME/.bashrc"

m_success="--auto-init SHELL : Initialization success!"
m_already_initialized="--auto-init SHELL : Already initialized"

#Setting Functions===========================
f_help() {
  cat <<'EOF'
This is a third party wrapper of pay-respects to help initializing from openSUSE packager.

pay-respects requires initialization before using.

Read the initialization guide with: f --guide

usage: f [OPTIONS]
  With no options, display this help.
  Options:
    --guide
                display the initialization guide for pay-respects
    --auto-init <bash|zsh|fish>   
                automatically initialize pay-respects for specific shell
    -h, --help          
                display this help and exit

Submit bugs or comments to: <https://build.opensuse.org/package/show/home:jayhsu397:packages/pay-respects>
EOF
}
#_____________________________________________

f_guide() {
  printf 'Shell initialization

Use --auto-init to configure shell initialization automatically,

For manual setup, add the matching line to your shell configuration:

Bash (%s):
    eval "$(pay-respects bash)"

Zsh (%s):
    eval "$(pay-respects zsh)"

Fish (%s):
    pay-respects fish | source

Restart your shell, then use f to correct the previous command.

For more options and other supported shells, see upstream:
    https://github.com/iffse/pay-respects\n' "${v_bash_file}" "${v_zsh_file}" "${v_fish_file}"
}
#_____________________________________________

f_init_bash() {
  if ! grep -qFx 'eval "$(pay-respects bash)"' "${v_bash_file}"; then
    cat <<'EOF' >>"${v_bash_file}"

#initialization of pay-respects for Bash

eval "$(pay-respects bash)"

EOF
    echo "${m_success/SHELL/bash}"
  else
    echo "${m_already_initialized/SHELL/bash}"
  fi
}
#_____________________________________________

f_init_zsh() {
  if ! grep -qFx 'eval "$(pay-respects zsh)"' "${v_zsh_file}"; then
    cat <<'EOF' >>"${v_zsh_file}"

#initialization of pay-respects for Zsh

eval "$(pay-respects zsh)"

EOF
    echo "${m_success/SHELL/zsh}"
  else
    echo "${m_already_initialized/SHELL/zsh}"
  fi
}
#_____________________________________________

f_init_fish() {
  if ! grep -qFx 'pay-respects fish | source' "${v_fish_file}"; then
    cat <<'EOF' >>"${v_fish_file}"
    
#initialization of pay-respects for Fish

pay-respects fish | source

EOF
    echo "${m_success/SHELL/fish}"
  else
    echo "${m_already_initialized/SHELL/fish}"
  fi
}

#_____________________________________________

f_check_permission() {
  test -e "$1" || {
    printf "\e[0;31;1m--auto-init %s : file %s doesn't exist\e[0m\n" "$2" "$1" >&2
    v_error_occurred=true
    return 0
  }
  test -f "$1" || {
    printf "\e[0;31;1m--auto-init %s : file %s isn't a regular file\e[0m\n" "$2" "$1" >&2
    v_error_occurred=true
  }
  test -r "$1" || {
    printf "\e[0;31;1m--auto-init %s : file %s isn't readable\e[0m\n" "$2" "$1" >&2
    v_error_occurred=true
  }
  test -w "$1" || {
    printf "\e[0;31;1m--auto-init %s : file %s isn't writable\e[0m\n" "$2" "$1" >&2
    v_error_occurred=true
  }
}

#_____________________________________________

#Analyzing Options===========================
if (($# == 0)); then
  f_help
  exit 0
fi

while (($# >= 1)); do
  case "$1" in
  --guide)
    f_guide
    exit 0
    ;;
  --help | -h)
    f_help
    exit 0
    ;;
  --auto-init)
    (($# < 2)) && printf "\e[0;31;1m--auto-init : Argument Missing\e[0m\n" >&2 && exit 1
    a_shell_type+=("$2")
    shift 2
    ;;
  *)
    printf "\e[0;31;1mInvalid Argument %s\e[0m\n" "$1" >&2
    exit 1
    ;;
  esac
done

#--f_check_permission------------------------
for shell_type in "${a_shell_type[@]}"; do
  case "${shell_type,,}" in
  bash)
    f_check_permission "${v_bash_file}" "bash"
    ;;
  zsh)
    f_check_permission "${v_zsh_file}" "zsh"
    ;;
  fish)
    f_check_permission "${v_fish_file}" "fish"
    ;;
  *)
    printf "\e[0;31;1mInvalid Argument %s, --auto-init only supports <bash|zsh|fish>\e[0m\n" "${shell_type}" >&2
    v_error_occurred=true
    ;;
  esac
done

[[ "${v_error_occurred}" == true ]] && exit 1

#--auto-init__________________________________
for shell_type in "${a_shell_type[@]}"; do
  case "${shell_type,,}" in
  bash)
    f_init_bash
    ;;
  zsh)
    f_init_zsh
    ;;
  fish)
    f_init_fish
    ;;
  *)
    printf "\e[0;31;1mUnexpected Error\e[0m\n" >&2
    exit 1
    ;;
  esac
done

printf "\e[0;32;1mCompleted successfully\e[0m\n"
