#!/bin/bash

echo '<<<checkrestart>>>'

TMP_FILE=/tmp/checkrestart.output
PID_FILE=/tmp/checkrestart.pid

if ! type checkrestart &>/dev/null ; then
    echo "ERROR: checkrestart not available"
    exit 0
fi

still_running="no"
pid=$(cat $PID_FILE 2>/dev/null)
if [ ${#pid} -gt 0 ] ; then
    ps ax | awk -v PID=$pid '{if($1==PID && $6~"checkrestart")exit 1}' > /dev/null 2> /dev/null
    if [ $? -eq 1 ] ; then
        still_running="$pid"
    fi
fi

if [ "$still_running" != "no" ]; then
    echo "pid ${still_running} still exists"
    exit 0
fi

if [[ $(find "$TMP_FILE" -mmin -1440 2>/dev/null | wc -l) -gt "0" ]] ; then
    cat $TMP_FILE 2>/dev/null
else
    cat $TMP_FILE 2>/dev/null
    if [ $? -eq 1 ] ; then
        echo "checkrestart not checked yet"
    fi
    checkrestart &> $TMP_FILE  & disown 2>/dev/null
    echo "$!" > $PID_FILE
fi
