#!/bin/sh

: << =cut

=head1 NAME

digitemp_ - Plugin to monitor Dallas 1-wire temperature sensors using
digitemp

=head1 CONFIGURATION

The following environment variables are used by this plugin:

 digitemprc - config file to use. (Default /etc/digitemp.conf)
              This must be generated with digitemp_<model> -i

=head1 USAGE

Symlink as the name of the digitemp program to use, i.e. digitemp_DS2490
and create configuration file (default: /etc/digitemp.conf).

=head1 AUTHOR

  Copyright (C) 2006 Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>

=head1 LICENSE

This program 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; version 2 dated June, 1991.

This program 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 this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

=head1 MAGIC MARKERS

 #%# capabilities=autoconf suggest
 #%# family=auto

=cut

# common values: digitemp_DS2490, digitemp_DS9097, digitemp_DS9097U
digitemp_bin=${0##*/}
model=${digitemp_bin##*_}
digitemprc=${digitemprc:-/etc/digitemp.conf}


get_sensor_data() {
    local output_pattern="$1"
    "$digitemp_bin" -c "$digitemprc" -q -a -o "$output_pattern" | grep -v ^Found
}


# find executable files in the search path starting with "digitemp_"
find_digitemp_executables() {
    [ -z "${PATH:-}" ] && return
    # split PATH by colons
    local IFS=":"
    # shellcheck disable=SC2086
    find $PATH -maxdepth 1 -xtype f -executable -name "digitemp_*" -print0 2>/dev/null \
        | xargs -0 -r -n 1 basename
}


if [ "$1" = "suggest" ]; then
    find_digitemp_executables
    exit 0
fi


if [ "$1" = "autoconf" ]; then
    if [ -z "$(find_digitemp_executables)" ]; then
        echo "no (failed to find executable starting with 'digitemp_')"
    elif [ ! -r "$digitemprc" ]; then
        echo "no (failed to read configuration file: $digitemprc)"
    else
        echo "yes"
    fi
    exit 0
fi


# check if the digitemp executable is available
if ! [ -x "$(command -v "$digitemp_bin")" ]; then
    echo "executable '$digitemp_bin' not found" >&2
    exit 1
fi

if [ "$1" = "config" ]; then
    echo "graph_title DigiTemp $model temperatures"
    echo 'graph_vlabel degrees C'
    echo 'graph_category sensors'
    echo "graph_info This graph shows the temperature read from $model 1-wire sensors"
    get_sensor_data "%R %s" | while read -r serial sensor; do
        echo "sensor$serial.label sensor #$sensor"
        echo "sensor$serial.type GAUGE"
        echo "sensor$serial.info Temperature from sensor #$sensor"
        echo "sensor$serial.critical 30"
        echo "sensor$serial.warning 25"
   done
   exit 0
fi

get_sensor_data "sensor%R.value %C"
