#!/bin/sh
#General variables
FLUXPLUGHOME="/opt/fluxplug"
myhost=$(/bin/hostname -f)
MAXLINES=10

# Utilities
CURL="/usr/bin/curl"
AWK="/usr/bin/awk"
WC="/usr/bin/wc"
DF="/bin/df"
SED="/bin/sed"
CHRONY="/usr/bin/chronyc"

#Utilities with opts
CURLWOPTS="$CURL -is -o /dev/null -w \"%{http_code}\\\n\" --netrc-file $FLUXPLUGHOME/.fluxrc -XPOST 'http://influxdb.url/write?db=mydb' --data-binary"
CHRONYWOPTS="$CHRONY -nc tracking"
DFWOPTS="$DF -t ext4"
SEDWOPTS="$SED -e 1d"

#Directories
tmpdir="$FLUXPLUGHOME"/tmp
datadir="$FLUXPLUGHOME"/data

send_file() {
    datfile="$1"
    echo $CURLWOPTS" @$datfile"
}

write_line() {
    line="$1"
    length="$2"
    file="$3"
    tmpfile="$tmpdir"/"$file"
    datafile="$datadir"/"$file"

    echo "$line" >> "$tmpfile"
    lines=$("$WC" -l < "$tmpfile")
    if [ "$lines" -ge $length ]; then
        mv "$tmpfile" "$datafile"
        send_file "$datafile"
    fi
}

execute_every_minute() {
    for i in $(ls -1 $FLUXPLUGHOME/plugins-enabled/01m*); do
        . $i
    done

}

execute_every_minute
