#!/bin/bash
# Set up a configuration for sensord for "primary use"
# The way we choose the configuration is as follows:
#   1. Prefer the hybris plugin (if available)
#   2. If not, try to pick the best-match from the boardname

cd /etc/sensorfw/
if [ -x /usr/sbin/sensorfwd ]; then
    if ldconfig -p | grep libhybrissensorfw ; then
        # Always prefer hybris sensor backend to anything else (also overwrite
        # old setting, as sometimes the boardname matches, e.g. on grouper)
        if [ "$(readlink primaryuse.conf)" != sensord-hybris.conf ]; then
            ln -sf sensord-hybris.conf primaryuse.conf
        fi
    elif [ ! -f primaryuse.conf -a -f /etc/boardname ]; then
        # Determine the correct "primary" config file: the first conf file
        # whose basename is a substring of boardname, if any (i.e. 'foo.conf'
        # matches all devices which have 'foo' in their /etc/boardname)
        for f in sensord-*.conf; do
            fbase="${f#sensord-}"
            if grep -q "${fbase%.conf}" /etc/boardname; then
                # N9/N950 (RM-696/RM-680) boardname containts rm_680 for both
                # use /proc/1/environ to tell them apart
                if [ "$f" = "sensord-rm_680.conf" ] && grep -q 'RM-696' /proc/1/environ; then
                   f="sensord-rm_696.conf"
                fi
                ln -s $f primaryuse.conf
                break
            fi
        done
    fi
fi
