#!/bin/bash
# oca-install.sh
# GPLv3+
# OCB 11 worked, no OCA modules.
# Is OCB best git repo to use as base ??? XXX
#
set -x

apt-get install \
        postgresql-all

# XXX set up postgres config.
sudo su - postgres -c "createuser -s oca"
sudo -u postgres psql -c "CREATE USER oca WITH password 'XXX';"
sudo -u postgres psql -c "ALTER USER oca CREATEDB;"

sudo -u postgres createuser odoo -U postgres -dRSP

#sudo adduser --system --home=/opt/oca --group oca
sudo adduser --system --home=/opt/odoo --group odoo

sudo mkdir /var/log/oca
sudo mkdir /var/log/odoo

# uh, add to /root/.bashrc XXX
#export PYTHONPATH=/opt/odoo

# XXX Oh boy
root@ns32:/opt/odoo# pip install -e .

# if you install gis module
sudo apt-get install postgis


sudo git clone https://www.github.com/odoo/odoo --depth 1 --branch 9.0 --single-branch /opt/odoo
#sudo git clone https://www.github.com/odoo/odoo --depth 1 --branch 10.0 --single-branch /opt/odoo

sudo chown -R odoo: /opt/odoo/
sudo chown odoo:root /var/log/odoo
sudo chown odoo: /etc/odoo-server.conf
sudo chmod 640 /etc/odoo-server.conf

# 9, get openerp-server for /etc/init.d/

# 10+
sudo systemctl start odoo-server
sudo systemctl status odoo-server


apt-get install				\
	python-pip			\
	libxml2-dev python-libxml2	\
	libxslt1-dev			\
	cython				\
	libldap2-dev			\
	zlib1g-dev			\
	libgsasl7-dev libsasl2-dev	\
	libevent-dev			\
        python-babel python-decorator python-docutils python-feedparser python-gevent python-greenlet python-html2text python-jinja2 python-lxml python-mako python-markupsafe python-mock python-ofxparse python-passlib python-pil python-psutil python-psycopg2 python-pydot python-pyldap python-pyparsing python-pypdf2 python-serial python-dateutil python-arrow python-usb python-yaml python-qrcode python-reportlab python-requests python-six python-suds python-vatnumber python-vobject python-werkzeug python-xlsxwriter python-xlwt python-xlrd wkhtmltopdf python-pdfkit


# Build deps when building from source/git:
# XXX installs everything that matches node-*, because I'm not sure what all is needed
# These are python3 versions:
apt-get install				\
	python3-pip			\
	libxml2-dev python3-libxml2	\
	libxslt1-dev			\
	cython3				\
	libldap2-dev			\
	python3-pyldap python3-ldap3	\
	zlib1g-dev			\
	libgsasl7-dev libsasl2-dev	\
	libevent-dev			\
        python3-babel python3-decorator python3-docutils python3-feedparser python3-gevent python3-greenlet python3-html2text python3-jinja2 python3-lxml python3-mako python3-markupsafe python3-mock python3-ofxparse python3-passlib python3-pil python3-psutil python3-psycopg2 python3-pydot python3-pyldap python3-pyparsing python3-pypdf2 python3-serial python3-dateutil python3-arrow python3-usb python3-yaml python3-qrcode python3-reportlab python3-requests python3-six python3-suds python3-vatnumber python3-vobject python3-werkzeug python3-xlsxwriter python-xlwt python3-xlrd wkhtmltopdf python3-pdfkit

#	`apt-cache search node | grep ^node- |cut -f 1 -d " "`	\
#	nodeenv nodejs nodejs-dev nodejs-legacy			\
#	node-read-package-json node-normalize-package-data
#	nodejs nodejs-legacy		\

# Per: https://www.linode.com/docs/websites/cms/install-odoo-10-on-ubuntu-16-04 adapted for Debian
apt-get install git python-pip postgresql postgresql-server-dev-all python-all-dev python-dev python-setuptools libxml2-dev libxslt1-dev libevent-dev libsasl2-dev libldap2-dev pkg-config libtiff5-dev libjpeg-dev zlib1g-dev libfreetype6-dev liblcms2-dev liblcms2-utils libwebp-dev tcl8.6-dev tk8.6-dev python-tk libyaml-dev fontconfig


apt-get clean

# XXX fails with some mercurial thing or similar, idk
# maybe just for 11+ ?
sudo pip3 install -r /opt/odoo/doc/requirements.txt
sudo pip3 install -r /opt/odoo/requirements.txt

# For 2.7 versions
sudo pip install -r /opt/odoo/doc/requirements.txt
sudo pip install -r /opt/odoo/requirements.txt

# Install npm, not in Debian
# XXX Doesn't work, known bug:
# https://github.com/npm/npm/issues/18524
#curl -L https://npmjs.org/install.sh | sh

# curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -

curl -sL https://deb.nodesource.com/setup_8.x | bash -
apt-get install -y nodejs
#npm install -g less
npm install -g less less-plugin-clean-css

cp /opt/odoo/debian/odoo.conf /etc/odoo-server.conf

vim /lib/systemd/system/odoo-server.service
# doesn't want execute bits
#chmod 755 /lib/systemd/system/odoo-server.service
chown root: /lib/systemd/system/odoo-server.service


### Addons and Modules
#
# As jebba...
#git clone https://github.com/OCA/maintainer-tools.git

# Note, this is 2.2 gigs
git clone https://github.com/OCA/OCB.git
cd OCB/
#git checkout remotes/origin/10.0
git checkout remotes/origin/9.0
pip3 install -r requirements.txt

cp -p debian/odoo.conf ~/oca.conf
# Change username to oca
vim ~/odoo.conf

# First time to init new modules ??? XXX
./odoo-bin --init=all -d oca2 --config=/home/jebba/oca.conf

# XXX auto
./odoo-bin					\
	-d oca2					\
	--config=/home/jebba/odoo.conf

# On workstation, create tunnel:
ssh -N -C -L 8069:localhost:8069 ns32
# and open in browser:
# http://localhost:8069

# XXX, deps below fail.
#sudo pip install wand
apt install python-simplejson
#apt install python3-simplejson
# as jebba, on server:
mkdir OCA
cd OCA/
for i in `cat ../oca11-addons`
 do git clone $i
done

pip uninstall odoo9-addon-server-environment odoo9-addon-server-environment-files-sample odoo9-addon-server-environment-ir-config-parameter

# NO:
# HMM, to autofind modules for OCA.
# As root, or not:
export PIP_FIND_LINKS="https://wheelhouse.odoo-community.org/oca"
# Fails as jebba because wants to write to /usr/local
# XXX TOTAL KLUDGE:
sudo chown -R jebba /usr/local
# run as jebba:
pip3 install odoo-autodiscover
pip3 install setuptools-odoo
# uh, as root, odoo 9
pip install odoo-autodiscover
pip install setuptools-odoo

# Meh, odoo-autodiscover says this command must work:
python -c "import odoo.api"
# But this does:
python3 -c "import odoo.api"


# For OCA modules:
# still fails.
apt install libminizip1 minizip

# hw_scanner module needs libevdev2 (?) installed. Maybe also:
# python-evemu - Linux Input Event Device Emulation Library - Python bindings
# python3-evemu - Linux Input Event Device Emulation Library - Python3 bindings
#apt install python-evemu python3-evemu

# Nope:
hw_scanner
connector_prestashop
mail_environment
server_environment_files

# To install postgis stuff add to db:
sudo su - postgres
psql
CREATE EXTENSION postgis;
CREATE EXTENSION postgis_topology;

