upstream flyspray {
    server unix:///run/php-fpm7/flyspray.socket;
}

server {
    listen       80;
    listen       [::]:80;
    server_name  {{ flyspray_domain }};

    access_log   /var/log/nginx/{{ flyspray_domain }}/access.log reduced;
    access_log   /var/log/nginx/{{ flyspray_domain }}/access.log.json json_reduced;
    error_log    /var/log/nginx/{{ flyspray_domain }}/error.log;

    include snippets/letsencrypt.conf;

    location / {
        access_log off;
        return 301 https://$server_name$request_uri;
    }
}

map $uri $redirect {
    default "";
    include maps/migrated-tasks.map;
}

server {
    listen       443 ssl http2;
    listen       [::]:443 ssl http2;
    server_name  {{ flyspray_domain }};

    access_log   /var/log/nginx/{{ flyspray_domain }}/access.log reduced;
    access_log   /var/log/nginx/{{ flyspray_domain }}/access.log.json json_reduced;
    error_log    /var/log/nginx/{{ flyspray_domain }}/error.log;

    ssl_certificate      /etc/letsencrypt/live/{{ flyspray_domain }}/fullchain.pem;
    ssl_certificate_key  /etc/letsencrypt/live/{{ flyspray_domain }}/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/{{ flyspray_domain }}/chain.pem;

    root {{ flyspray_dir }};

    location /.git {
        deny all;
    }

    location /setup {
        deny all;
    }

    location /attachments {
        location ~ \.php$ {return 403;}
    }

    location /cache {
        location ~ \.php$ {return 403;}
    }

    location ~ ^/task/(?<task_id>[0-9]+) {
        if ($redirect != "") {
            return 302 $redirect;
        }
        rewrite ^/task/[0-9]+$ /index.php?do=details&task_id=$task_id last;
        rewrite ^/task/[0-9]+/comment(?<comment_id>[0-9]+)$ /index.php?do=details&task_id=$task_id&comment=$comment_id last;
        rewrite ^/task/[0-9]+/depends$ /index.php?do=depends&task_id=$task_id last;
        if ( $arg_prune ~ "[0-9]+" ) {
            rewrite ^ /index.php?do=depends&task_id=$task_id&prune=$arg_prune last;
        }
        rewrite ^/task/[0-9]+/edit$ /index.php?do=details&task_id=$task_id&edit=yep last;
    }

    location ~ ^/newtask {
        rewrite ^/newtask$ /index.php?do=newtask last;
        rewrite ^/newtask/proj(?<project_id>[0-9]+)$ /index.php?do=newtask&project=$project_id last;
    }

    location ~ ^/reports$ {
        rewrite ^/reports$ /index.php?do=reports last;
    }

    location ~ ^/myprofile$ {
        rewrite ^/myprofile$ /index.php?do=myprofile last;
    }

    location ~ ^/user/(?<user_id>[0-9]+)$ {
        rewrite ^/user/[0-9]+$ /index.php?do=user&id=$user_id last;
    }

    location ~ ^/logout$ {
        rewrite ^/logout$ /index.php?do=authenticate&logout=1 last;
    }

    location ~ ^/admin {
        rewrite ^/admin/(?<area_id>[a-zA-Z]+)$ /index.php?do=admin&area=$area_id last;
        rewrite ^/admin/editgroup/(?<group_id>[0-9]+)$ /index.php?do=admin&area=editgroup&id=$group_id last;
    }

    location ~ ^/pm {
        rewrite ^/pm/proj(?<project_id>[0-9]+)/(?<area_id>[a-zA-Z]+)$ /index.php?do=pm&project=$project_id&area=$area_id last;
        rewrite ^/pm/editgroup/(?<group_id>[0-9]+)$ /index.php?do=pm&area=editgroup&id=$group_id last;
    }

    location ~ ^/edituser/(?<user_id>[0-9]+)$ {
        rewrite ^/edituser/[0-9]+$ /index.php?do=admin&area=users&user_id=$user_id last;
    }

    location ~ ^/register$ {
        rewrite ^/register$ /index.php?do=register last;
    }

    location ~ ^/lostpw$ {
        rewrite ^/lostpw$ /index.php?do=lostpw last;
    }

    location ~ ^/roadmap {
        rewrite ^/roadmap$ /index.php?do=roadmap last;
        rewrite ^/roadmap/proj(?<project_id>[0-9]+)$ /index.php?do=roadmap&project=$project_id last;
    }

    location ~ ^/toplevel {
        rewrite ^/toplevel$ /index.php?do=toplevel last;
        rewrite ^/toplevel/proj(?<project_id>[0-9]+)$ /index.php?do=toplevel&project=$project_id last;
    }

    location ~ ^/proj(?<project_id>[0-9]+)$ {
        rewrite ^/proj[0-9]+$ /index.php?project=$project_id last;
    }

    location ~ ^/index$ {
        rewrite ^/index$ /index.php?do=index last;
    }

    location ~ ^/index/proj(?<project_id>[0-9]+)$ {
        rewrite ^/index/proj[0-9]+$ /index.php?do=index&project=$project_id last;
    }

    location ~ \.php$ {
        access_log   /var/log/nginx/{{ flyspray_domain }}/access.log main;
        access_log   /var/log/nginx/{{ flyspray_domain }}/access.log.json json_main;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        try_files    $uri =404;
        include fastcgi_params;
        fastcgi_pass flyspray;
    }
}
