#!/bin/bash

# nginx
rm -f /etc/nginx/sites-enabled/default
cat <<EOL > /etc/nginx/sites-available/onlyoffice-documentserver
map \$http_host \$this_host {
  "" \$host;
  default \$http_host;
}
map \$http_x_forwarded_proto \$the_scheme {
  default \$http_x_forwarded_proto;
  "" \$scheme;
}
map \$http_x_forwarded_host \$the_host {
  default \$http_x_forwarded_host;
  "" \$this_host;
}
map \$http_upgrade \$proxy_connection {
  default upgrade;
  "" close;
}
proxy_set_header Host \$http_host;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection \$proxy_connection;
proxy_set_header X-Forwarded-Host \$the_host;
proxy_set_header X-Forwarded-Proto \$the_scheme;
server {
  listen 0.0.0.0:80;
  listen [::]:80 default_server;
  server_tokens off;
  rewrite ^\/OfficeWeb(\/apps\/.*)$ /web-apps\$1 redirect;
  location / {
    proxy_pass http://localhost:8000;
    proxy_http_version 1.1;
  }
}
EOL

sudo ln -s /etc/nginx/sites-available/onlyoffice-documentserver /etc/nginx/sites-enabled/onlyoffice-documentserver

service nginx start

# rabbitmq-server
service rabbitmq-server start

# postgresql
service postgresql start
sudo -i -u postgres psql -c "CREATE USER onlyoffice WITH PASSWORD 'onlyoffice';"
sudo -i -u postgres psql -c "CREATE DATABASE onlyoffice OWNER onlyoffice;"
PGPASSWORD=onlyoffice psql -hlocalhost -Uonlyoffice -d onlyoffice -f /home/out/linux_64/onlyoffice/documentserver/server/schema/postgresql/createdb.sql

# supervisor
mkdir -p /var/log/onlyoffice/documentserver/converter
mkdir -p /var/log/onlyoffice/documentserver/docservice

cat <<EOL > /etc/supervisor/conf.d/ds-converter.conf
[program:converter]
command=/home/out/linux_64/onlyoffice/documentserver/server/FileConverter/converter
directory=/home/out/linux_64/onlyoffice/documentserver/server/FileConverter
environment=LD_LIBRARY_PATH=/home/out/linux_64/onlyoffice/documentserver/server/FileConverter/bin,NODE_ENV=development-linux,NODE_CONFIG_DIR=/home/out/linux_64/onlyoffice/documentserver/server/Common/config
stdout_logfile=/var/log/onlyoffice/documentserver/converter/out.log
stdout_logfile_backups=0
stdout_logfile_maxbytes=0
stderr_logfile=/var/log/onlyoffice/documentserver/converter/err.log
stderr_logfile_backups=0
stderr_logfile_maxbytes=0
autostart=true
autorestart=true
EOL

cat <<EOL > /etc/supervisor/conf.d/ds-docservice.conf
[program:docservice]
command=/home/out/linux_64/onlyoffice/documentserver/server/DocService/docservice
directory=/home/out/linux_64/onlyoffice/documentserver/server/DocService
environment=NODE_ENV=development-linux,NODE_CONFIG_DIR=/home/out/linux_64/onlyoffice/documentserver/server/Common/config
stdout_logfile=/var/log/onlyoffice/documentserver/docservice/out.log
stdout_logfile_backups=0
stdout_logfile_maxbytes=0
stderr_logfile=/var/log/onlyoffice/documentserver/docservice/err.log
stderr_logfile_backups=0
stderr_logfile_maxbytes=0
autostart=true
autorestart=true
EOL

cat <<EOL > /etc/supervisor/conf.d/ds.conf
[group:ds]
programs=docservice,converter
EOL

service supervisor start

tail -f /dev/null