#!/bin/sh

if [ $(id -u) != "0" ]; then
    echo "Error: Please use root role to install!"
    exit 1
fi

clear
echo "========================================"
echo ""
echo "========================================"
echo "======= nginx_rtmp one key sctipt ======"
echo "========================================"
echo ""
echo "========================================"

get_char()
{
SAVEDSTTY=`stty -g`
stty -echo
stty cbreak
dd if=/dev/tty bs=1 count=1 2> /dev/null
stty -raw
stty echo
stty $SAVEDSTTY
}
echo ""
#echo "Press any key to start...or Press Ctrl+c to cancel"
#char=`get_char`

echo "========================================"
echo "Start install nginx_rtmp ..."
echo "========================================"
yum -y install gcc gcc-c++ autoconf automake
yum -y install zlib zlib-devel openssl openssl-devel pcre-devel

cur_dir=$(pwd)

cd nginx-1.8.0

echo "========================================"
echo "configure start ..."
echo "========================================"

 ./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--error-log-path=/usr/local/nginx/logs/error.log \
--add-module=../nginx-rtmp-module \
--with-http_mp4_module

echo "========================================"
echo "make &install"
echo "========================================"

make
make install

echo "========================================"
echo "Setting start"
echo "========================================"

cd $cur_dir
\cp nginx.conf /usr/local/nginx/
\cp nginx-rtmp-module/stat.xsl /usr/local/nginx/html/
\cp nginx /etc/rc.d/init.d/
chmod +x /etc/rc.d/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
service nginx start
echo "Service Test:"
echo "service nginx status"
echo "Result:"
service nginx status

#cat > /etc/fstab <<EOF
#tmpfs /usr/local/nginx/html/app tmpfs defaults,size=512M 0 0
#EOF
#mount -a
#echo "tmpfs Test:"
#df -h
mount -t tmpfs -o size=512m tmpfs /usr/local/nginx/html/app
echo "mount -t tmpfs -o size=512m tmpfs /usr/local/nginx/html/app" >> /etc/rc.d/rc.local


echo "========================================"
echo "Firewall config"
echo "========================================"

iptables -I INPUT -p tcp --dport 6080 -j ACCEPT
iptables -I INPUT -p tcp --dport 5080 -j ACCEPT
iptables -I INPUT -p tcp --dport 1935 -j ACCEPT
service iptables save

echo "========================================"
echo "Install end."
echo "========================================"

  nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error.log  info;

#pid        logs/nginx.pid;
worker_rlimit_nofile 51200;

events {
    use epoll;
    worker_connections  1024;
}

rtmp_auto_push on;

rtmp_auto_push_reconnect 1s;

rtmp {
    server {
		listen 1935;
		chunk_size 4096;
		application live {
			live on;
			max_connections 1024;
		}
		application hls {
			live on;
			hls on;
			hls_path /usr/local/nginx/html/app;
			hls_fragment 3s;
			hls_playlist_length 30s;
			hls_sync 100ms;			
			meta copy;
			recorder chunked {
				record all;
				#record_max_size 6200K;
				record_interval 10s;
				record_suffix -%Y-%m-%d-%H_%M_%S.flv;
				record_path /data/www/Upload/Rec/Chunked;
			}
			recorder all {
	                        record all;
	                        record_suffix -%Y-%m-%d-%H_%M_%S.flv;
				record_max_size 6200000K;
	                        record_path /data/www/Upload/Rec;
			}
		}
		application Upload {
			play /data/www/Upload;
		}
    }
}

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

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       5080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
        location /hls {
			types {
				#application/vnd.apple.mpegurl m3u8;
				application/x-mpegurl m3u8;
				video/mp2t ts;
			}
			alias /usr/local/nginx/html/app;
		}
        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }
        location /stat.xsl {
            root html;
        }
    }

server {
        listen       6080;
        server_name  localhost;

        location / {
            root   /data/www/AVA.ResourcesPlatform.AdminUI;
            index  index.html index.htm;

	    mp4;
	    mp4_buffer_size       1m;
	    mp4_max_buffer_size   5m;
	    #mp4_limit_rate        on;
	    #mp4_limit_rate_after  30s;
	    limit_rate 1m; # about 2mbit
	    limit_rate_after 5m;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
	#location ~ \.mp4$ {
	#}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}