log_format debug_log_log 'test ${document_root} $http_user_agent';
access_log /var/log/nginx-debug.log debug_log_log;
error_log /var/log/nginx-error.log notice;
rewrite_log on;

server {
	listen 80;

	root   /Users/notplants/computer/projects/lichen-with-markdown;
	#include mime.types;
	index index.gmi;

	server_name lichen2.local;

	location / {

	  	# --- discard .gmi extensions and redirect
		rewrite (.*)\.gmi$ $1 permanent;
		# --- try .gmi files without extensions
		if (-f ${document_root}${uri}.gmi) {
			set $redirect_url ${uri}.gmi;
			set $is_redirected true;
			set $new_url /cms/render.php?${redirect_url};
			#add_header Content-Type text/plain;
            #return 200 $new_url;
			rewrite ^(.*)$ $new_url last;
		}
		# --- try index.gmi 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;
		}
        # --- try .md files without extensions
        if (-f ${document_root}${uri}.md) {
            set $redirect_url ${uri}.md;
            set $is_redirected true;
            set $new_url /cms/render.php?${redirect_url};
            #add_header Content-Type text/plain;
            #return 200 $new_url;
            rewrite ^(.*)$ $new_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} ${uri}/ =404;
	}

	location /cms/ {

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

                #add_header Content-Type text/plain;
                #return 200 $fastcgi_script_name;

                fastcgi_pass 127.0.0.1:9000;
                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;
	}
}
