#!/bin/bash

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;93m'
MAGENTA='\033[0;35m'
NC='\033[0m' # No Color

config_dir="$HOME/.config/uncom/"
logfile="$config_dir/check_size.log"

. /usr/share/appdata/check-syslog-size.conf

function log() {
    echo $1 >> $logfile
}

function show_alert() {
    log "Show alert message"
    userID=`env | grep XAUTHORITY | awk -Fuser/ '{print $NF}' | awk -F\/ '{print $1}'`
    userName=`cat /etc/passwd | grep $userID | awk -F: '{print $1}'`
    log "userID = $userID ; userName = $userName; problem = $1; log = $2"
    log ""
    sudo -u $userName uncom-check-syslog-dialog -g --problem $1 --log $2
    exit 0
}

echo -e "${GREEN}Checking syslog size${NC}"

# Check for config dir
[[ -d $config_dir ]] || {
    echo -e "${GREEN}Creating config directory${NC}"
    mkdir -p $config_dir
    echo "size: 0" > $config_dir/size_old
    echo "date:" `date +%s` >> $config_dir/size_old
    echo "date_human:" `date +%s` >> $config_dir/size_old
    log "Creating config directory:" `date`
}

# Start of the check
date_now=`date +%s`
date_now_human=`date`
log "Check date: $date_now_human"

# Analytics
problem="undefined"
log="undefined"

#################################
# Check /var/log directory size #
#################################

size_varlog=`du -s -m /var/log | awk '{print $1}'`
if [[ $size_varlog -lt $varlog_limit ]]
then echo -e "${GREEN}/var/log size is OK${NC}"
    log "size_varlog = $size_varlog, is OK"
else echo -e "${RED}/var/log size $size_varlog is bigger than $varlog_limit (Mb), proceed${NC}"
    log "/var/log size $size_varlog is bigger then $varlog_limit (Mb), show alert triggerred"
    problem="size_varlog"
    log=`du -s -m /var/log/* | sort -rh | head -1 | awk '{print $NF}'`
    if [[ -f $HOME/.config/uncom/dont_show_check_warning ]]
    then echo -e "${GREEN}Alert file exists${NC}"
        . $HOME/.config/uncom/dont_show_check_warning
        if [ "xx$size_varlog_disabled" == "xxtrue" ]
        then log "Alert is disabled for size_varlog, proceed with syslog"
            # We set problem to undefined to check syslog growth speed
            problem="undefined"
        fi
    fi
fi

##################################
# Check syslog size growth speed #
##################################

# Only if size_varlog is ok
if [ "$problem" == "undefined" ]
then
    size_now=`du -m /var/log/syslog | awk '{print $1}'`
    size_old=`cat $config_dir/size_old | grep "size:" | awk '{print $NF}'`
    date_old=`cat $config_dir/size_old | grep "date:" | awk '{print $NF}'`
    date_old_human=`cat $config_dir/size_old | grep "date_human:" | awk '{print $NF}'`

    size_diff=`expr $size_now \- $size_old`

    echo -e "${GREEN}Previous size: ${MAGENTA} $size_old Mb ${NC}"
    echo -e "${GREEN}Current size:  ${MAGENTA} $size_now Mb ${NC}"
    echo
    echo -e "${GREEN}Diff:  ${MAGENTA} $size_diff Mb ${NC}"
    echo -e "${GREEN}Limit: ${MAGENTA} $daily_grow Mb ${NC}"

    log "Previous size: $size_old Mb"
    log "Current size:  $size_now Mb"
    log "Diff:  $size_diff Mb"
    log "Limit: $daily_grow Mb"

    echo "size: $size_now" > $config_dir/size_old
    echo "date: $date_now" >> $config_dir/size_old
    echo "date_human: $date_now_human" >> $config_dir/size_old

    if [[ $size_diff -lt $daily_grow ]]
    then echo -e "${GREEN}OK, exit${NC}"
        log ""
    else echo -e "${RED}Growing too fast, proceed${NC}"
        log "Growing too fast, show alert triggerred"
        problem="unknown"

        # Check nouveau driver problem: number of strings > 10000
        nouveau_hits=`grep "nouveau" /var/log/syslog | grep DATA_ERROR | wc -l`
        [ $nouveau_hits -gt 10000 ] && {
            nouveau="true"
            problem="nouveau"
}

        # Check AER PCI problem: number of strings > 10000
        aer_hits=`grep "AER: Correctable error message received" /var/log/syslog | wc -l`
        [ $aer_hits -gt 10000 ] && {
            aer="true"
            problem="aer"
}

        # Check vkteams problem: number of strings > 10000
        vkteams_hits=`grep "vkteamsdesktop.desktop" /var/log/syslog | wc -l`
        [ $vkteams_hits -gt 10000 ] && {
            vkteams="true"
            problem="vkteams"
}

        # Check yandex browser problem: number of strings > 10000
        ybrowser_hits=`grep "yandex-browser.desktop" /var/log/syslog | wc -l`
        [ $ybrowser_hits -gt 10000 ] && {
            ybrowser="true"
            problem="ybrowser"
}
    fi

fi

if [ "$problem" == "undefined" ]
then echo -e "${GREEN}Everything is ok, exiting${NC}"
else # Check if "Dont show again" was selected
    if [[ -f $HOME/.config/uncom/dont_show_check_warning ]]
    then echo -e "${GREEN}Alert file exists${NC}"
        . $HOME/.config/uncom/dont_show_check_warning
        if [ "xx$problem" == "xxsize_varlog" ] && [ "xx$size_varlog_disabled" == "xxtrue" ]
        then log "Alert is disabled"
            log ""
            exit 0
        fi
        if [ "xx$nouveau" == "xxtrue" ] && [ "xx$nouveau_disabled" == "xxtrue" ]
        then log "Alert is disabled"
            log ""
            exit 0
        fi
        if [ "xx$aer" == "xxtrue" ] && [ "xx$aer_disabled" == "xxtrue" ]
        then log "Alert is disabled"
            log ""
            exit 0
        fi
        if [ "xx$vkteams" == "xxtrue" ] && [ "xx$vkteams_disabled" == "xxtrue" ]
        then log "Alert is disabled"
            log ""
            exit 0
        fi
        if [ "xx$ybrowser" == "xxtrue" ] && [ "xx$ybrowser_disabled" == "xxtrue" ]
        then log "Alert is disabled"
            log ""
            exit 0
        fi
    else # No file - create it
        [ -d $HOME/.config/uncom/ ] || mkdir -p $HOME/.config/uncom/
        touch $HOME/.config/uncom/dont_show_check_warning
    fi

    show_alert $problem $log
fi
