#!/bin/bash
# webdav.4fsake.me nginx 설정 - location / 을 rclone WebDAV 프록시로 교체

sed -i '/server_name webdav\.4fsake\.me;/,/listen \[\:\:\]:443 ssl;/{
    s|location / {.*||
    /try_files \$uri \$uri\/ =404;/d
}' /etc/nginx/sites-enabled/default

# location / 블록 교체
python3 - <<'EOF'
import re

with open('/etc/nginx/sites-enabled/default', 'r') as f:
    content = f.read()

old = """	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files $uri $uri/ =404;
	}"""

new = """	location / {
		proxy_pass http://localhost:8888/;
		proxy_set_header Host $http_host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header X-Forwarded-Proto $scheme;
		proxy_buffering off;
	}"""

content = content.replace(old, new)

with open('/etc/nginx/sites-enabled/default', 'w') as f:
    f.write(content)

print("완료")
EOF

nginx -t && systemctl reload nginx
echo "nginx 재로드 완료"
