#!/bin/bash
. /etc/openvpn/login/config.sh

dt=$(date +'%Y-%m-%d %H:%M:%S')

# 1. Update bandwidth logs 
# We target the 'online' status for this specific user to clear the session
mysql -u "$USER" -p"$PASS" -D "$DB" -h "$HOST" -e "UPDATE bandwidth_logs SET bytes_received='$bytes_received', bytes_sent='$bytes_sent', time_out='$dt', status='offline' WHERE username='$common_name' AND status='online' ORDER BY id DESC LIMIT 1"

# 2. Reset user status in the main table
mysql -u "$USER" -p"$PASS" -D "$DB" -h "$HOST" -e "UPDATE users SET is_connected='0', device_connected='0', active_address='', active_date='' WHERE user_name='$common_name'"