main
1#!/usr/bin/env bash
2# Script to update the ntfy configuration from client.yml.in
3# Based on aichat-update-config pattern
4
5export PATH=$PATH:/run/current-system/sw/bin
6export PASSAGE_DIR=/home/vincent/.local/share/passage
7export PASSAGE_IDENTITIES_FILE=/home/vincent/.local/share/passage/identities
8
9# Define the input and output file names
10SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
11INPUT_FILE="${SCRIPT_DIR}/client.yml.in"
12OUTPUT_FILE="${HOME}/.config/ntfy/client.yml"
13
14# Check if the input file exists
15if [ ! -f "$INPUT_FILE" ]; then
16 echo "Error: Input file '$INPUT_FILE' not found."
17 exit 1
18fi
19
20# Create the output directory if it doesn't exist
21mkdir -p "$(dirname "$OUTPUT_FILE")"
22
23# Create an empty output file
24: >"$OUTPUT_FILE"
25
26# Read the input file line by line
27while IFS= read -r line; do
28 # Regex to find "passage::" followed by characters that are NOT a double quote or whitespace,
29 # until a double quote or whitespace is encountered.
30 if [[ "$line" =~ passage::([^\"]+) ]]; then
31 # The full string that needs to be replaced in the line (including "passage::")
32 passage_full_string="passage::${BASH_REMATCH[1]}"
33
34 # The string to pass to the 'passage show' command (without "passage::" or quotes)
35 passage_string_for_command=$(echo "${BASH_REMATCH[1]%\"}" | xargs)
36
37 echo "Found passage key in line: $passage_full_string"
38 echo "Executing command: passage show \"$passage_string_for_command\""
39
40 # Execute the passage command and capture its output
41 if passage_output=$(passage show "$passage_string_for_command" 2>/dev/null | tr -d '\n\r' | xargs) && [ -n "$passage_output" ]; then
42 # Replace the passage placeholder with the actual value
43 modified_line="${line//"$passage_full_string"/"$passage_output"}"
44
45 echo "Modified line: $modified_line"
46 echo "$modified_line" >>"$OUTPUT_FILE"
47 else
48 echo "Warning: 'passage show $passage_string_for_command' failed or returned empty. Keeping original line."
49 echo "$line" >>"$OUTPUT_FILE"
50 fi
51 else
52 # If no "passage::" found, write the original line
53 echo "$line" >>"$OUTPUT_FILE"
54 fi
55done <"$INPUT_FILE"
56
57echo "Processing complete. ntfy client configuration written to '$OUTPUT_FILE'."