#!/usr/bin/env bash
# Author: OpenAI Codex
# AI-assisted implementation, generated and reviewed with OpenAI Codex.

set -euo pipefail
# Keep glob expansion and sorting stable across locales, so -n remains
# repeatable when the service and filesystem contents have not changed.
scf_lc_all_was_set=false
scf_original_lc_all=""
if [[ -n ${LC_ALL+x} ]]; then
  scf_lc_all_was_set=true
  scf_original_lc_all=$LC_ALL
fi
export LC_ALL=C

restore_locale() {
  if [[ $scf_lc_all_was_set == true ]]; then
    export LC_ALL=$scf_original_lc_all
  else
    unset LC_ALL
  fi
}

usage() {
  cat >&2 <<'EOF'
Usage: scf [-r] [-n NUM] SERVICE

Open a readable text file referenced by a systemd service's ExecStart.
SERVICE may be given with or without the .service suffix.
List file arguments, text entry points, file references, then directory entries.
File names and extensions do not determine eligibility.

  -r  recursively inspect referenced files and directories (maximum reference depth: 3)
  -n  open candidate NUM directly, without displaying the selection list

Environment:
  SCF_MAX_CANDIDATES  maximum number of displayed candidates (default: 20)
  SCF_MAX_SCAN        maximum number of filesystem paths inspected (default: 256)
  SCF_MAX_BYTES       total bytes parsed for recursive references (default: 1048576)
  SCF_MAX_DEPTH       maximum reference depth (default: 3)
EOF
  exit 2
}

recursive=false
selection_number=""
service=""
while (( $# > 0 )); do
  argument=$1
  shift

  if [[ $argument == -- ]]; then
    [[ -z $service && $# -eq 1 ]] || usage
    service=$1
    shift
    break
  fi

  if [[ $argument == -* ]]; then
    options=${argument#-}
    [[ -n $options ]] || usage
    while [[ -n $options ]]; do
      option=${options:0:1}
      options=${options:1}
      case $option in
        r) recursive=true ;;
        n)
          if [[ -n $options ]]; then
            selection_number=$options
            options=""
          else
            (( $# > 0 )) || usage
            selection_number=$1
            shift
          fi
          ;;
        *) usage ;;
      esac
    done
  else
    [[ -z $service ]] || usage
    service=$argument
  fi
done

if [[ -z $service ]]; then
  usage
fi

interactive=false
if [[ -t 0 && -t 1 && -t 2 ]]; then
  interactive=true
fi

# Prompts and candidate details are written to stderr.  Keep pipelines and
# redirected output free of escape sequences.
if [[ $interactive == true ]]; then
  color_item=$'\033[1;36m'
  color_detail=$'\033[2;37m'
  color_prompt=$'\033[1;33m'
  color_reset=$'\033[0m'
else
  color_item=""
  color_detail=""
  color_prompt=""
  color_reset=""
fi

if [[ -n $selection_number && ! $selection_number =~ ^[0-9]+$ ]]; then
  echo "scf: invalid candidate number: $selection_number" >&2
  exit 2
fi
if [[ -n $selection_number ]]; then
  # Bash arithmetic treats numbers with a leading zero as octal.
  selection_number=$((10#$selection_number))
  if (( selection_number < 1 )); then
    echo "scf: invalid candidate number: 0" >&2
    exit 2
  fi
fi

service_argument=$service
if [[ $service != *.service ]]; then
  service+=.service
fi

# systemd uses \xHH escapes in unit names derived from paths.
checked_service=${service//\\x[0-9A-Fa-f][0-9A-Fa-f]/_}
if [[ ! $checked_service =~ ^[A-Za-z0-9_.@:%+-]+\.service$ ]]; then
  echo "scf: invalid service name: $service_argument" >&2
  exit 2
fi

if ! exec_start=$(systemctl show --no-pager --property=ExecStart --value -- "$service"); then
  echo "scf: cannot inspect $service" >&2
  exit 1
fi

if [[ -z $exec_start || $exec_start == "{}" ]]; then
  echo "scf: $service has no ExecStart" >&2
  exit 1
fi

# Keep recursion bounded even when configuration files refer to each other.
max_candidates=${SCF_MAX_CANDIDATES:-20}
max_scan=${SCF_MAX_SCAN:-256}
max_bytes=${SCF_MAX_BYTES:-1048576}
max_reference_depth=${SCF_MAX_DEPTH:-3}
if [[ ! $max_candidates =~ ^[0-9]+$ ]]; then
  echo "scf: SCF_MAX_CANDIDATES must be a positive integer" >&2
  exit 2
fi
if [[ ! $max_reference_depth =~ ^[0-9]+$ ]]; then
  echo "scf: SCF_MAX_DEPTH must be a non-negative integer" >&2
  exit 2
fi
if [[ ! $max_scan =~ ^[0-9]+$ ]]; then
  echo "scf: SCF_MAX_SCAN must be a positive integer" >&2
  exit 2
fi
if [[ ! $max_bytes =~ ^[0-9]+$ ]]; then
  echo "scf: SCF_MAX_BYTES must be a positive integer" >&2
  exit 2
fi
# Normalize user-provided decimal values before using Bash arithmetic.
max_candidates=$((10#$max_candidates))
max_scan=$((10#$max_scan))
max_bytes=$((10#$max_bytes))
max_reference_depth=$((10#$max_reference_depth))
if (( max_candidates < 1 )); then
  echo "scf: SCF_MAX_CANDIDATES must be a positive integer" >&2
  exit 2
fi
if (( max_scan < 1 )); then
  echo "scf: SCF_MAX_SCAN must be a positive integer" >&2
  exit 2
fi
if (( max_bytes < 1 )); then
  echo "scf: SCF_MAX_BYTES must be a positive integer" >&2
  exit 2
fi
declare -A resolved_paths=()
declare -A path_kinds=()
declare -A path_sizes=()
declare -A reference_cache=()
declare -A candidate_path=()
declare -A candidate_rank=()
declare -A candidate_depth=()
declare -A traversal_depth=()
declare -A candidate_source=()
declare -A candidate_source_detail=()
candidate_ids=()
recursive_paths=()
declare -A processed_depth=()
declare -A processed_rank=()
declare -A expanded_directories=()
directory_paths=()
directory_depths=()
directory_sources=()
entry_paths=()
entry_depths=()
entry_sources=()
entry_directories=()
scan_count=0
scan_limited=false
bytes_remaining=$max_bytes
bytes_limited=false

path_pattern="\"/([^\"\\\\]|\\\\.)*\"|'/([^'\\\\]|\\\\.)*'|/([^[:space:];,'\"})\\\\]|\\\\.)+"

decode_path() {
  local path=$1
  local decoded=""
  local escape character digits

  while [[ $path == *\\* ]]; do
    decoded+=${path%%\\*}
    path=${path#*\\}
    escape=${path:0:1}
    path=${path:1}
    case $escape in
      x)
        digits=${path:0:2}
        if [[ $digits =~ ^[0-9a-fA-F]{2}$ ]]; then
          printf -v character '%b' "\\x$digits"
          decoded+=$character
          path=${path:2}
        else
          decoded+='\x'
        fi
        ;;
      [0-7])
        digits=$escape${path:0:2}
        if [[ $digits =~ ^[0-7]{3}$ ]]; then
          printf -v character '%b' "\\0$digits"
          decoded+=$character
          path=${path:2}
        else
          decoded+="\\$escape"
        fi
        ;;
      s|' ') decoded+=' ' ;;
      /|\"|\') decoded+=$escape ;;
      a|b|e|f|n|r|t|v|\\)
        printf -v character '%b' "\\$escape"
        decoded+=$character
        ;;
      *) decoded+="\\$escape" ;;
    esac
  done
  printf '%s\n' "$decoded$path"
}

extract_paths() {
  local path
  while IFS= read -r path; do
    if [[ $path == \"*\" || $path == \'*\' ]]; then
      path=${path:1:-1}
    fi
    decode_path "$path"
  done < <(grep -a -oE "$path_pattern" || true)
}

# Preserve the context of each path without interpreting shell or Python code.
# Search-path assignments suppress directory expansion, while file references
# on those same lines remain candidates.
extract_references() {
  local context path
  while IFS=$'\t' read -r context path; do
    if [[ $path == \"*\" || $path == \'*\' ]]; then
      path=${path:1:-1}
    fi
    printf '%s\t' "$context"
    decode_path "$path"
  done < <(
    SCF_PATH_PATTERN="$path_pattern" awk '
      BEGIN {
        pattern = ENVIRON["SCF_PATH_PATTERN"]
        # These variables describe runtime search lists, rather than file types.
        variables = "(PATH|PYTHONPATH|LD_LIBRARY_PATH|LIBRARY_PATH|MANPATH|INFOPATH|PERL5LIB|RUBYLIB|CLASSPATH|NODE_PATH|PKG_CONFIG_PATH|GI_TYPELIB_PATH|XDG_DATA_DIRS)"
        assignment = "(^|[;[:space:]])(export[[:space:]]+)?" variables "[[:space:]]*\\+?="
        python = "(sys\\.path(\\[[^]]*\\])?[[:space:]]*\\+?=|sys\\.path\\.(insert|append|extend)[[:space:]]*\\(|site\\.addsitedir[[:space:]]*\\()"
      }
      function emit(fragment, context, path, key, previous) {
        while (match(fragment, pattern)) {
          previous = RSTART > 1 ? substr(fragment, RSTART-1, 1) : ""
          path = substr(fragment, RSTART, RLENGTH)
          fragment = substr(fragment, RSTART + RLENGTH)
          # A slash inside a URL, version, or variable expansion is not an
          # absolute path token. Quoted absolute paths keep their full value.
          if (substr(path, 1, 1) == "/" && previous != "" &&
              previous !~ /[[:space:]]/ && !index("=<>({[,", previous)) continue
          key = context SUBSEP path
          if (!seen[key]++) printf "%s\t%s\n", context, path
        }
      }
      function uncomment(line, i, character, quoted, escaped) {
        quoted = quote
        for (i = 1; i <= length(line); i++) {
          character = substr(line, i, 1)
          if (escaped) { escaped = 0; continue }
          if (character == "\\" && quoted != "\047") { escaped = 1; continue }
          if (quoted != "") {
            if (character == quoted) quoted = ""
          } else if (character == "\"" || character == "\047") {
            quoted = character
          } else if (character == "#" && (i == 1 || substr(line, i-1, 1) ~ /[[:space:];]/)) {
            return substr(line, 1, i-1)
          }
        }
        return line
      }
      # End a shell value at its unquoted token boundary. A Python search-path
      # call/list may span lines, but statements after it keep their own context.
      function consume_search(line, start, i, character, escaped) {
        for (i = start; i <= length(line); i++) {
          character = substr(line, i, 1)
          if (escaped) { escaped = 0; continue }
          if (character == "\\" && quote != "\047") { escaped = 1; continue }
          if (quote != "") {
            if (character == quote) quote = ""
          } else if (character == "\"" || character == "\047") {
            quote = character
          } else if (mode == "shell" && character ~ /[[:space:];]/) {
            mode = ""
            return i
          } else if (mode == "python") {
            if (character == "[" || character == "(") balance++
            if ((character == "]" || character == ")") && balance > 0) {
              balance--
              if (method && balance == 0) {
                mode = ""
                return i+1
              }
            }
            if (character == ";" && balance == 0) {
              mode = ""
              return i
            }
          }
        }
        if (quote == "" && balance == 0 && !escaped) mode = ""
        return i
      }
      {
        line = uncomment($0)
        if (mode == "" && line ~ /^[[:space:]]*(;|\/\/)/) next
        cursor = 1
        while (cursor <= length(line) || mode != "") {
          if (mode != "") {
            stop = consume_search(line, cursor)
            emit(substr(line, cursor, stop-cursor), "search")
            cursor = stop
            if (cursor > length(line)) break
            continue
          }
          rest = substr(line, cursor)
          shell_start = 0
          python_start = 0
          if (match(rest, assignment)) {
            shell_start = RSTART
            shell_length = RLENGTH
          }
          if (match(rest, python)) {
            python_start = RSTART
            python_length = RLENGTH
          }
          if (!shell_start && !python_start) {
            emit(rest, "file")
            break
          }
          if (shell_start && (!python_start || shell_start < python_start)) {
            start = shell_start
            header_length = shell_length
            mode = "shell"
            method = 0
          } else {
            start = python_start
            header_length = python_length
            mode = "python"
            method = substr(rest, start+header_length-1, 1) == "("
          }
          emit(substr(rest, 1, start-1), "file")
          quote = ""
          balance = method ? 1 : 0
          cursor += start+header_length-1
        }
      }
    '
  )
}

# Cache both lexical resolution and physical attributes. Callers use the
# globals canonical_path and path_kind so cache updates stay in this shell.
inspect_path() {
  local path=$1 controls size
  [[ $path == /* ]] || return 1
  canonical_path=""
  path_kind=""
  if [[ -n ${resolved_paths[$path]+cached} ]]; then
    canonical_path=${resolved_paths[$path]}
    [[ -n $canonical_path ]] || return 1
  else
    resolved_paths[$path]=""
    case $path in
      /dev|/dev/*|/proc|/proc/*|/sys|/sys/*) return 1 ;;
    esac
    canonical_path=$(readlink -f -- "$path") || return 1
    case $canonical_path in
      /dev|/dev/*|/proc|/proc/*|/sys|/sys/*) return 1 ;;
    esac
    resolved_paths[$path]=$canonical_path
  fi

  if [[ -n ${path_kinds[$canonical_path]+cached} ]]; then
    path_kind=${path_kinds[$canonical_path]}
    [[ $path_kind != skip ]]
    return
  fi
  if (( scan_count >= max_scan )); then
    scan_limited=true
    return 1
  fi
  scan_count=$((scan_count + 1))
  path_kind=skip
  if [[ -d $canonical_path ]]; then
    path_kind=directory
  elif [[ -f $canonical_path && -r $canonical_path ]]; then
    # Inspect a bounded prefix. Allow whitespace, ANSI escapes and UTF-8 bytes,
    # but reject NUL and other binary controls even after an ASCII first line.
    if controls=$(head -c 65536 -- "$canonical_path" | tr -d '\11\12\14\15\33\40-\176\200-\377' | wc -c) &&
        (( controls == 0 )) && size=$(stat -c '%s' -- "$canonical_path"); then
      path_kind=text
      path_sizes[$canonical_path]=$size
      if head -c 2 -- "$canonical_path" | grep -qxF '#!'; then
        path_kind=script
      fi
    fi
  fi
  path_kinds[$canonical_path]=$path_kind
  [[ $path_kind != skip ]]
}

queue_directory() {
  directory_paths+=("$1")
  directory_depths+=("$2")
  directory_sources+=("$3")
}

add_candidate() {
  local path=$1 rank=$2 depth=$3 source=$4 detail=$5
  inspect_path "$path" || return 0
  [[ $path_kind != directory ]] || return 0
  local id=$canonical_path

  if [[ -z ${traversal_depth[$id]+seen} ]] || (( depth < traversal_depth[$id] )); then
    traversal_depth[$id]=$depth
    if [[ $recursive == true ]] && (( depth < max_reference_depth )); then
      recursive_paths+=("$id")
    fi
  fi
  if [[ $recursive == true && -n ${candidate_rank[$id]+seen} ]] &&
      (( rank < candidate_rank[$id] && traversal_depth[$id] < max_reference_depth )); then
    recursive_paths+=("$id")
  fi
  if [[ -z ${candidate_rank[$id]+seen} ]]; then
    candidate_ids+=("$id")
    candidate_path[$id]=$path
  elif (( rank > candidate_rank[$id] ||
      (rank == candidate_rank[$id] && depth >= candidate_depth[$id]) )); then
    return 0
  fi

  candidate_rank[$id]=$rank
  candidate_depth[$id]=$depth
  candidate_source[$id]=$source
  candidate_source_detail[$id]=$detail
}

consider_reference() {
  local path=$1 rank=$2 depth=$3 source=$4 detail=$5 context=${6:-file}
  # Defer directory probing until all known explicit files have been inspected.
  if [[ -d $path ]]; then
    if [[ $recursive == true && $context != search ]]; then
      queue_directory "$path" "$depth" "$source"
    fi
  else
    add_candidate "$path" "$rank" "$depth" "$source" "$detail"
  fi
}

expand_directory() {
  local path=$1 depth=$2 source=$3
  inspect_path "$path" || return 0
  [[ $path_kind == directory ]] || return 0
  local id=$canonical_path entry
  if [[ -n ${expanded_directories[$id]+seen} ]] &&
      (( expanded_directories[$id] <= depth )); then
    return 0
  fi
  expanded_directories[$id]=$depth

  shopt -s dotglob nullglob
  for entry in "$path"/*; do
    # Only expand one level. Referenced subdirectories get their own tasks.
    [[ -f $entry ]] || continue
    entry_paths+=("$entry")
    entry_depths+=("$depth")
    entry_sources+=("$source")
    entry_directories+=("$path")
  done
  shopt -u dotglob nullglob
}

# Keep argv[0] separate for each command, so a file used as an entry point
# in one ExecStart can still be a higher-ranked argument in another command.
extract_arguments() {
  awk '
    {
      rest = $0
      while (match(rest, /argv\[[0-9]*\]=/)) {
        field = substr(rest, RSTART, RLENGTH)
        rest = substr(rest, RSTART+RLENGTH)
        quote = ""
        escaped = 0
        for (stop = 1; stop <= length(rest); stop++) {
          character = substr(rest, stop, 1)
          if (escaped) { escaped = 0; continue }
          if (character == "\\") { escaped = 1; continue }
          if (quote != "") {
            if (character == quote) quote = ""
          } else if (character == "\"" || character == "\047") {
            quote = character
          # systemctl prints the shell command supplied to -c without adding
          # quotes. Its semicolons belong to argv until a property boundary.
          } else if (character == ";") {
            tail = substr(rest, stop)
            if (tail ~ /^;[[:space:]]*ignore_errors=(yes|no)[[:space:]]*;[[:space:]]*start_time=/ ||
                tail ~ /^;[[:space:]]*argv\[[0-9]*\]=/ ||
                tail ~ /^;[[:space:]]*}[[:space:]]*($|\{[[:space:]]*path=)/) break
          }
        }
        value = substr(rest, 1, stop-1)
        rest = substr(rest, stop+1)
        if (field == "argv[0]=") continue
        if (field == "argv[]=") {
          sub(/^[[:space:]]*/, "", value)
          quote = ""
          escaped = 0
          for (first = 1; first <= length(value); first++) {
            character = substr(value, first, 1)
            if (escaped) { escaped = 0; continue }
            if (character == "\\") { escaped = 1; continue }
            if (quote != "") {
              if (character == quote) quote = ""
            } else if (character == "\"" || character == "\047") {
              quote = character
            } else if (character ~ /[[:space:]]/) {
              break
            }
          }
          value = substr(value, first)
        }
        print value
      }
    }
  '
}

mapfile -t entry_points < <(
  printf '%s\n' "$exec_start" \
    | grep -oE '(^|\{[[:space:]]*)path=[^;]+' \
    | extract_paths | awk '!seen[$0]++'
)
mapfile -t initial_paths < <(
  printf '%s\n' "$exec_start" | extract_arguments | extract_paths | awk '!seen[$0]++'
)

# Process every explicit argument before entry points or directory contents.
for path in "${initial_paths[@]}"; do
  consider_reference "$path" 0 0 ExecStart argument
done
for path in "${entry_points[@]}"; do
  add_candidate "$path" 1 0 ExecStart "entry point"
done

file_index=0
directory_index=0
entry_index=0
while [[ $recursive == true ]]; do
  if (( file_index < ${#recursive_paths[@]} )); then
    id=${recursive_paths[file_index]}
    file_index=$((file_index + 1))
    depth=${traversal_depth[$id]}
    if [[ -n ${processed_depth[$id]+seen} ]] &&
        (( processed_depth[$id] <= depth && processed_rank[$id] <= candidate_rank[$id] )); then
      continue
    fi
    processed_depth[$id]=$depth
    processed_rank[$id]=${candidate_rank[$id]}
    next_depth=$((depth + 1))
    reference_rank=2
    # A directory neighbour does not gain stronger provenance merely by
    # mentioning another file. Keep that branch behind explicit references.
    if (( candidate_rank[$id] > reference_rank )); then reference_rank=${candidate_rank[$id]}; fi
    if [[ -z ${reference_cache[$id]+cached} ]]; then
      read_bytes=${path_sizes[$id]}
      if (( read_bytes > bytes_remaining )); then
        read_bytes=$bytes_remaining
        bytes_limited=true
        # A cut path on the last line must not become a different candidate.
        reference_cache[$id]=$(head -c "$read_bytes" -- "$id" | sed '$d' | extract_references)
      else
        reference_cache[$id]=$(head -c "$read_bytes" -- "$id" | extract_references)
      fi
      bytes_remaining=$((bytes_remaining - read_bytes))
    fi
    while IFS=$'\t' read -r context referenced_path; do
      [[ $context == file || $context == search ]] || continue
      consider_reference "$referenced_path" "$reference_rank" "$next_depth" "$id" "file reference" "$context"
      if [[ $scan_limited == true ]] && (( scan_count >= max_scan )); then break; fi
    done <<< "${reference_cache[$id]}"
  elif (( entry_index < ${#entry_paths[@]} )); then
    if (( scan_count >= max_scan )); then scan_limited=true; break; fi
    add_candidate "${entry_paths[entry_index]}" 3 "${entry_depths[entry_index]}" \
      "${entry_sources[entry_index]}" "directory ${entry_directories[entry_index]}"
    entry_index=$((entry_index + 1))
  elif (( directory_index < ${#directory_paths[@]} )); then
    if (( scan_count >= max_scan )); then scan_limited=true; break; fi
    expand_directory "${directory_paths[directory_index]}" \
      "${directory_depths[directory_index]}" "${directory_sources[directory_index]}"
    directory_index=$((directory_index + 1))
  else
    break
  fi
done

# Stable insertion by source rank and depth preserves discovery order for ties.
paths=()
for id in "${candidate_ids[@]}"; do
  position=${#paths[@]}
  while (( position > 0 )); do
    previous=${paths[position - 1]}
    if (( candidate_rank[$previous] < candidate_rank[$id] ||
        (candidate_rank[$previous] == candidate_rank[$id] &&
         candidate_depth[$previous] <= candidate_depth[$id]) )); then
      break
    fi
    paths[position]=${paths[position - 1]}
    position=$((position - 1))
  done
  paths[position]=$id
done

if [[ $scan_limited == true ]]; then
  printf 'scf: inspection truncated at SCF_MAX_SCAN=%s; increase it to inspect more references\n' "$max_scan" >&2
fi
if [[ $bytes_limited == true ]]; then
  printf 'scf: recursive parsing truncated at SCF_MAX_BYTES=%s; increase it to read more text\n' "$max_bytes" >&2
fi
if (( ${#paths[@]} > max_candidates )); then
  printf 'scf: showing %s of %s text files (SCF_MAX_CANDIDATES); increase it to show more\n' \
    "$max_candidates" "${#paths[@]}" >&2
  paths=("${paths[@]:0:max_candidates}")
fi

if (( ${#paths[@]} == 0 )); then
  echo "scf: no existing configuration or script path found in ExecStart of $service" >&2
  echo "ExecStart: $exec_start" >&2
  exit 1
fi

if [[ $interactive == false && -z $selection_number ]]; then
  selection_number=1
fi

if [[ -n $selection_number ]]; then
  if (( selection_number > ${#paths[@]} )); then
    echo "scf: candidate number $selection_number is out of range (1-${#paths[@]})" >&2
    exit 2
  fi
  selected_id=${paths[selection_number - 1]}
  config_path=${candidate_path[$selected_id]}
elif (( ${#paths[@]} > 1 )); then
  declare -A displayed_indices=()
  for i in "${!paths[@]}"; do
    displayed_indices["${paths[i]}"]=$((i + 1))
  done

  printf '%sscf: multiple referenced text files for %s:%s\n' \
    "$color_prompt" "$service" "$color_reset" >&2
  for i in "${!paths[@]}"; do
    id=${paths[i]}
    path=${candidate_path[$id]}
    source=${candidate_source[$id]}
    if [[ $source == ExecStart ]]; then
      source_label=ExecStart
    elif [[ -n ${displayed_indices[$source]+seen} ]]; then
      source_label="#${displayed_indices[$source]}"
    else
      source_label=$source
    fi
    source_detail=${candidate_source_detail[$id]}
    if [[ -n $source_detail ]]; then
      source_label+=" ($source_detail)"
    fi
    printf '%s  %d) %s%s\n' \
      "$color_item" "$((i + 1))" "$path" "$color_reset" >&2
    printf '%s     %s; depth: %d; derived from: %s%s\n' \
      "$color_detail" "${path_kinds[$id]}" "${candidate_depth[$id]}" "$source_label" "$color_reset" >&2
  done
  read -r -p "${color_prompt}Select a file [1]: ${color_reset}" selection
  selection=${selection:-1}
  if [[ ! $selection =~ ^[0-9]+$ ]]; then
    echo "scf: invalid selection" >&2
    exit 2
  fi
  selection=$((10#$selection))
  if (( selection < 1 || selection > ${#paths[@]} )); then
    echo "scf: invalid selection" >&2
    exit 2
  fi
  selected_id=${paths[selection - 1]}
  config_path=${candidate_path[$selected_id]}
else
  selected_id=${paths[0]}
  config_path=${candidate_path[$selected_id]}
fi

if [[ $interactive == false ]]; then
  restore_locale
  exec cat -- "$config_path"
fi

editor=${VISUAL:-${EDITOR:-vim}}
read -r -a editor_command <<< "$editor"
if (( ${#editor_command[@]} == 0 )); then
  editor_command=(vim)
fi
restore_locale
exec "${editor_command[@]}" "$config_path"
