#!/usr/bin/env python
# log event for arduino yun RouterReset

# for dealing with dates and times
from datetime import datetime, timedelta

# for checking if a file exists
import os

# for dealing with command-line arguments
import sys

# function for formatting change in time
def strfdelta(tdelta):
    hours, rem = divmod(tdelta.seconds, 3600)
    hours += tdelta.days * 24
    minutes, seconds = divmod(rem, 60)
    return("%d:%02d:%02d" % (hours, minutes, seconds))

directory = '/mnt/sd/arduino/www/'

# current time
now = datetime.today()

# command-line argument: status (startup/on/off)
status = sys.argv[1] if len(sys.argv)>1 else 'startup'

logfile = os.path.join(directory, now.strftime('log_%Y-%m.txt'))
lastlog_file = os.path.join(directory, '.lastlog.txt')
lastlog_status = status # default: no change

if os.path.exists(lastlog_file):
    lastlog = open(lastlog_file).readlines()
    lastlog_time = datetime.strptime(lastlog[0].strip(), '%Y-%m-%d %H:%M:%S')
    lastlog_status = lastlog[1].strip()

# add to log
with open(logfile, 'at') as f:
    output = '%-10s %-8s %-7s' % (now.strftime('%Y-%m-%d'), 
                                 now.strftime('%H:%M:%S'), status)
    if (status != 'startup' and status != lastlog_status):
        output += (' (was %-3s ' % lastlog_status +
                   strfdelta(now - lastlog_time)) + ')'

    f.write(output + '\n')

if status != 'startup' and (not os.path.exists(lastlog_file) or status != lastlog_status): # update 'last log'
  with open(lastlog_file, 'wt') as f:
      f.write(now.strftime('%Y-%m-%d %H:%M:%S') + '\n')
      f.write(status + '\n')


@@ -60,43 +60,8 @@
 et%0A%0A
-# for dealing with dates and times%0A
 from
@@ -101,392 +101,89 @@
 lta%0A
-%0A# for checking if a file exists%0Aimport os%0A%0A# for dealing with command-line arguments%0Aimport sys%0A%0A# function for formatting change in time%0Adef strfdelta(tdelta):%0A    hours, rem = divmod(tdelta.seconds, 3600)%0A    hours += tdelta.days * 24%0A    minutes, seconds = divmod(rem, 60)%0A    return(%22%25d:%2502d:%2502d%22 %25 (hours, minutes, seconds))%0A%0Adirectory = '/mnt/sd/arduino/www/'%0A%0A# current time%0A
+import os%0Aimport sys%0A%0A# function to log time/event%0Adef logEvent (directory):%0A    
 now 
@@ -202,16 +202,20 @@
 oday()%0A%0A
+    
 # comman
@@ -255,16 +255,20 @@
 on/off)%0A
+    
 status =
@@ -315,16 +315,20 @@
 artup'%0A%0A
+    
 logfile 
@@ -384,16 +384,20 @@
 .txt'))%0A
+    
 lastlog_
@@ -443,16 +443,20 @@
 g.txt')%0A
+    
 lastlog_
@@ -493,16 +493,20 @@
 change%0A%0A
+    
 if os.pa
@@ -530,16 +530,20 @@
 _file):%0A
+    
     last
@@ -579,24 +579,28 @@
 lines()%0A    
+   
+ 
 lastlog_time
@@ -661,16 +661,20 @@
 %25M:%25S')%0A
+    
     last
@@ -706,16 +706,94 @@
 trip()%0A%0A
+    # does log file exist?%0A    logfile_not_new = os.path.exists(logfile)%0A%0A    
 # add to
@@ -797,16 +797,20 @@
  to log%0A
+    
 with ope
@@ -828,24 +828,28 @@
 'at') as f:%0A
+    
     output =
@@ -899,10 +899,12 @@
 d'),
- 
 %0A
+   
     
@@ -966,16 +966,20 @@
 status)%0A
+    
     if (
@@ -1029,16 +1029,20 @@
 tatus):%0A
+    
         
@@ -1104,16 +1104,20 @@
         
+    
 strfdelt
@@ -1145,21 +1145,24 @@
 ) + ')'%0A
-%0A
+   
     
+ 
 f.write(
@@ -1177,16 +1177,44 @@
  '%5Cn')%0A%0A
+    # update 'last log'%0A    
 if statu
@@ -1295,37 +1295,23 @@
 status):
- # update 'last log'%0A
+%0A      
   with o
@@ -1336,24 +1336,30 @@
 'wt') as f:%0A
+      
       f.writ
@@ -1402,16 +1402,22 @@
 + '%5Cn')%0A
+      
       f.
@@ -1429,17 +1429,502 @@
 (status + '%5Cn')%0A
+    if status == 'startup' and os.path.exists(lastlog_file):%0A        os.remove(lastlog_file)%0A%0A    # if it was a new log file, re-build web page:%0A    if not logfile_not_new:%0A        print 'rebuild web'%0A%0A%0A# function for formatting change in time%0Adef strfdelta(tdelta):%0A    hours, rem = divmod(tdelta.seconds, 3600)%0A    hours += tdelta.days * 24%0A    minutes, seconds = divmod(rem, 60)%0A    return(%22%25d:%2502d:%2502d%22 %25 (hours, minutes, seconds))%0A%0Aif __name__ == '__main__':%0A    logEvent('test')
 %0A

move stuff into a function