aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/property_utils.sh
blob: 3644fa992fa61877ada1bce54e0cf3a757a70b62 (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
50
51
52
53
54
55
56
57
#!/bin/bash
###############################
# exposes library functions to modify properties in a property 
#
# TODO function append_property
#
###############################

set -euo pipefail

function cnt_lines() {
  FILE=$1
  cat $FILE | wc -l
}

function write_prop() {
  PROPERTY=$1
  VALUE=$2
  FILE=$3

  # uncomment below to help debug calls to set_property
  #echo "setting property $PROPERTY to value $VALUE in $FILE"
  
  echo "" >> $FILE
  echo "${PROPERTY}=${VALUE}" >> $FILE 
}

function set_property() {
  PROPERTY=$1
  VALUE=$2
  FILE=$3

  REGEXP="${PROPERTY//\./\\.}\\s*="

  if grep -q "$REGEXP" "$FILE"; then
     # delete line of specified property
    LINE_COUNT=$(cnt_lines $FILE)

    if [[ "$OSTYPE" == "darwin"* ]]; then
      sed -i '' /${REGEXP}/d "$FILE"
    else
      sed -i /${REGEXP}/d "$FILE"
    fi

    # add property if at least one line deleted
    NEW_LINE_COUNT=$(cnt_lines $FILE)

    if [[ $LINE_COUNT -gt $NEW_LINE_COUNT ]]; then
      write_prop $PROPERTY $VALUE $FILE
    fi

  else
    write_prop $PROPERTY $VALUE $FILE
  fi
}