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

# Set variables
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)
    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

    USE_AVAHI_SERVICE_FOR_TOUCH_REMOTE=no
    if [ "${USE_AVAHI_SERVICE_FOR_TOUCH_REMOTE}" = "yes" ]
    then
      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
    fi

    USE_SAMBA=no
    if [ "${USE_SAMBA}" = "yes" ]
    then
      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
    fi

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

    if [ ! -d "/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
