#!/usr/bin/env python3
import secrets, ipaddress, pathlib, os
root=pathlib.Path(__file__).resolve().parents[1]
file=root/'.env'
if file.exists():raise SystemExit('.env already exists; refusing to overwrite')
net=ipaddress.ip_network(os.getenv('ITNA_VPN_NETWORK','10.77.0.0/24'),strict=False)
ip=ipaddress.ip_address(os.getenv('ITNA_VPN_SERVER_IP','10.77.0.1'))
if ip not in net or not ip.is_private:raise SystemExit('Private VPN address must belong to VPN network')
def token(n=40):return secrets.token_urlsafe(n)
lines=[f'DB_ROOT_PASSWORD={token()}',f'DB_PASSWORD={token()}',f'RADIUS_CLIENT_NET={net}',f'RADIUS_CLIENT_SECRET={token()}',f'VPN_SERVER_IP={ip}','SYNC_INTERVAL=30']
file.write_text('\n'.join(lines)+'\n')
os.chmod(file,0o600)
print('Generated .env with restricted mode; keep it off public web root.')
