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

# 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 "Invalid use of copyifneeded()"
    return 1
  fi
  
  TARGETDIR="$1"
  CONFIGNAME="$2"
  SOURCEDIR="$3"
  if [ "$#" == 4 ]; then
    CONFIGTEMPLATE="$4"
  else
    CONFIGTEMPLATE="$2"
  fi
  
  echo -n "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}".template
    copyifneeded "${APPHOSTCONFIG_TARGETDIR}" \
                 "${APPHOSTCONFIG_NAME}" \
                 "${APPHOSTCONFIG_TEMPDIR}"
    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 "Checking ${PERSISTENT_LOG_DIR} ... "
    if [ -d "${PERSISTENT_LOG_DIR}" ]; then
      echo "found."
    else
      echo "not found."
      echo -n "Creating ${PERSISTENT_LOG_DIR} ... "
      if mkdir -p "${PERSISTENT_LOG_DIR}"; then
        echo "done."
      else
        echo "failed."
      fi
    fi
    echo -n "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 -n "Checking access rights for project files..."
    if [ -f /etc/avahi/services/touch-remote.service ]; then
      echo -n "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 -n "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 -n "Empty ${OPTTMP} directory..."
    rm -rf ${OPTTMP}
    mkdir -p ${OPTTMP}

	echo -n "Create dbus directory..."
	mkdir -p /var/lib/dbus

	mkdir -p ${OBJECTMODEL_DIR}

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

exit 0