#!/bin/bash

apt-get update -y >> /var/log/odoo15_install.log 2>&1
apt-get install -y xfonts-75dpi git python3 python3-pip python3-dev python3-venv python3-wheel python3-setuptools node-less build-essential libsasl2-dev libjpeg-dev gdebi libzip-dev libldap2-dev libxslt-dev libssl-dev libffi-dev libxml2-dev libxslt1-dev zlib1g-dev libsasl2-dev libldap2-dev postgresql postgresql-server-dev-all npm nginx jq net-tools >> /var/log/odoo15_install.log 2>&1
apt --fix-broken install >> /var/log/odoo15_install.log 2>&1
wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.5/wkhtmltox_0.12.5-1.bionic_amd64.deb >> /var/log/odoo15_install.log 2>&1
dpkg -i wkhtmltox_0.12.5-1.bionic_amd64.deb >> /var/log/odoo15_install.log 2>&1
apt install -f -y >> /var/log/odoo15_install.log 2>&1
cp /usr/local/bin/wkhtmltoimage  /usr/bin/wkhtmltoimage
cp /usr/local/bin/wkhtmltopdf  /usr/bin/wkhtmltopdf

# create Odoo user and directory
su - postgres -c "createuser -s odoo"
useradd -m -U -r -d /opt/odoo -s /bin/bash odoo

# download odoo from github
su - odoo -s /bin/bash -c "git clone https://www.github.com/odoo/odoo --depth 1 --branch 15.0 --single-branch" >> /var/log/odoo15_install.log 2>&1

# create Python Virtual environment and install dependencies
su - odoo -s /bin/bash -c "/usr/bin/python3 -m venv /opt/odoo/odoo/venv"
su - odoo -s /bin/bash -c "/opt/odoo/odoo/venv/bin/pip install --upgrade pip" >> /var/log/odoo15_install.log 2>&1
su - odoo -s /bin/bash -c "/opt/odoo/odoo/venv/bin/pip install -r /opt/odoo/odoo/requirements.txt" >> /var/log/odoo15_install.log 2>&1

# config Odoo
su - odoo -s /bin/bash -c "mkdir /opt/odoo/config"
cat <<-'EOF' > /opt/odoo/config/odoo.conf
[options]
admin_passwd = password-master
db_host = False
db_port = False
db_user = odoo
db_password = False
addons_path = /opt/odoo/odoo/addons
proxy_mode = True
; csv_internal_sep = ,
; db_maxconn = 64
; db_name = False
; db_template = template1
; dbfilter = ^%d$
; debug_mode = False
; email_from = False
workers = 2
max_cron_threads = 1
; list_db = True
; log_db = False
; log_handler = [':INFO']
; log_level = info
; logfile = None
longpolling_port = 8072
; osv_memory_age_limit = 1.0
; osv_memory_count_limit = False
; smtp_password = False
; smtp_port = 25
; smtp_server = localhost
; smtp_ssl = False
; smtp_user = False
; xmlrpc = True
; xmlrpc_interface = 
; xmlrpc_port = 8069
; xmlrpcs = True
; xmlrpcs_interface = 
; xmlrpcs_port = 8071
without_demo = True
demo = {}
# server_wide_modules = base,web,crm,account
server_wide_modules = None

EOF

#password=`echo $1|jq .password|sed 's/"//g'`
password=`echo $1|base64 -d|jq .password|sed 's/"//g'`
sed -i s/password-master/$password/g /opt/odoo/config/odoo.conf

# Set permissions
chown -R odoo: /opt/odoo/

# config Nginx
cat <<-'EOF' > /etc/nginx/sites-available/default
# Odoo servers
upstream odoo {
 server 127.0.0.1:8069;
}

upstream odoochat {
 server 127.0.0.1:8072;
}

# HTTP -> HTTPS
server {
    listen 80;
    server_name localhost;
    proxy_read_timeout 720s;
    proxy_connect_timeout 720s;
    proxy_send_timeout 720s;

    # Proxy headers
    proxy_set_header X-Forwarded-Host \$host;
    proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto \$scheme;
    proxy_set_header X-Real-IP \$remote_addr;

    # log files
    access_log /var/log/nginx/odoo.access.log;
    error_log /var/log/nginx/odoo.error.log;

  # Redirect websocket requests to odoo gevent port
    location /websocket {
       proxy_pass http://odoochat;
       proxy_set_header X-Forwarded-Host \$host;
       proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
       proxy_set_header X-Forwarded-Proto \$scheme;
       proxy_set_header X-Real-IP \$remote_addr;
  }

    # Handle / requests
    location / {
       proxy_redirect off;
       proxy_pass http://odoo;
    }

    # Cache static files
    location ~* /web/static/ {
        proxy_cache_valid 200 90m;
        proxy_buffering on;
        expires 864000;
        proxy_pass http://odoo;
    }

    # Gzip
    gzip_types text/css text/less text/plain text/xml application/xml application/json application/javascript;
    gzip on;
}
EOF

#get localhost IP
ip_address=$(hostname -I | awk '{print $1}')
sed -i s/localhost/${ip_address}/g /etc/nginx/sites-available/default

# restart Nginx
systemctl restart nginx >> /var/log/odoo15_install.log 2>&1

# touch Odoo config file
cat <<-'EOF' > /etc/systemd/system/odoo.service
[Unit]
Description=Odoo
Documentation=http://www.odoo.com
[Service]
# Ubuntu/Debian convention:
EnvironmentFile=/etc/environment
Environment=PATH=/opt/odoo/odoo/venv/bin:/usr/local/bin:/usr/bin:/bin
ExecStart=/opt/odoo/odoo/venv/bin/python3 /opt/odoo/odoo/odoo-bin -c /opt/odoo/config/odoo.conf
User=odoo
Type=simple
[Install]
WantedBy=default.target
EOF

# reload systemd config
systemctl daemon-reload >> /var/log/odoo15_install.log 2>&1

# start Odoo
systemctl enable odoo >> /var/log/odoo15_install.log 2>&1
systemctl start odoo >> /var/log/odoo15_install.log 2>&1

sleep 30
status=`netstat -untpl|grep 8069`
if [ -z "$status" ]; then
    echo "Odoo15 install failed!"
else
    echo "Odoo15 install successful!"
fi

