#!/bin/sh

set -eu

puavo_conf_json_dir='/state/etc/puavo/local'
puavo_conf_json_path="${puavo_conf_json_dir}/puavo_conf.json"

read_old_config() {
  if [ -s "$puavo_conf_json_path" ]; then
    if old_config=$(jq -r '
                      if (.|type) != "object" then error("not an object")
                      else . end
                    ' "$puavo_conf_json_path" 2>/dev/null); then
      printf %s "$old_config"
      return 0
    else
      # parsing of old config failed, move it aside but keep a backup
      mv "$puavo_conf_json_path" "${puavo_conf_json_path}.$(date +%s)"
    fi
  fi

  echo '{}'
}

# this is to allow checking permissions to run this tool (through sudo)
if [ $# -eq 1 ]; then
  if [ "$1" = '--check' ]; then
    exit 0
  fi
fi

if [ $# -ne 2 ]; then
  cat <<EOF >&2
usage: $(basename $0) key value         set a key to a value
       $(basename $0) -u key            unset a local value for key
EOF
  exit 1
fi

do_unset=false
if [ "$1" = '-u' ]; then
  do_unset=true
  key=$2
else
  key=$1
  value=$2
fi

case "$key" in
  puavo.abitti2.version \
  | puavo.grub.boot_default \
  | puavo.grub.developer_mode.enabled \
  | puavo.grub.windows.enabled \
  | puavo.pkg.* \
  | puavo.xsessions.user_registration.enabled)
    ;;
  *)
    if $do_unset; then
      echo "no permission to unset puavo-conf '$key'" >&2
    else
      echo "no permission to change puavo-conf '$key' --> '$value'" >&2
    fi
    exit 1
    ;;
esac

install -d -o root -g root -m 755 "$puavo_conf_json_dir"

test -e "$puavo_conf_json_path" || touch "$puavo_conf_json_path"
exec 3< "$puavo_conf_json_path"
if ! flock -w 10 -x 3; then
  echo "could not get a lock on $puavo_conf_json_path" >&2
  exit 1
fi

if $do_unset; then
  read_old_config | jq --arg key "$key" '.[$key] = null' \
    > "${puavo_conf_json_path}.tmp"
  mv "${puavo_conf_json_path}.tmp" \
     "$puavo_conf_json_path"
  # We have to run puavo-conf-update so that the default value
  # for $key gets set.
  puavo-conf-update > /dev/null 2>&1
  exit 0
fi

puavo-conf "$key" "$value"

read_old_config \
  | jq --arg key "$key" --arg value "$value" '.[$key] = $value' \
  > "${puavo_conf_json_path}.tmp"
mv "${puavo_conf_json_path}.tmp" \
   "$puavo_conf_json_path"
