#!/bin/sh

# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2018-present Team LibreELEC (https://libreelec.tv)

. /etc/profile
oe_setup_addon service.tvmosaic

sleep "$tvm_delay"

export TVMOSAIC_ROOT_CONFIG_DIR="$ADDON_HOME"

pkg_rev() {
  cat "$1/pkg_rev" 2> /dev/null
}

if [ "$(pkg_rev $ADDON_DIR/config)" != "$(pkg_rev $ADDON_HOME)" ]; then
  cp -r "$ADDON_DIR/config/"* "$ADDON_HOME"
fi

if [ -f "$ADDON_HOME/tvmosaic_configuration.xml" ]; then
  tvmosaic_reg -reginstall "$ADDON_HOME/data/common/product_info/tvmosaic.xml" update
else
  tvmosaic_reg -preparenewinstall "$ADDON_DIR/bin" "$ADDON_HOME/data" "$ADDON_HOME/shared.inst"
  tvmosaic_reg -reginstall "$ADDON_HOME/data/common/product_info/tvmosaic.xml" install
fi

tvmosaic_server
