#!/bin/sh
source /etc/preinit
script_init

hakchi(){
  [ "$1" == "hakchi" ] && shift
  case "$1" in
    set|unset)
      if echo "$2" | grep -qEe '^cfg_'; then
        [ "$1" == "set" ] && eval "$2="\${3}"" || unset "$2"
        return 0
      else
        echo "only cfg_ variables are supported with hakchi $1"
      fi
      return 1
      ;;
    get)
      eval "echo "\$$2""
      return 0
      ;;
    eval)
      if [ -z "$2" ]; then
        source /dev/stdin
      else
        eval "$2"
      fi
      return $?
      ;;
    source)
      source "$2"
      return $?
      ;;
    *)
      if type "$1" | grep -q '^.* is a shell function$' 2>/dev/null; then
        ${1+"$@"}
      else
        echo "no such function: $@"
        return 1
      fi
      ;;
  esac
}

hakchi "$(basename "$0")" ${1+"$@"}
exit_code=$?
save_config
exit $exit_code
