#map $is_redirected $auth_type {
#	default "Restricted";
#	true "off";
#}
#
log_format debug_log 'test $lang $http_user_agent';
access_log /var/log/nginx-debug.log debug_log;

server {
	listen 80;
	root /srv/lichen-with-markdown;

	server_name simplecat.commoninternet.net;

# accept-language: en,en-US;q=0.8,ja;q=0.6
set $first_language $http_accept_language;
if ($http_accept_language ~* '^(.+?),') {
    set $first_language $1;
}

set $lang 'en';
if ($first_language ~* 'de') {
    set $lang 'de';
}



	include mime.types;
	index index.gmi;

	location /en/ {
		set $lang 'en';
		rewrite ^/en(.*)$ $1 last;
	}

	location /de/ {
		set $lang 'de';
		rewrite ^/de(.*)$ $1 last;
	}


	location / {
	  	# --- discard .gmi extensions and redirect
		rewrite (.*)\.gmi$ $1 permanent;

		# --- try .gmi files with language
		if (-f ${document_root}/${lang}/${uri}.gmi) {
			set $redirect_url /${lang}/${uri}.gmi;
			set $is_redirected true;
			rewrite ^(.*)$ /cms/render.php?${redirect_url} last;
		}
		# --- try .gmi files without extensions
		if (-f ${document_root}${uri}.gmi) {
			set $redirect_url ${uri}.gmi;
			set $is_redirected true;
			rewrite ^(.*)$ /cms/render.php?${redirect_url} last;
		}
		# --- try index files with language
		if (-f ${document_root}/${lang}/${uri}index.gmi) {
			set $redirect_url /${lang}/${uri}index.gmi;
			set $is_redirected true;
			rewrite ^(.*)$ /cms/render.php?${redirect_url} last;
		}
		# --- try index files
		if (-f ${document_root}${uri}index.gmi) {
			set $redirect_url ${uri}index.gmi;
			set $is_redirected true;
			rewrite ^(.*)$ /cms/render.php?${redirect_url} last;
		}
		# --- discard .md extensions and redirect
		rewrite (.*)\.md$ $1 permanent;

		# --- try .md files with language
		if (-f ${document_root}/${lang}/${uri}.md) {
			set $redirect_url /${lang}/${uri}.md;
			set $is_redirected true;
			rewrite ^(.*)$ /cms/render.php?${redirect_url} last;
		}
		# --- try .gmi files without extensions
		if (-f ${document_root}${uri}.md) {
			set $redirect_url ${uri}.md;
			set $is_redirected true;
			rewrite ^(.*)$ /cms/render.php?${redirect_url} last;
		}
		# --- try index files with language
		if (-f ${document_root}/${lang}/${uri}index.md) {
			set $redirect_url /${lang}/${uri}index.md;
			set $is_redirected true;
			rewrite ^(.*)$ /cms/render.php?${redirect_url} last;
		}
		# --- try index files
		if (-f ${document_root}${uri}index.md) {
			set $redirect_url ${uri}index.md;
			set $is_redirected true;
			rewrite ^(.*)$ /cms/render.php?${redirect_url} last;
		}

		#try_files ${uri}.${lang} ${uri}.${lang}/ $uri $uri/ =404;
		try_files ${uri}.${lang} ${uri}.${lang}/ $uri $uri/ =404;
	}

	location /cms/ {
		#auth_basic $auth_type;
		#auth_basic_user_file /srv/lichen.htpasswd;

		fastcgi_split_path_info ^(.+\.php)(/.+)$;
		set $path_info $fastcgi_path_info;

		fastcgi_pass unix:/run/php/php8.0-fpm.sock;
		include fastcgi_params;


		fastcgi_param REDIRECT_URL $redirect_url if_not_empty;
		fastcgi_param SCRIPT_FILENAME ${document_root}${fastcgi_script_name};
		fastcgi_param PATH_INFO $path_info;

		try_files $fastcgi_script_name =404;
	}

	location /theme/ {
		return 404;
	}
}