#!/bin/sh
# Copyright (c) 2013-2019 Algo Communication Products Ltd.

### BEGIN INIT INFO
# Provides:          algo-setup-prenet
# Required-Start:    $local_fs $time
# Required-Stop:     $local_fs
# Default-Start:     S
# Default-Stop:      0 6
# Short-Description: Set up Algo product environment before networking init.
### END INIT INFO

EARLY_LOG=/run/algo-early.log
READCONF=/opt/algo/bin/readconf
PLATFORM=$($READCONF product.platform)

use_default()
{
	/opt/algo/bin/show-state reset-confirmed
	/opt/algo/bin/restore-default
	/opt/algo/bin/apply-settings all
	reboot
	sleep 100000
}

brown_check_reset()
{
	RESET_LINE=/tmp/gpio/btnreset/value
	for i in $(seq 1 5); do
		if [ "$(cat $RESET_LINE)" = "1" ]; then
			sleep 1
			# test again after 1 second to confirm reset
			if [ "$(cat $RESET_LINE)" = "1" ]; then
				use_default
			fi
		fi
		sleep 1
	done
}

cameron_check_reset()
{
	# check call button
	for i in $(seq 1 5); do
		if [ "$(i2cget -f -y 1 0x20)" = "0x0c" ]; then
			sleep 1
			# test again after 1 second to confirm reset
			if [ "$(i2cget -f -y 1 0x20)" = "0x0c" ]; then
				use_default
			fi
		fi
		sleep 1
	done
}

dailly_check_reset()
{
	/opt/algo/bin/bootui reset
        OUT=$?
        if [ $OUT -eq 2 ]; then
		use_default
        fi
}

# execute after leap upgrade
run_postupg()
{
	POSTUPG_CMD=/opt/algo/cache/postupg
	if [ -x $POSTUPG_CMD ]; then
		$POSTUPG_CMD
		rm -f $POSTUPG_CMD
		sync
	fi
}

start_service()
{
	if [ -d /lost+found ]; then
		rm -rf /lost+found/*
		sync
	fi

	mkdir -p /run/algo-uenv /run/algo-prod
	/opt/algo/bin/fw_printenv -n algoprodid > /run/algo-uenv/algoprodid \
		2>/dev/null || :
	/opt/algo/bin/fw_printenv -n algohwver > /run/algo-uenv/algohwver \
		2>/dev/null || :

	# for backward compatibility with early 8188 boards
	if [ ! -s /run/algo-uenv/algoprodid ]; then
		echo "8188" > /run/algo-uenv/algoprodid
	fi
	if [ ! -s /run/algo-uenv/algohwver ]; then
		echo "v1" > /run/algo-uenv/algohwver
	fi

	mkdir -p /run/ntp
	chown ntp.ntp /run/ntp
	if [ ! -L /var/lib/ntp ]; then
		rm -rf /var/lib/ntp
		ln -sf /run/ntp /var/lib/ntp
	fi

	mkdir -p /opt/algo/config/certs/trusted
	/opt/algo/bin/update-certs

	if [ ! -r /opt/algo/info/algo_device.pem ]; then
		CERT_PATH=/tmp/mmc
		mkdir -p $CERT_PATH
		if [ "$PLATFORM" = "dailly" ]; then
			DATA_DEVICE=/dev/mtdblock6
		else
			DATA_DEVICE=/dev/mmcblk0boot1
		fi
		mount -t vfat -o ro $DATA_DEVICE $CERT_PATH

		if [ -f $CERT_PATH/algo_device.pem ]; then
			cp $CERT_PATH/algo_device.pem /opt/algo/info/
			sync
		fi

		umount $CERT_PATH
		rm -rf $CERT_PATH
	fi

	if [ -r /opt/algo/info/algo_device.pem ]; then
		cat /etc/ssl/certs/algo_issuing.pem \
			/etc/ssl/certs/algo_intermediate.pem \
			/etc/ssl/certs/algo_ca.pem > /tmp/algo_chain.pem

		openssl verify -CAfile /tmp/algo_chain.pem \
			/opt/algo/info/algo_device.pem

		if [ $? -eq 0 ]; then
			echo "Algo device certificate installed" >> $EARLY_LOG
		else
			echo "Algo device cert could not be validated" >> \
				$EARLY_LOG
		fi
		echo "Installed" > /run/algo-cert.status
	else
		echo "Algo device certificate not found" >> $EARLY_LOG
		echo "Not Installed" > /run/algo-cert.status
	fi

	/opt/algo/bin/config-gpio
	if [ -x /opt/algo/bin/prod-setup-prenet ]; then
		/opt/algo/bin/prod-setup-prenet
	fi
	run_postupg

	/opt/algo/bin/show-state reset
	if [ "$PLATFORM" = "brown" ]; then
		brown_check_reset
	elif [ "$PLATFORM" = "cameron" ]; then
		cameron_check_reset
	elif [ "$PLATFORM" = "dailly" ]; then
		dailly_check_reset
	fi
	/opt/algo/bin/show-state reset-ignored

	if [ ! -f /opt/algo/config/user.conf ]; then
		use_default
	else
		/opt/algo/bin/upgrade-settings
		# system integrity check
		if [ "$($READCONF admin.sic.use)" = "1" ]; then
			/opt/algo/bin/run-debsums
		fi
	fi

	if [ ! -d /opt/algo/config/license ]; then
		mkdir -p /opt/algo/config/license
		sync
	fi

	if [ -x /opt/algo/bin/prod-setup-prenet ]; then
		/opt/algo/bin/prod-setup-prenet
	fi
}

stop_service()
{
	echo "do nothing"
}

case "$1" in
start)
	start_service
	;;

stop)
	stop_service
	;;

*)
	log_action_msg "Usage: /etc/init.d/algo-setup-prenet {start|stop}"
	exit 1
esac

exit 0
