#!/bin/bash
# simple script with accompanying service that waits for the /data partition
# to be mounted which often happens after other services start that need
# calibration files in /data
#
# to use, make your other systemd service files reference
# voxl-wait-for-fs.service like this:
#
# [Unit]
# After=voxl-wait-for-fs.service
# Requires=voxl-wait-for-fs.service

set +e

# Sets up the modalai's log files and folders
setup_journal_system()
{
	echo "Setting up journal system"

	VOLATILE_DIR="/run/mjrn/"
	PERSIST_DIR="/data/mjrn/"
	BOOT_DIR="${PERSIST_DIR}current-boot"
	BOOT_LOCK="${VOLATILE_DIR}current-boot-ready"
	LEVEL_FILE="${PERSIST_DIR}log-level"
	LEVEL_DIR="${PERSIST_DIR}log-level.d/"

	mkdir -p $VOLATILE_DIR
	mkdir -p $PERSIST_DIR
	mkdir -p $LEVEL_DIR

	if [ -f $BOOT_LOCK ] ; then
		# All Good
		echo "Journal system has already been set up for this boot"
	elif ls $PERSIST_DIR | grep -qv current ; then

		# Don't bother creating a new folder if the old one was empty
		if [ $( ls $BOOT_DIR 2>&1 | wc -l ) -eq 0 ] ; then
			touch $BOOT_LOCK
		else
			# Previous boots detected, take the highest number and increment
			echo "Previous boot setup detected, creating new boot"
			local HIGHEST_PREV=$( ls ${PERSIST_DIR} | grep boot | grep -v current | tail -n 1 | egrep -o [0-9]+ )
			local NEW_BOOT=$( printf "${PERSIST_DIR}boot-%06d" $((HIGHEST_PREV+1)) )
			mkdir $NEW_BOOT
			rm -f $BOOT_DIR
			ln -sf $NEW_BOOT $BOOT_DIR
			touch $BOOT_LOCK
		fi
	else
		# No previous boots, first time setup
		echo "No previous boots detected, performing first time setup "
		local NEW_BOOT=$( printf "${PERSIST_DIR}boot-%06d" 1 )
		mkdir $NEW_BOOT
		rm -f $BOOT_DIR
		ln -sf $NEW_BOOT $BOOT_DIR
		touch $BOOT_LOCK
	fi

	# Default level shows warning and error logs, no debug or verbose
	if ! [ -f $LEVEL_FILE ] ; then
		echo "warning" > $LEVEL_FILE
	fi

}


# Speed up disk writing to reduce chance of FS corruption.
# These should really be set in /etc/sysctl.conf but that doesn't seem to work.
# Note that /etc/initscripts/init_post_boot is a QC file that also tries to set
# dirty_expire_centisecs but we comment out that line in the voxl-utils postinst
# script so that both can be set here to our desired values
echo 50 > /proc/sys/vm/dirty_writeback_centisecs
echo 10 > /proc/sys/vm/dirty_expire_centisecs



# QRB5165 and QCS6490 have many parititions we should wait for, but systemctl handles those
# with /lib/systemd/system/*.mount so we just need wait-for-fs.service to run
# after local-fs.target and we should be good. We still want to run this script
# on QRB5165 to give us flexibility to tweak in the future and also to set
# the disk flush timeouts above.
setup_journal_system
echo "DONE"
exit 0



# # on APQ8096, wait for /data/ partiion to be mounted
# for i in {0..10}; do

# 	if grep -q "/data" /proc/mounts; then
# 		setup_journal_system
# 		echo "voxl-wait-for-fs SUCCESS after $i seconds"
# 		exit 0
# 	fi
# 	sleep 1

# done

# echo "voxl-wait-for-fs: data partition failed to mount after $i seconds"
# exit 1
