#!/bin/sh
# Copyright (C) 2016-2018 Algo Communication Products Ltd.

mkdir -p /tmp/web/mp

# make sure settings download link works
READCONF=/opt/algo/bin/readconf
PROD=$($READCONF product.name)

if [ ! -d /opt/algo/web/tones ]; then
	rm -rf /opt/algo/web/tones
	mkdir -p /opt/algo/web/tones
	sync
fi

if [ ! -e /opt/algo/config/tones ]; then
	ln -sf /opt/algo/web/tones /opt/algo/config/tones
	sync
fi

mkdir -p /opt/algo/cache/fm

# clean up links generated by old firmware
cd /opt/algo/web/root/control/
if ls -U settings*.txt >/dev/null 2>&1; then
	rm -f settings*.txt
	sync
fi

if ls -U scheduler*.txt >/dev/null 2>&1; then
	rm -f scheduler*.txt
	sync
fi

# install copyright files to be compatible with old base firmware
PLATFORM=$($READCONF product.platform)
if [ ! -d /opt/algo/doc/linux-image-$PLATFORM ] && \
	[ -f /opt/algo/info/linux-image-$PLATFORM.copyright ]; then
	mkdir -p /opt/algo/doc/linux-image-$PLATFORM
	cp /opt/algo/info/linux-image-$PLATFORM.copyright \
		/opt/algo/doc/linux-image-$PLATFORM/copyright
fi
if [ ! -d /opt/algo/doc/uboot-tools-$PLATFORM ] && \
	[ -f /opt/algo/info/uboot-tools-$PLATFORM.copyright ]; then
	mkdir -p /opt/algo/doc/uboot-tools-$PLATFORM
	cp /opt/algo/info/uboot-tools-$PLATFORM.copyright \
		/opt/algo/doc/uboot-tools-$PLATFORM/copyright
fi

PROD_INIT=/opt/algo/bin/prod-init-webui
[ -x $PROD_INIT ] && $PROD_INIT
