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

# addons profile.d/*.profile
for config in /storage/.kodi/addons/*/profile.d/*.profile; do
  if [ -f "$config" ] ; then
    . $config
  fi
done

oe_setup_addon() {
  if [ ! -z $1 ] ; then
    DEF="/storage/.kodi/addons/$1/settings-default.xml"
    CUR="/storage/.kodi/userdata/addon_data/$1/settings.xml"

    # export some useful variables
    ADDON_DIR="$HOME/.kodi/addons/$1"
    ADDON_HOME="$HOME/.kodi/userdata/addon_data/$1"
    ADDON_LOG_FILE="$ADDON_HOME/service.log"

    [ ! -d $ADDON_HOME ] && mkdir -p $ADDON_HOME

    # copy defaults
    if [ -f "$DEF" -a ! -f "$CUR" ] ; then
      cp "$DEF" "$CUR"
    fi

    # parse config
    for xml_file in "$DEF" "$CUR"; do
      if [ -f "$xml_file" ]; then
        XML_SETTINGS_VER="$(xmlstarlet sel -t -m settings -v @version $xml_file)"
        if [ "$XML_SETTINGS_VER" = "2" ]; then
          eval $(xmlstarlet sel -t -m settings/setting -v @id -o "=" -v . -n "$xml_file" | sed -e "s/'/'\\\\''/g; s/=/='/; s/$/'/")
        else
          eval $(xmlstarlet sel -t -m settings -m setting -v @id -o "=" -v @value -n "$xml_file" | sed -e "s/'/'\\\\''/g; s/=/='/; s/$/'/")
        fi
      fi
    done
  fi
}
