summaryrefslogtreecommitdiff
path: root/inimodify.sh
blob: 939e47a46e947a745f8efff910748efe4ebdfb8e (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
#!/bin/bash
# Dynamically modifies openssl config file
# ./inimodify.sh [header] [variable] [value] < openssl.cnf
# ./inimodify.sh "usr_cert" "extendedKeyUsage" "clientAuth, serverAuth" < openssl.cnf
#requires bash 4.0 regexes

set -x
set -e
[ -n "$1" ] && HEADER="$1"   || exit 2
[ -n "$2" ] && VARIABLE="$2" || exit 3
[ -n "$3" ] && VALUE="$3"    || exit 4

IFS=$'\n'
HEADER_STANZA=0
VARIABLE_MODIFIED=0
while read line; do
  if [[ "$line" =~ ^\ *\[\ *"$HEADER"\ *\] ]]; then
    HEADER_STANZA=1
    echo "$line"
  elif [ "$HEADER_STANZA" == 1 ] && [[ "$line" =~ ^\ *"$VARIABLE"\ *= ]]; then
    echo -n "$line"
    echo ", $VALUE"
    VARIABLE_MODIFIED=1
  elif [ "$HEADER_STANZA" == 1 ] && [[ "$line" =~ ^\ *\[\ .+\ \] ]]; then
    if [ "$VARIABLE_MODIFIED" == 0 ]; then
      echo "$VARIABLE = $VALUE"
      echo
      VARIABLE_MODIFIED=1
    fi
    HEADER_STANZA=0
    echo "$line"
  else
    echo "$line"
  fi

done