#!/bin/sh
################################################################################
#      This file is part of OpenELEC - http://www.openelec.tv
#      Copyright (C) 2009-2014 Stephan Raue (stephan@openelec.tv)
#
#  OpenELEC is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 2 of the License, or
#  (at your option) any later version.
#
#  OpenELEC is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with OpenELEC.  If not, see <http://www.gnu.org/licenses/>.
################################################################################

# Creates a unique machine-id based on a local MAC address which persists over
# reboots, upgrades and reinstallation. For systems with slow loading network
# drivers (no MAC's available) dbus-uuidgen is used which persists over reboot
# and upgrades but not reinstallation. If a MAC is used it is hashed to make it
# anonymous. The machine-id is used by dbus and systemd, and to collect basic
# active-installation statistics on LibreELEC users.

if [ -e "/storage/.cache/machine-id" ]; then
  MACHINEID=`cat /storage/.cache/machine-id`
elif [ -e "/sys/class/net/eth0/address" ]; then
  MAC_ADDRESS=`cat /sys/class/net/eth0/address`
elif [ -e "/sys/class/net/wlan0/address" ]; then
  MAC_ADDRESS=`cat /sys/class/net/wlan0/address`
else
  MAC_ADDRESS=`/usr/bin/dbus-uuidgen`
fi

if [ -z "$MACHINEID" ]; then
  MACHINEID=`echo $MAC_ADDRESS | md5sum | cut -f1 -d" "`
  mkdir -p /storage/.cache
fi

echo "$MACHINEID" > /storage/.cache/machine-id
