#!/bin/sh

# Load ipmodule variables
. /opt/gira/share/devicestack/ipmodule-vars

CONDITION_RESULT=0

case "$1" in
  start)
    printf "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 "(tool not available)"
    fi
    if [ ${CONDITION_RESULT} -eq 1 ]
    then
      printf "Button press sequence detected, executing factory reset.\\n"
      ${FACTORY_RESET}
    elif [ ${CONDITION_RESULT} -eq 2 ]
    then
      printf "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 "Configuration reset override detected, will reset configuration\\n"
      printf "when the userdata is mounted.\\n"
      printf "enabled\\n" > ${CONFIGURATION_RESET_FILE}
    else
      printf "No request detected.\\n"
    fi
    ;;
  stop)
    ;;
  *)
    printf "Usage: %s (start|stop)\\n" "$0"
    exit 1
esac

exit 0
