#!/bin/sh
#
# Monitors the codeload.progress file and outputs a keep-alive message
# after ~24 seconds if it has not been changed (based on file size).
# This keeps FTP and WBI sessions from timing out.

if [ -z "$1" ]; then
   exit;
fi

PROGFILE=$1

if [ ! -f $PROGFILE ]; then
    exit;
fi

count=0

while true; do
    FILESIZE=`stat -c%s $PROGFILE`
    [ $? -ne 0 ] && exit -1
    sleep 2
    if [ ! -f $PROGFILE ]; then
        exit;
    fi
    FILESIZE1=`stat -c%s $PROGFILE`
    [ $? -ne 0 ] && exit -1
    if [ $FILESIZE -eq $FILESIZE1 ]; then
        count=$((count+1))
    else
        count=0
    fi

    # If no message has been logged for ~24 seconds, write a keep-alive message.
    if [ $count -ge 12 ]; then
        echo "Please wait..."
        echo "Please wait..." >> $PROGFILE
        count=0
    fi
done

