#!/bin/sh

# This script reports non personally identifiable information to OSMC so that we can
# see active user statistics.

[ -e /etc/osmc/prefs.d/stats_preferences ] && . /etc/osmc/prefs.d/stats_preferences

if [ "$OSMC_REPORT_STATS" != 1 ]; then echo "Reporting statistics is disabled"; exit 0; fi

OSMC_VERSION=$(cat /etc/os-release | grep VERSION_ID | cut -d '=' -f 2 | tr -d '"')
OSMC_DEV="$(sed 's/\ /\n/g' /proc/cmdline | grep "^osmcdev=" | sed 's/^osmcdev=//g')"
OSMC_DEV_SUBDEVICE=$OSMC_DEV
if grep -q "osmcdev=rbp" /proc/cmdline; then
   OSMC_DEV_SUBDEVICE=$(cat /proc/cpuinfo | grep Revision | cut -d ' ' -f 2)
fi
if grep -q "osmcdev=vero3" /proc/cmdline; then
   OSMC_DEV_SUBDEVICE=$(cat /proc/device-tree/model)
fi
DEVICE_ID=$(cat /.osmcguid)
KERNEL_VERSION=$(/bin/uname -r)
KODI_VERSION=$(/usr/bin/dpkg -l | grep mediacenter-osmc | head -n1 | awk {'print $3'})

ENDPOINT_URL="https://stats.osmc.tv"

curl -s -S -X POST \
        -F "OSMC_VERSION=$OSMC_VERSION" \
        -F "OSMC_DEV=$OSMC_DEV" \
        -F "OSMC_DEV_SUBDEVICE=$OSMC_DEV_SUBDEVICE" \
        -F "DEVICE_ID=$DEVICE_ID" \
        -F "KERNEL_VERSION=$KERNEL_VERSION" \
        -F "KODI_VERSION=$KODI_VERSION" \
$ENDPOINT_URL >/dev/null
