#!/bin/bash

data_file=/tmp/HDmon.out  #Using a file in /tmp for now
cumul_mins=0
stat_file=/tmp/HDmonStat.out


# Storing the initial_load count for later use
set `sudo smartctl -a /dev/sda | egrep '(Temperature|Load_Cycle_Count|Power_On_Hours)'`
# This will store them into  $1, $2 and $3.
set `echo  $@ | cut -d\  -f10,20,30`  
initial_load_count=$2

# Important!! The data is always appended to the $data_file file, so delete it
# if you want to take readings from the beginning.

# Touch the data files the file, before anything else
touch $data_file
touch $stat_file

# Create a marker to denote new stream of data using '****'s
echo "*****************************" >> $data_file
echo `date`  >> $data_file
echo "*****************************" >> $data_file
echo -n "APM --> " >> $data_file
echo `sudo hdparm -iI /dev/sda | grep "Advanced power management level" | cut -d: -f2` >> $data_file
# Create the header in the file
echo "Time      Power_on_time     Load_Cycle_Count  Temperature(Deg C)       Cumulative count/minute" >> $data_file
echo >>$data_file 

# Get the values
set `sudo smartctl -a /dev/sda | egrep '(Temperature|Load_Cycle_Count|Power_On_Hours)'`
# This will store them into  $1, $2 and $3.
set `echo  $@ | cut -d\  -f10,20,30`  

# Storing the load count for later use
load_count=$2
((delta_load_count=$load_count-$initial_load_count))

# Dump into the file one by one
echo -n `date | cut -d\   -f4`'     ' >> $data_file ; echo -n "$1              $2             $3                                " >> $data_file


# Output the cumulative load count per minute here
if [ $cumul_mins -eq 0 ]
then
# ND stands for NOT DEFINED
echo "ND" >> $data_file
else
echo "scale=2; $delta_load_count/$cumul_mins" | bc  >> $data_file
fi

# Writing into file ends here

# Show the value in the small xterm window
# Delete the file at the beginning
\rm $stat_file
echo `date` >> $stat_file
echo -n "APM --> " >> $stat_file
echo `sudo hdparm -iI /dev/sda | grep "Advanced power management level" | cut -d: -f2` >> $stat_file
#echo -n "Power on hours -->  "  >> $stat_file 
#echo $1  >> $stat_file

echo -n "LC Cnt --> " >> $stat_file
echo $2  >> $stat_file

echo -n "Temp -->  "  >> $stat_file
echo $3  >> $stat_file
echo -n "Avg. LC/hour --> "  >> $stat_file
# Bash does not have floating point support built-in, so use bc to calculate
# the avg no. of load cycles per hour.
echo "scale=2; $2/$1" | bc >> $stat_file
((cumul_mins=$cumul_mins+1))


