#!/bin/sh -e

### SYSTEM DEFINITIONS ########################################################
BOOTED_SYSTEM="?"

### BINARIES ##################################################################
SH="/bin/sh"
MOUNT="/bin/mount"
UNMOUNT="/bin/umount"
CHMOD="/bin/chmod"
CHOWN="/bin/chown"
DEVICESTACK="/opt/gira/bin/devicestack"
DEVICECONFIG="/opt/userdata/devicestack/devicestackconfig.xml"
[ -x ${DEVICESTACK} ] || DEVICESTACK="/opt/im/DeviceStack"

### STATUS FILES ##############################################################
CLONE_STATUS_FILE="/opt/userdata/.clone-status"
FWU_STATUS_FILE="/opt/userdata/.fwu-status"
SSH_ENABLE_FILE="/opt/userdata/.ssh-enabled"
WATCHDOG_ENABLE_FILE="/opt/userdata/.wd-enabled"
COMMISSIONED_FILE="/opt/extparam/.commissioned"

### LOG FILES #################################################################
CLONE_LOG="/var/log/clone.log"
FWU_PRE_LOG="/var/log/fwu_pre.log"
FWU_POST_LOG="/var/log/fwu_post.log"

### INIT SCRIPTS ##############################################################

### MOUNT POINTS ##############################################################
OFFLINE_SYSTEM_DIR="/opt/fwu/system_offline"
CURRENT_SYSTEM_DIR="/opt/fwu/system_current"

### SYSTEM A DEVICES ##########################################################

### SYSTEM B DEVICES ##########################################################

### ADDITIONAL DEVICES ########################################################
USER_DATA=$(cat /opt/extparam/userdata_root_blkdev)

### SCRIPT PARAMETERS #########################################################
APP_UPDATE="FALSE"
SYS_UPDATE="FALSE"
COMMISSION="FALSE"

### FUNCTIONS #################################################################
parse_arguments()
{
  if [ "$1" = "/UpdateApplicationFiles:True" ]
  then
    APP_UPDATE="TRUE"
    printf "Application files are SET to be updated.\\n"
  else
    printf "application files are NOT set to be updated.\\n"
  fi

  if [ "$2" = "/UpdateSystemFiles:True" ]
  then
    SYS_UPDATE="TRUE"
    printf "System files are SET to be updated..\\n"
  else
    printf "System files are NOT set to be updated..\\n"
  fi

  if [ "$3" = "/CommissioningMode:True" ]
  then
    COMMISSION="TRUE"
    printf "Running in commissioning mode.\\n"
  else
    printf "Running in update mode.\\n"
  fi
}

check_executable()
{
  [ -x $1 ] || (printf "FATAL: %s binary is not executable.\\n" "$1" && exit 1)
}

check_file()
{
  [ -f $1 ] || (printf "FATAL: %s is not a file.\\n" "$1" && exit 2)
}

identify_offline()
{
	printf "[ipmodule-functions] identify_offline() called but not implemented!\\n"
}

set_offline_system_a()
{
	printf "[ipmodule-functions] set_offline_system_a() called but not implemented!\\n"
}

set_offline_system_b()
{
	printf "[ipmodule-functions] set_offline_system_b() called but not implemented!\\n"
}

erase_offline()
{
	printf "[ipmodule-functions] erase_offline() called but not implemented!\\n"
}

erase_userdata()
{
	printf "[ipmodule-functions] erase_userdata() called but not implemented!\\n"
}

mount_offline()
{
	printf "[ipmodule-functions] mount_offline() called but not implemented!\\n"
}

unmount_offline()
{
	printf "[ipmodule-functions] unmount_offline() called but not implemented!\\n"
}

mount_current()
{
	printf "[ipmodule-functions] mount_current() called but not implemented!\\n"
}

unmount_current()
{
	printf "[ipmodule-functions] unmount_current() called but not implemented!\\n"
}

clone_current()
{
	printf "[ipmodule-functions] clone_current() called but not implemented!\\n"
}

remount_current_rw()
{
  printf "Remounting current system writable...\\n"
  (${MOUNT} -o remount,rw /) || printf "Failed to remount current system as writable.\\n"
}

remount_current_ro()
{
  printf "Remounting current system read-only...\\n"
  sync
  (${MOUNT} -o remount,ro /) || printf "Failed to remount current system as read-only.\\n"
}

umount_single()
{
  printf "Unmounting %s ... " "$1"
  if [ "$(mount | grep $1 | wc -l)" -gt "0" ]
  then
    umount $1
    printf "done.\\n"
  else
    printf "not mounted.\\n"
  fi
}

umount_all()
{
	printf "[ipmodule-functions] umount_all() called but not implemented!\\n"
}

disable_ssh()
{
	printf "[ipmodule-functions] disable_ssh() called but not implemented!\\n"
}

enable_ssh()
{
	printf "[ipmodule-functions] enable_ssh() called but not implemented!\\n"
}

disable_watchdog_trigger()
{
	printf "[ipmodule-functions] disable_watchdog_trigger() called but not implemented!\\n"
}

enable_watchdog_trigger()
{
	printf "[ipmodule-functions] enable_watchdog_trigger() called but not implemented!\\n"
}

