readonly POP_CHECK_DATA="${BACKGROUND_DATA}/pop_check.data";
readonly POP_CHECK_ERRORS="${BACKGROUND_DATA}/pop_check.errors";

pop_check_main(){
        local pop_initial_status="$(/scripts/restartsrv_pop3 --status 2>&1)";
        local pop_restart;

        declare -A pop_data;

        local disabled="$(grep pop:1 /etc/chkserv.d/chkservd.conf)";
        local inactive="service is down";
        local active="is running";

        if [[ -z "$disabled" ]]; then
                pop_data[service_status]="$SERVICE_DISABLED";
        elif [[ "$pop_initial_status" =~ $active ]]; then
                pop_data[service_status]="$SERVICE_ACTIVE";
        else
                /scripts/restartsrv_pop3 --start 1>/dev/null 2>&1;

                pop_restart="$(/scripts/restartsrv_pop3 --status 2>&1)";

                if [[ "$pop_restart" =~ "$inactive" ]]; then
                        pop_data[service_status]="$SERVICE_DOWN";
                        pop_data[auto_fix_status]="$AUTO_FIX_FAIL";
                else
                        pop_data[service_status]="$SERVICE_ACTIVE";
                        pop_data[auto_fix_status]="$AUTO_FIX_SUCCESS";
                fi;
        fi;

        pop_data[enabled_protocols]="$(grep "protocols =" /etc/dovecot/dovecot.conf | sed -E 's/protocols =[ ]?//')";

        local ports="110 995";
        for p in $ports; do
                local port="$(netstat -lntp | grep ":$p " | grep "tcp ")";
                if [[ -n "$port" ]]; then
                        pop_data["$p"]="$port";
                fi;
        done;

        bash_arr_to_json pop_data "${!pop_data[@]}" > "$POP_CHECK_DATA";
}

pop_check(){
        run_check_in_background "pop_check_main" "$POP_CHECK_DATA" "$POP_CHECK_ERRORS"\
                "${CHECK_LOCKS_DIR}/${FUNCNAME}.lock";
}
