#!/bin/sh
me="[$(printf $0 | xargs basename)]"

source /opt/gira/share/devicestack/ipmodule-vars

CONFIGURATION_RESET_FILE="/var/tmp/configuration_reset_condition"
CHK_FACTRSTCOND_TOOL="/opt/gira/bin/chkfactrst"
FACTORY_RESET="/opt/gira/bin/factory-reset"

CONDITION_RESULT=0

case "$1" in
  start)
    printf "${me} Checking factory reset condition ... \\n"
    if [ -x "${CHK_FACTRSTCOND_TOOL}" ]; then
      ${CHK_FACTRSTCOND_TOOL} 2>&1 > /var/log/check-factory-reset.log
      CONDITION_RESULT=$?
    else
      printf "${me} tool not available"
    fi
    if [ ${CONDITION_RESULT} -eq 1 ]; then
      printf "${me} Button press sequence detected, executing factory reset.\\n"
      ${FACTORY_RESET}
    elif [ ${CONDITION_RESULT} -eq 2 ]; then
      printf "${me} Factory reset override detected, executing factory reset.\\n"
      ${FACTORY_RESET}
    elif [ ${CONDITION_RESULT} -eq 3 ]; then
      # The configuration-reset script requires a mounted userdata
      printf "${me} Configuration reset override detected, will reset configuration\\n"
      printf "${me} when the userdata is mounted.\\n"
      printf "enabled\\n" > ${CONFIGURATION_RESET_FILE}
    else
      printf "${me} No request detected.\\n"
    fi
    ;;
  stop)
    ;;
  *)
    printf "Usage: %s (start|stop)\\n" "$0"
    exit 1
esac

exit 0
