#!/bin/sh

# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2009-2014 Stephan Raue (stephan@openelec.tv)
# Copyright (C) 2018-present Team LibreELEC (https://libreelec.tv)

KODI_ROOT=$HOME/.kodi

BOOT_STATE="$(cat $HOME/.config/boot.status 2>/dev/null)"

# May not exist if testing a clean /storage/.kodi without rebooting
mkdir -p $KODI_ROOT/userdata

# hack: make addon-bins executable
# done in kodi on addon install. but just in case..
chmod +x $KODI_ROOT/addons/*/bin/* 2>/dev/null

# setup Kodi sources
if [ ! -f $KODI_ROOT/userdata/sources.xml ]; then
  if [ -f /usr/share/kodi/config/sources.xml ]; then
    cp /usr/share/kodi/config/sources.xml $KODI_ROOT/userdata
  fi
fi

# common setup guisettings
if [ ! -f $KODI_ROOT/userdata/guisettings.xml ] ; then
  if [ -f /usr/share/kodi/config/guisettings.xml ]; then
    cp /usr/share/kodi/config/guisettings.xml $KODI_ROOT/userdata
  fi
  if [ "$BOOT_STATE" = "SAFE" ]; then
    [ ! -f $KODI_ROOT/userdata/guisettings.xml ] && echo '<settings version="2"></settings>' > $KODI_ROOT/userdata/guisettings.xml
    xmlstarlet ed --omit-decl --inplace -s settings -t elem -n setting -v "maroon" -i settings/setting -t attr -n id -v lookandfeel.skincolors $KODI_ROOT/userdata/guisettings.xml
  fi
fi

KODI_ARGS=""

echo "KODI_ARGS=\"$KODI_ARGS\"" > /run/libreelec/kodi.conf

if [ "$(uname -m)" = "x86_64" ]; then
  echo "MALLOC_MMAP_THRESHOLD_=524288" >> /run/libreelec/kodi.conf
else #arm
  echo "MALLOC_MMAP_THRESHOLD_=8192" >> /run/libreelec/kodi.conf
fi

if [ -f /storage/.config/kodi.conf ] ; then
  cat /storage/.config/kodi.conf >>/run/libreelec/kodi.conf
fi
