#!/bin/sh
LOGFILE=/opt/userdata/log/buddyinfo.csv
LOGLOCAL=/opt/userdata/log/buddyinfo.csv
LOGCARD=/opt/card/buddyinfo.csv
INTERVAL=60
BACKUPSIZE=1024000
COUNT=60

while
    if [ ! -f /opt/card/.nocard ]; then
      LOGFILE=${LOGCARD}
      COUNT=60
      BACKUPSIZE=1024000
    else
      LOGFILE=${LOGLOCAL}
      BACKUPSIZE=512000
    fi
    if [ ${COUNT} -ge 60 ]; then
      if [ ! -f ${LOGFILE} ]; then
          echo '"Unixtime [s]";"Uptime [s]";"0: 4kB";"1: 8kB";"2: 16kB";"3: 32kB";"4: 64kB";"5: 128kB";"6: 256kB";"7: 512kB";"8: 1MB";"9: 2MB";"10: 4MB"' > ${LOGFILE}   
      fi
      buddyline=$(cat /proc/buddyinfo );
      $(printf "%s %s %s\n" "$(date +%s)" "$(cat /proc/uptime | cut -d. -f0)" "${buddyline#*l}" | sed "s/ *$//" | sed "s/ \+/;/g" >> ${LOGFILE})
      if [ "$(wc -c ${LOGFILE} | cut -d ' ' -f1)" -gt ${BACKUPSIZE} ]; then
          mv -f ${LOGFILE} ${LOGFILE}.1
      fi
      COUNT=0
    else
      let COUNT=COUNT+1
    fi
    sleep ${INTERVAL}
do :; done
