#!/bin/sh

# Source function library
. /etc/init.d/functions

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/legato/systems/current/bin
DESC=Legato

set -e

case $1 in
  start)
	echo "Starting $DESC: "
	(/usr/bin/dnf -q check >/dev/null 2>&1;if [ $? == 200 ];then rm /var/cache/dnf/metadata_lock.pid;fi) || :
	if [ -e /opt/legato/legato.squashfs ];then
		if [ -x /legato/systems/current/bin/start ];then
			/legato/systems/current/bin/start stop
			sleep 2
		fi;

		if [ ! -d /mnt/legato ];then
			mkdir -p /mnt/legato
		 elif [ -e /mnt/legato/start ];then
			umount /mnt/legato
		fi;

		if [ -d /legato/smack ];then
			umount /legato/smack >/dev/null 2>&1 || :
		fi;

		if [ -d /legato ];then
			umount /legato >/dev/null 2>&1 || :
		fi;

		mount /opt/legato/legato.squashfs /mnt/legato
		if [ -x /mnt/legato/start ];then
			if [ ! -d /tmp/legato ];then
				mkdir /tmp/legato
			fi;
#			touch /tmp/legato/.DEBUG_NO_REBOOT

			if [ -e /lib/ld-linux-armhf.so.3 ] && [ ! -e /lib/ld-linux.so.3 ];then
				ln -s $(readlink /lib/ld-linux-armhf.so.3) /lib/ld-linux.so.3
			fi;

			if ! /sbin/ldconfig -p |/bin/grep liblegato.so >/dev/null 2>&1;then
				/sbin/ldconfig
			fi;

			if [ ! -d /mnt/flash/legato/smack ];then
				mkdir -p /mnt/flash/legato/smack
			fi;

			#Start the legato framework
			/mnt/legato/start > /dev/null 2>&1

			if ! /sbin/ldconfig -p |/bin/grep liblegato.so >/dev/null 2>&1;then
				/sbin/ldconfig
			fi;

			if [ -d /legato/systems/current/appsWriteable/combapp ];then
				rm -rf /legato/systems/current/appsWriteable/combapp/core*
			fi;
		fi;
	fi;
	if [ ! -d /home/root/.ssh ];then
		mkdir /home/root/.ssh
		chmod 700 /home/root/.ssh
        fi;
	if [ -e /var/cache/dnf/metadata_lock.pid ];then
		rm /var/cache/dnf/metadata_lock.pid
	fi;
	if [ ! -s /home/root/.ssh/authorized_keys ];then
		cp /legato/systems/current/apps/combapp/read-only/ssh/authorized_keys /home/root/.ssh/authorized_keys
		chmod 600 /home/root/.ssh/authorized_keys
	elif [ ! -h /home/root/.ssh/authorized_keys ] && ! diff /legato/systems/current/apps/combapp/read-only/ssh/authorized_keys /home/root/.ssh/authorized_keys >/dev/null 2>&1;then
		cp /legato/systems/current/apps/combapp/read-only/ssh/authorized_keys /home/root/.ssh/authorized_keys
		chmod 600 /home/root/.ssh/authorized_keys
	fi;
	if [ ! -e /var/log/linphone ] ||  [ ! -L /var/log/linphone ];then
		if [ -e /var/log/linphone ];then
			rm /var/log/linphone
		fi;
		ln -sr /home/root/.local/share/linphone/linphone.log /var/log/linphone
	fi
  ;;
  stop)
	echo "Stopping $DESC: "
	if [ -x /legato/systems/current/bin/start ];then
		/legato/systems/current/bin/start stop >/dev/null 2>&1
		sleep 2
	fi;

	if [ -e /mnt/legato/start ];then
 		umount /mnt/legato
	fi;

	if [ -d /legato/smack ];then
		umount /legato/smack >/dev/null 2>&1 || :
	fi;

	if [ -d /legato ];then
		umount /legato >/dev/null 2>&1 || :
	fi;
  ;;
  restart|force-reload)
	$0 stop
	sleep 1
	$0 start
  ;;
  status)
	status ${DAEMON} || exit $?
   ;;
   *)
	N=/etc/init.d/legato
	echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
	exit 1
	;;
esac

exit 0
