#!/bin/sh

me="[S12userdata]"

echo "${me} Enter."

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

case "$1" in
  start)
    echo -n "${me} Checking userdata fs ... "
    if [ ! -f ${UD_MOUNTED_FILE} ]
    then
      echo "done."
      echo -n "${me} Mounting userdata fs ... "
      $MOUNT -t ${USERDATA_FS_TYPE} -o rw,suid,sync ${UD_BLOCKDEV} ${MP_USERDATA}
      if [ ! "$(cat /proc/mounts | grep ${MP_USERDATA})" == "" ]
      then
        echo "done!"

        # The configuration reset can only happen if userdata is mounted, so we check here.
        CONFIGURATION_RESET_CONDITION=0
        echo "${me} Reading configuration reset condition from temp file \"${CONFIGURATION_RESET_FILE}\""
        if [ -f "${CONFIGURATION_RESET_FILE}" ]
        then
          CONFIGURATION_RESET_CONDITION=`cat ${CONFIGURATION_RESET_FILE} | tr -d '\040\011\012\015'`
          echo "${me} Condition: ${CONFIGURATION_RESET_CONDITION}"
        else
          echo "${me} Condition temp file \"${CONFIGURATION_RESET_FILE}\" not found!"
        fi
        if [ "enabled" = "${CONFIGURATION_RESET_CONDITION}" ]
        then
          echo "${me} Configuration reset override detected, executing configuration reset."
          ${CONFIGURATION_RESET}
        else
          echo "${me} No configuration reset override detected, skipping."
        fi
        # Either way, delete the temp file afterwards..
        echo "${me} Removing temp condition file..."
        rm -f "${CONFIGURATION_RESET_FILE}"

        echo "${me} Updating mount info."
        touch ${UD_MOUNTED_FILE}
        echo "${me} Creating FWU directory."
        mkdir -p ${UD_FWU_DIR}
        logger -t "${me} userdata.initd" -s "Creating directories..."
        mkdir -p /opt/userdata/devicestack
        mkdir -p /opt/userdata/knxstack

        # Check if a user named 'logic' exists on the system.
        # If so, it will run the LogicEngine and have restricted access to files.
        # The $LOGIC_DIR needs to be writable though. This will be ensured here.
        LOGIC_DIR="/opt/userdata/logicengine"
        LOGIC_USER="logic"
        if id ${LOGIC_USER} >/dev/null 2>&1
        then
          # If so, make sure the 'logicengine' directory belongs to 'logic',
          # so the LogicEngine can write nodes and its configuration.
          mkdir -p ${LOGIC_DIR}
          # Check if directory does not yet belong to the logic user
          if [ "$(ls -ld ${LOGIC_DIR} | awk '{print $3}')" != "${LOGIC_USER}" ]
          then
            echo "${me} Setting ownership of ${LOGIC_DIR} to ${LOGIC_USER} ..."
            chown -R ${LOGIC_USER}:${LOGIC_USER} ${LOGIC_DIR}
          fi
        fi

      else
        echo "${me} Failed!"
        echo "${me} Executing factory reset."
        ${FACTORY_RESET}
        echo "${me} Rebooting NOW!"
        ${REBOOT}
      fi
    else
      echo "skipped (already mounted)."
    fi
    ;;
  stop)
    echo -n "${me} Checking userdata fs ... "
    if [ -f ${UD_MOUNTED_FILE} ]
    then
      echo "done."
      echo -n "${me} Unmounting userdata fs ..."
      sync
      $UMOUNT ${MP_USERDATA}
      if [ "$(cat /proc/mounts | grep ${MP_USERDATA})" == "" ]
      then
        echo "done."
      else
        echo "failed."
      fi
    else
      echo "skipped (not mounted)."
    fi
    ;;
  *)
    echo "${me} Usage: $0 (start|stop)"
    exit 1
esac

echo "${me} Exit."

exit 0
