summaryrefslogtreecommitdiff
path: root/editpost.sh
blob: 2c0213b43f57d91f3cd7a78577bf34d02e667f1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/bash


EDITOR_COMMAND="${EDITOR:-nano}"
if [[ -z "$1" ]]; then
  echo "Usage: $0 <pattern>"
  exit 1
fi

update_last_modified_at() {
  local file="$1"
  local current_date="$(date +"%Y-%m-%d %H:%M %z")"
  if grep -q '^last_modified_at:' "$file"; then
    sed -i "s|^last_modified_at:.*|last_modified_at: $current_date|" "$file"
  else
    sed -i "/^date:/a last_modified_at: $current_date" "$file"
  fi
}

pattern="_posts/$1"
files=( $pattern )

if [[ (${#files[@]} -eq 1 && ! -e "${files[0]}") || ${#files[@]} -eq 0 ]]; then
  echo "No files found matching pattern: $1"
  exit 1
fi

if [[ ${#files[@]} -eq 1 ]]; then
  update_last_modified_at "${files[0]}"
  "$EDITOR_COMMAND" "${files[0]}"
  exit 0
fi

echo "Multiple files found:"
i=0
for f in "${files[@]}"; do
  echo "$i) $f"
  ((i++))
done

read -p "Select a file number: " choice

if [[ "$choice" -ge 0 && "$choice" -lt ${#files[@]} ]]; then
  update_last_modified_at "${files[choice]}"
  "$EDITOR_COMMAND" "${files[$choice]}"
else
  echo "Invalid selection."
  exit 1
fi