Log rolling to preserve .gz extension

- Workaround to avoid rotated log to end up in a filename .log.gz.1
  it should now become .log.1.gz
  Hopefully this will make it easier to gunzip rotated and gzipped rolled log.

One long ONOS core "run" will  result in files like
  onos.onos-vm.log
  onos.onos-vm.1.log.gz
  onos.onos-vm.2.log.gz
  ...
when new "run" is started (e.g., ONOS restart), old log will be renamed like:
  onos.onos-vm.log => onos.onos-vm.log.1
  onos.onos-vm.1.log.gz => onos.onos-vm.1.log.1.gz
  onos.onos-vm.2.log.gz => onos.onos-vm.2.log.1.gz
  ...

Behavior of all other log, which does not rotate within a single run is not changed.

Change-Id: Id8dffb1f65fcb7c59f79203405c5c697b1751685
diff --git a/onos.sh b/onos.sh
index 30a7d8a..1c89b4f 100755
--- a/onos.sh
+++ b/onos.sh
@@ -174,16 +174,27 @@
   echo "${usage}"
 }
 
+# rotate-log [log-filename] [max rotations]
+# Example:
+#  foobar.log -> foobar.log.1
+#  foobar.log.1 -> foobar.log.2
+#  foobar.log.gz -> foobar.log.1.gz
 function rotate-log {
   local logfile=$1
   local nr_max=${2:-10}
   if [ -f $logfile ]; then
+    # TODO treating only .gz now. probably want more generic solution
+    local basename=${logfile%%.gz}
+    local append=""
+    if [ "$basename" != "$logfile" ]; then
+      append=".gz"
+    fi
     for i in `seq $(expr $nr_max - 1) -1 1`; do
-      if [ -f ${logfile}.${i} ]; then
-        mv -f ${logfile}.${i} ${logfile}.`expr $i + 1`
+      if [ -f ${basename}.${i}${append} ]; then
+        mv -f ${basename}.${i}${append} ${basename}.`expr $i + 1`${append}
       fi
     done
-    mv $logfile $logfile.1
+    mv ${basename}${append} ${basename}.1${append}
   fi
 }
 
@@ -920,7 +931,6 @@
   for rolled_log in ${rolled_log_shellpat}; do
     if [ -f ${rolled_log} ]; then
       rotate-log ${rolled_log}
-      # NOTE: renamed file will end up with an extension like .log.gz.1
     fi
   done