#!/usr/bin/env bash

NAME=$1

PRIVATE_KEY=~/.ssh/id_rsa
PUBLIC_KEY=~/.ssh/id_rsa.pub

DB_NAME=zdcbasedb10

echo "### Create Ubuntu 18.04 LTS VM"
multipass launch bionic --name $NAME --cpus 2 --mem 2048M --disk 12G --cloud-init ./cloud-init/zdc-odoo-init.yaml

echo "### Install wkhtmltopdf"
multipass exec $NAME -- sudo wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.bionic_amd64.deb -O /tmp/wkhtmltox_0.12.6-1.bionic_amd64.deb
multipass exec $NAME -- sudo apt install /tmp/wkhtmltox_0.12.6-1.bionic_amd64.deb -y

echo "### Add ssh private and public key"
multipass transfer $PRIVATE_KEY $NAME:/home/ubuntu/.ssh/id_rsa
multipass transfer $PUBLIC_KEY $NAME:/home/ubuntu/.ssh/id_rsa.pub
multipass transfer config/ssh_known_hosts $NAME:/home/ubuntu/.ssh/known_hosts
multipass exec $NAME -- chmod 600 /home/ubuntu/.ssh/id_rsa

echo "### Add  OCB Odoo, OCA addons"
multipass exec $NAME -- /usr/bin/git clone git@gitea.egroup.sk:development/zdc-odoo --depth 1 --branch master /home/ubuntu/odoo12
multipass exec $NAME -- /usr/bin/git clone git@gitea.egroup.sk:development/zdc-core-addons --depth 1 --branch master /home/ubuntu/custom/zdc-core-addons
multipass exec $NAME -- /usr/bin/git clone git@gitea.egroup.sk:development/zdc-oca-addons --depth 1 --branch master /home/ubuntu/custom/zdc-oca-addons
multipass exec $NAME -- /usr/bin/git clone git@gitea.egroup.sk:development/zdc-addons --depth 1 --branch master /home/ubuntu/custom/dev-addons

echo "### Install Python Environment"
multipass exec $NAME -- sudo -u ubuntu /usr/bin/python3 -m venv /home/ubuntu/venv-odoo12
multipass transfer config/venv-install.sh $NAME:/home/ubuntu/config/venv-install.sh
multipass exec $NAME -- sudo -u ubuntu /bin/bash /home/ubuntu/config/venv-install.sh

echo "### Setup OCB Odoo 12"
multipass transfer config/odoo12.conf $NAME:/home/ubuntu/config/odoo12.conf
multipass transfer config/odoo12_service.conf $NAME:/home/ubuntu/config/odoo12.service
multipass exec $NAME -- sudo cp /home/ubuntu/config/odoo12.conf /etc/odoo12.conf
multipass exec $NAME -- sudo cp /home/ubuntu/config/odoo12.service /etc/systemd/system/odoo12.service
multipass exec $NAME -- sudo systemctl daemon-reload
multipass exec $NAME -- sudo systemctl enable --now odoo12.service

echo "### Create ZDC Template DB"
multipass transfer db/zdc-db-dump-v1.sql.gz $NAME:/home/ubuntu/db/zdc-db-dump.sql.gz
multipass transfer db/zdc-db-filestore-v1.zip $NAME:/home/ubuntu/db/zdc-db-filestore.zip
multipass exec $NAME -- gzip -d /home/ubuntu/db/zdc-db-dump.sql.gz
multipass exec $NAME -- sudo -u postgres psql -c "CREATE DATABASE $DB_NAME WITH OWNER=ubuntu;"
multipass exec $NAME -- sudo -u postgres psql -d $DB_NAME -f /home/ubuntu/db/zdc-db-dump.sql
multipass exec $NAME -- mkdir -p /home/ubuntu/.local/share/Odoo/filestore/$DB_NAME
multipass exec $NAME -- unzip /home/ubuntu/db/zdc-db-filestore.zip -d /home/ubuntu/.local/share/Odoo/filestore/$DB_NAME

echo "### Check VM"
multipass list  | grep $NAME
URL_IP=`multipass info $NAME | grep IPv4 | sed -e 's/ //g' -e '/^$/d' | sed -e 's/IPv4://g' -e '/^$/d'`

echo "###"
echo "### Odoo database manager on $NAME server: http://$URL_IP:8069/web/database/selector"
echo "###"

open http://$URL_IP:8069/web/
