#!/usr/bin/perl -w

=head1 NAME

    mk-nginx-package-files - создание файлов для пакета с nginx

=head1 DESCRIPTION

    config, init, logrotate, zabbix, директория, 

=cut

use strict;
use warnings;

use SvnRelease::MkFiles;

my $mkfiles = SvnRelease::MkFiles->new(
    files => [
        {tmpl => 'nginx_init_d', file => "/etc/init.d/[% instance %]", perm => 0755, },
        {tmpl => 'nginx_conf', file => "/etc/nginx/[% instance %].conf[% conf ? '.' _ conf : '' %]", perm => 0644, conf => 1, },
        {tmpl => 'nginx_vhost_conf', file => "/etc/nginx/sites-available/[% instance %]-vhost.conf[% conf ? '.' _ conf : '' %]", perm => 0644, conf => 1, },
        {tmpl => 'logrotate_d', file => "/etc/yandex-logrotate.d/[% instance %]", },
        {tmpl => 'cron_d', file => "/etc/cron.d/[% instance %]", },

        {tmpl => 'debian_postinst', file => "/DEBIAN/postinst", perm => 0755, },
        {tmpl => 'debian_postrm', file => "/DEBIAN/postrm", perm => 0755, },

        {dir => "/var/log/[% instance %]/arc"},
        ],
    templates_fh => \*DATA,
    );

my ($instance, @dummy) = $mkfiles->parse_options("instance_name");
if (@dummy || $instance !~ /^[a-z0-9_\-]+$/) {
    $mkfiles->usage("Incorrect instance name");
}

$mkfiles->{vars}->{instance} = $instance;

$mkfiles->mk();

__DATA__
<< nginx_init_d
#! /bin/sh

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/nginx
NAME=[% instance %]
DESC=nginx
DAEMON_OPTS="-c /etc/nginx/$NAME.conf"

ulimit -n 16384

export LC_ALL=en_US.utf8
export LANG=en

test -x $DAEMON || exit 0

# Include nginx defaults if available
#if [ -f /etc/default/nginx ] ; then
#	. /etc/default/nginx
#fi

set -e

case "$1" in
  start)
	echo -n "Starting $DESC: "
	start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
		--exec $DAEMON -- $DAEMON_OPTS
	echo "$NAME."
	;;
  stop)
	echo -n "Stopping $DESC: "
	start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
		--exec $DAEMON
	echo "$NAME."
	;;
  restart|force-reload)
	echo -n "Restarting $DESC: "
	start-stop-daemon --stop --oknodo --quiet --pidfile \
		/var/run/$NAME.pid --exec $DAEMON
	sleep 1
	start-stop-daemon --start --quiet --pidfile \
		/var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
	echo "$NAME."
	;;
  reload)
      echo -n "Reloading $DESC configuration: "
      start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \
          --exec $DAEMON
      echo "$NAME."
      ;;
  upgrade)
      echo -n "Upgrading $DESC configuration: "
      start-stop-daemon --stop --signal USR2 --quiet --pidfile /var/run/$NAME.pid
      sleep 5
      start-stop-daemon --stop --signal QUIT --quiet --pidfile /var/run/$NAME.pid.oldbin
      echo "$NAME."
      ;;
  configtest)
      $DAEMON -t $DAEMON_OPTS
      ;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|reload|force-reload|configtest}" >&2
	exit 1
	;;
esac

exit 0

<< nginx_conf
user www-data;
worker_processes  1;
worker_rlimit_nofile 32768;

error_log  /var/log/[% instance %]/error.log;
pid        /var/run/[% instance %].pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    access_log    /var/log/[% instance %]/access.log;

    sendfile        on;

    keepalive_timeout  0;
    tcp_nodelay        on;
    server_tokens off;        

    client_body_buffer_size 1m;
    client_max_body_size 50m;

    gzip  on;
    gzip_disable "MSIE [4-6]\.";
    gzip_types application/x-javascript text/css text/plain;

    server {
        listen   [% port || 10080 %];

        include /etc/nginx/sites-available/[% instance %]-vhost.conf;
    }
     
    [% IF ssl_port %]
    # HTTPS server
    server {
        listen   [% ssl_port %];

        ssl on;
        ssl_certificate      /etc/nginx/ssl/[% instance %].crt;
        ssl_certificate_key  /etc/nginx/ssl/[% instance %].key;

        ssl_session_timeout  5m;
        ssl_session_cache  shared:SSL:32m;
    
        include /etc/nginx/sites-available/[% instance %]-vhost.conf;
    }
    [% END %]
}

<< nginx_vhost_conf
        location / {
             root   /var/www/[% instance %]/htdocs;
        }

        location ~ .svn {
            deny  all;
        }

<< logrotate_d
/var/log/[% instance %]/*.log {
    daily
    dateext
    rotate 10000
    missingok
    compress
    delaycompress
    notifempty
    sharedscripts
    create 0644 root root
    postrotate
    if [ -f /var/run/[% instance %].pid ]; then \
        if [ -x /usr/sbin/invoke-rc.d ]; then \
            /usr/sbin/invoke-rc.d [% instance %] reload > /dev/null; \
        else \
            /etc/init.d/[% instance %] reload > /dev/null; \
        fi; \
    fi;
    endscript
}


<< cron_d
MAILTO = [% admin_email || 'root@localhost' %]

# logrotate
59 23 * * * root /usr/sbin/logrotate /etc/yandex-logrotate.d/[% instance %]

<< debian_postinst
#!/bin/bash -e                                                                                                                                                                                                                   
. /usr/share/debconf/confmodule

update-rc.d -f nginx remove >/dev/null

update-rc.d [% instance %] defaults >/dev/null

<< debian_postrm
#!/bin/bash -e                                                                                                                                                                                                                   
. /usr/share/debconf/confmodule

update-rc.d -f [% instance %] remove >/dev/null

