#! /bin/sh
IN_WORK_FLAG=/tmp/in_work

sleep 1
if test ! -f $IN_WORK_FLAG
  then
    touch $IN_WORK_FLAG

    #FAILED_LOGIN_USERS_TABLE=/tmp/failed_logined_users_table
    #FAILED_LOGIN_USERS_EXISTS_FLAG=/tmp/failed_login_exists
    LOGINED_USERS_TABLE=/tmp/logined_users_table
    LOGINED_USERS_TABLE_EXISTS=/tmp/logined_users_exists
    
    
    TMP=/tmp/sorm_evnt_tmp
    empty="1"

    rm $TMP
    last -a -w -F|grep "still logged in" | grep    "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" | awk '{print $1 ":" $11 ":" $5 ":" $6}'| awk -F ":" '{print ":::succ:" $1 "@" $2 ";"    $3 "d" $4 "h" $5"m" $6 "s:::" }' >> $TMP
    last -a -w -F|grep "still logged in" | grep -v "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" | awk '{print $1 ":" $11 ":" $5 ":" $6}'| awk -F ":" '{print ":::succ:" $1 "@localhost;" $3 "d" $4 "h" $5"m" $6 "s:::" }' >> $TMP
    cat $TMP |uniq -u > $LOGINED_USERS_TABLE
    count=`cat $LOGINED_USERS_TABLE |wc -c`
    if [ $count -le $empty ]
    then
     rm $LOGINED_USERS_TABLE_EXISTS
    else
     touch $LOGINED_USERS_TABLE_EXISTS
    fi
  
    #rm $TMP
    #lastb -a -w -F |grep -v "^$" |grep -v "btmp begins"| grep    "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" | awk '{print $1 ":" $15 ":" $5 ":" $6}'| awk -F ":" '{print ":::fail:" $1"@"$2";" $3"d" $4"h" $5"m" $6 "s:::" }' >> $TMP
    #lastb -a -w -F |grep -v "^$" |grep -v "btmp begins"| grep -v "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" | awk '{print $1 ":" $15 ":" $5 ":" $6}'| awk -F ":" '{print ":::fail:" $1"@localhost;" $3"d" $4"h" $5"m" $6 "s:::" }' >> $TMP
    #cat $TMP |uniq -u > $FAILED_LOGIN_USERS_TABLE
    #count=`cat $FAILED_LOGIN_USERS_TABLE |wc -c`
    #if [ $count -le $empty ]
    #then
    # rm $FAILED_LOGIN_USERS_EXISTS_FLAG
    #else
    # touch $FAILED_LOGIN_USERS_EXISTS_FLAG
    #fi


    rm $IN_WORK_FLAG
  fi
