#!/bin/sh
. /etc/profile
. /opt/gira/share/devicestack/ipmodule-vars
. /opt/gira/bin/environment

me="[$(printf $0 | xargs basename)]"

# Set variables
APPHOSTCONFIG_NAME="iscapphostconfig.xml"
APPHOSTCONFIG_TEMPDIR="/opt/gira/etc/iscapphost"
APPHOSTCONFIG_TARGETDIR="/opt/userdata/etc/iscapphost"
KNXSTACKCONFIG_NAME="knxstack.conf"
KNXSTACKCONFIG_TEMPDIR="/opt/gira/etc/knxstack"
KNXSTACKCONFIG_TARGETDIR="/opt/userdata/etc/knxstack"
APP_DIR="/opt/userdata/isc"
APPCONFIG_DIR="/opt/userdata/iscAppConfigRoot/1"
OPTTMP="/opt/userdata/tmp"
OBJECTMODEL_DIR="/opt/userdata/devicestack/iscOM"

copyifneeded()
{
  # sanity check
  if [ "$#" != "3" -a "$#" != "4" ]; then
    echo -n "${me} Invalid use of copyifneeded()"
    return 1
  fi

  TARGETDIR="$1"
  CONFIGNAME="$2"
  SOURCEDIR="$3"
  if [ "$#" == 4 ]; then
    CONFIGTEMPLATE="$4"
  else
    CONFIGTEMPLATE="$2"
  fi

  echo -n "${me} Checking ${TARGETDIR}/${CONFIGNAME} ... "
  if [ -f "${TARGETDIR}/${CONFIGNAME}" ]; then
    echo "found."
  else
    echo "not found."
    echo -n "Copying ${CONFIGNAME} ... "
    mkdir -p "${TARGETDIR}"
    if cp "${SOURCEDIR}/${CONFIGTEMPLATE}" "${TARGETDIR}/${CONFIGNAME}"; then
      echo "done."
    else
      echo "failed."
    fi
  fi
}

case "$1" in
  start)
    copyifneeded "${KNXSTACKCONFIG_TARGETDIR}" \
                 "${KNXSTACKCONFIG_NAME}" \
                 "${KNXSTACKCONFIG_TEMPDIR}" \
                 "${KNXSTACKCONFIG_NAME}"
    copyifneeded "${APPHOSTCONFIG_TARGETDIR}" \
                 "${APPHOSTCONFIG_NAME}" \
                 "${APPHOSTCONFIG_TEMPDIR}"
    
    # check for correct iscOM path in iscapphostconfig.xml
    if [ "$(grep $OBJECTMODEL_DIR $APPHOSTCONFIG_TARGETDIR/$APPHOSTCONFIG_NAME)" == "" ]; then
      echo "${me} Replacing old $APPHOSTCONFIG_NAME..."
      cp -f $APPHOSTCONFIG_TEMPDIR/$APPHOSTCONFIG_NAME $APPHOSTCONFIG_TARGETDIR/$APPHOSTCONFIG_NAME
    fi

    echo -n "Checking ${APP_DIR} ... "
    if [ -d "${APP_DIR}" ]; then
      echo "found."
    else
      echo "not found."
      echo -n "Creating ${APP_DIR} ... "
      if mkdir -p "${APP_DIR}"; then
        echo "done."
      else
        echo "failed."
      fi
    fi
    echo -n "${me} Checking ${PERSISTENT_LOG_DIR} ... "
    if [ -d "${PERSISTENT_LOG_DIR}" ]; then
      echo "found."
    else
      echo "not found."
      echo -n "${me} Creating ${PERSISTENT_LOG_DIR} ... "
      if mkdir -p "${PERSISTENT_LOG_DIR}"; then
        echo "done."
      else
        echo "failed."
      fi
    fi
    echo -n "${me} Checking ${APPCONFIG_DIR} ... "
    if [ -d "${APPCONFIG_DIR}" ]; then
      echo "found."
    else
      echo "not found."
      echo -n "Creating ${APPCONFIG_DIR} ... "
      if mkdir -p "${APPCONFIG_DIR}"; then
        echo "done."
      else
        echo "failed."
      fi
    fi
    echo "${me} Checking access rights for project files"
    if [ -f /etc/avahi/services/touch-remote.service ]; then
      echo -n "${me} Setting owner for /etc/avahi/services/touch-remote.service..."
      mount -o remount,rw /
      if chown 1000:root /etc/avahi/services/touch-remote.service; then
        echo "done."
      else
        echo "failed."
      fi
      sync
      mount -o remount,ro /
    fi
    
    echo "${me} Checking links for samba configuration"
    # Samba needs write access, so we use the files in /opt/userdata/etc/samba.
    # Create empty files if neccessary
    if [ ! -d "/opt/userdata/etc/samba" ]; then
      mkdir -p /opt/userdata/etc/samba
    fi
    # check if we have tdb files, else create empty ones
    if [ ! -f /opt/userdata/etc/samba/passdb.tdb ]; then
      touch /opt/userdata/etc/samba/passdb.tdb
    fi
    if [ ! -f /opt/userdata/etc/samba/secrets.tdb ]; then
      touch /opt/userdata/etc/samba/secrets.tdb
    fi
    # start the kim daemon
    /opt/gira/bin/kimdaemon.sh &

    echo "${me} Empty ${OPTTMP} directory"
    rm -rf ${OPTTMP}
    mkdir -p ${OPTTMP}

    if [ ! -h "/var/lib/dbus" ]
    then
      echo -n "Create dbus directory..."
      mkdir -p /var/lib/dbus
    fi

    mkdir -p ${OBJECTMODEL_DIR}

    ;;
  stop)
    ;;
  *)
    echo "Usage: $0 (start|stop)"
    exit 1
esac

exit 0
