#!/bin/bash

# ============================================
# WireGuard VPN Setup Script
# Author: Auto-generated
# ============================================

set -e  # Exit on error

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

log()    { echo -e "${GREEN}[✓]${NC} $1"; }
warn()   { echo -e "${YELLOW}[!]${NC} $1"; }
error()  { echo -e "${RED}[✗]${NC} $1"; exit 1; }
info()   { echo -e "${BLUE}[i]${NC} $1"; }

# ============================================
# Check root
# ============================================
if [[ $EUID -ne 0 ]]; then
  error "Script ini harus dijalankan sebagai root. Gunakan: sudo bash $0"
fi

info "Memulai instalasi WireGuard VPN..."

# ============================================
# Update & Install
# ============================================
info "Update & upgrade sistem..."
apt update -y && apt upgrade -y
log "Sistem berhasil diupdate"

info "Menginstall wireguard, openresolv, curl..."
apt install -y wireguard openresolv curl
log "Package berhasil diinstall"

# ============================================
# Buat konfigurasi WireGuard
# ============================================
info "Membuat konfigurasi /etc/wireguard/wg0.conf..."

mkdir -p /etc/wireguard
chmod 700 /etc/wireguard

cat > /etc/wireguard/wg0.conf <<EOF

EOF

chmod 600 /etc/wireguard/wg0.conf
log "Konfigurasi berhasil dibuat"

# ============================================
# Enable & Start WireGuard
# ============================================
info "Mengaktifkan service wg-quick@wg0..."
systemctl enable wg-quick@wg0
log "Service berhasil di-enable"

info "Menjalankan service wg-quick@wg0..."
systemctl start wg-quick@wg0
log "Service berhasil dijalankan"

# ============================================
# Status
# ============================================
echo ""
echo -e "${BLUE}======================================${NC}"
echo -e "${BLUE}       STATUS WIREGUARD VPN           ${NC}"
echo -e "${BLUE}======================================${NC}"
systemctl status wg-quick@wg0 --no-pager

echo ""
info "Verifikasi koneksi tunnel:"
wg show

echo ""
log "Setup WireGuard VPN selesai!"
info "Tunnel aktif di: 10.8.0.4/24 -> mas.zmutclik.my.id:51820"