#!/usr/bin/env bash

set -x
echo "Start...";

cd ~ && sudo rm -rf monterossa && \
sudo apt update && sudo apt-get update && \
sudo apt install build-essential -y && \
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash - && \
sudo apt-get install -y nodejs && \
sudo npm i -g npm && \
node -v && npm -v && \
cd ~ && sudo mkdir -p monterossa && \
mkdir -p monterossa/shared && \
mkdir -p public-dir && \
sudo npm -g config set user root && \
sudo npm -g install gulp && \
sudo npm install pm2 -g && \
sudo apt-get install atop && \

read -p "Enter Username for git repository : " username
read -sp "Enter Password for git repository : " password

echo "Cloning iarchiv-client" && \
cd ~/monterossa && \
sudo git clone http://$username:$password@gitlab.elinext.com/iArchive/iarchiv-client.git && \
cd ~/monterossa/iarchiv-client/ && \
sudo git checkout -b unq/dev origin/unq/dev && \
sudo chmod -R 777 /home/ubuntu/.npm && \
sudo chown -R $USER:$(id -gn $USER) /home/ubuntu/.config && \
npm -g config set user root && \
sudo npm install && \
sudo mkdir -p logs && \

echo "Cloning iarchiv-server" && \
cd ~/monterossa && \
sudo git clone http://$username:$password@gitlab.elinext.com/iArchive/iarchiv-server.git && \
cd ~/monterossa/iarchiv-server/ && \
sudo git checkout -b unq/dev origin/unq/dev && \
sudo apt install node-gyp -y && \
sudo npm install && \
sudo mkdir -p logs && \

echo "Cloning iarchiv-pdf-worker" && \
cd ~/monterossa && \
sudo git clone http://$username:$password@gitlab.elinext.com/iArchive/iarchiv-pdf-worker.git && \
cd ~/monterossa/iarchiv-pdf-worker/ && \
sudo git checkout -b unq/dev origin/unq/dev && \
sudo apt install node-gyp -y && \
sudo npm install && \
# sudo apt upgrade -y && \
sudo mkdir -p filestorage && \
sudo mkdir -p filestorage/input && \
sudo mkdir -p filestorage/output  && \
sudo mkdir -p logs && \

echo "Cloning iarchiv-unified-storage" && \
cd ~/monterossa && \
sudo git clone http://$username:$password@gitlab.elinext.com/iArchive/iarchiv-unified-storage.git && \
cd ~/monterossa/iarchiv-unified-storage/ && \
sudo git checkout -b release origin/release && \
sudo rm -rf build && \
sudo npm install && \
echo "Mount disk space to unified-storage"
sudo mkdir documents-storage && \
sudo mount /dev/vdb documents-storage/ && \
sudo mkdir -p logs && \

echo "Installing LibreOffie" && \
cd ~ && sudo add-apt-repository ppa:libreoffice/ppa -y && \
sudo apt-get update -y && \
sudo apt install libreoffice --allow-unauthenticated -y && \
sudo apt-get install libreoffice-gnome && \

echo "Installing pdftk" && \
cd ~ && sudo apt install git default-jdk-headless ant libcommons-lang3-java libbcprov-java -y && \
sudo apt autoremove -y && \
sudo apt install default-jre-headless libcommons-lang3-java libbcprov-java && \
sudo apt install gdebi -y && \
sudo apt-get install gcc-6-base && \
sudo snap install pdftk-smoser && \
cd /usr/local/bin && \
sudo rm -f pdftk && \
sudo ln -s /snap/bin/pdftk-smoser.pdftk pdftk && \

echo "Install graphicsmagick" && \
cd ~ && sudo add-apt-repository ppa:dhor/myway -y && \
sudo apt-get update && \
sudo apt-get install graphicsmagick -y && \

echo "Install poppler-utils" && \
cd ~ && sudo apt-get install poppler-utils && \

echo "Install mongo-db" && \
cd ~ && sudo apt-get update && \
sudo apt-get install mongodb -y && \
sudo apt-get update && \
cd ~/monterossa && sudo mkdir -p db && \
sudo service mongodb stop && \
sudo mongod --fork --logpath /var/log/mongod.log --dbpath ~/monterossa/db/ && \
######### Populate mongo with some seeds (data)  ##########
cd ~/monterossa && \
sudo touch mongoDemo.js && \
sudo chmod 666 mongoDemo.js && \
sudo echo "db = db.getSiblingDB('iarchive');

db.createUser( { user: 'iarchive', pwd: 'iarchive', roles: [ 'readWrite', 'dbAdmin' ] } )
db.users.insert( { '_id' : ObjectId('579b4ccd4841865f3c9b8de7'), 'avatar' : '', 'city' : 'Alpnach Dorf', 'country' : 'GBR', 'email' : 'fr@monterossa.ch', 'firstName' : 'Monterossa', 'houseNumber' : '21', 'language' : 'en', 'lastName' : 'Holding AG', 'lastLoginDate' : '', 'locale' : '', 'password' : '\$2a\$10\$W0vqDxJTFgTs1Ls2F9QvTe7lvzkbUwtUsp.GcK8Ya3s2LxGt1S/O2', 'permissionRead' : true, 'permissionWrite' : true, 'permissionDelete' : true, 'phoneNumber' : '', 'photoUrl' : '/assets/images/avatar.png', 'username' : 'fr@monterossa.ch', 'role' : 'admin', 'showStartScreen' : true, 'state' : '', 'street' : 'Industriestrasse', 'visibility' : 'all', 'zipCode' : '6055', 'createDate' : ISODate('2016-07-29T12:32:13.019Z'), 'updateDate' : ISODate('2018-06-26T13:47:19.135Z'), 'roomOrder' : [], 'userConfigId' : '591b76f71e3d569123bb858b' } )
db.user_configs.insert( { '_id' : ObjectId('591b76f71e3d569123bb858b'), 'userId' : '579b4ccd4841865f3c9b8de7', 'backgroundUrl' : 'White-Floor', 'createDate' : ISODate('2017-05-30T09:08:29.880Z'), 'updateDate' : ISODate('2018-02-19T20:21:19.465Z') } )
db.uploading_queue.insert( { '_id' : ObjectId('5a9aaec25f7f635839dd56d3'), 'userId' : '579b4ccd4841865f3c9b8de7', 'queue' : [], 'updateDate' : ISODate('2018-07-04T12:22:10.236Z'), 'createDate' : ISODate('2018-03-03T14:18:42.340Z') } )
db.accounts.insert( { '_id' : ObjectId('577bcafeb72c81fe4909b6df'), 'userLimit' : 15.0, 'users' : [ '579b4ccd4841865f3c9b8de7' ], 'endLicenseDate' : ISODate('2017-07-05T17:21:28.469Z'), 'updateDate' : ISODate('2018-06-27T11:28:22.535Z'), 'storageUser' : '175', 'storageCapacity' : '700', 'storageUsed' : '175' } )
db.rooms_order.insert( { '_id' : ObjectId('5a65a64a032fed61972c9616'), 'owner' : '579b4ccd4841865f3c9b8de7', 'order' : [], 'updateDate' : ISODate('2018-07-04T12:22:30.043Z') } )

db.users.find().pretty()" > mongoDemo.js && \
sudo mongo < mongoDemo.js && \
sudo kill $(ps aux | grep -m1 mongo | awk '{print $2}') && \

echo "Install Redis" && \
cd ~ && sudo wget http://download.redis.io/redis-stable.tar.gz &&
tar xvzf redis-stable.tar.gz && \
cd redis-stable && \
make && \
sudo make install && \
sudo mkdir -p /var/log/redis && \
sudo touch /var/log/redis/redis.log && \
sudo chmod 666 /var/log/redis/redis.log && \

# echo "Create SSL-cert & key (in directories according config file" && \
# cd ~ && sudo add-apt-repository ppa:certbot/certbot -y && \
# sudo apt-get update && \
# sudo apt-get install certbot -y && \
# sudo apt-get install letsencrypt && \
# sudo certbot certonly --standalone -d digital-office.link -m fr@monterossa.ch && \

echo "Create self-assigned SSL-cer & key" && \
sudo mkdir -p /etc/letsencrypt/live/digital-office.link && \
cd /etc/letsencrypt/live/digital-office.link && \
sudo openssl genrsa -out privkey.pem 4096 && \
sudo openssl req -new -x509 -key ./privkey.pem -out cert.pem -days 365 -subj "/emailAddress=fr@monterossa.ch/C=CH/ST=Zurich/L=Zurich/O=Monterossa/CN=digital-office.link" && \
cd .. && sudo chmod 600 -R ./digital-office.link && \

echo "Install nginx" && \
sudo apt-get update && \
sudo apt-get install nginx -y && \
sudo chmod 666 /etc/nginx/nginx.conf && \
sudo echo "user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
	worker_connections 768;
	# multi_accept on;
}

http {
    server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name www.digital-office.link;
        return 301 https://\$server_name\$request_uri;
    }

    server{
            listen 443;
            ssl on;
            server_name www.digital-office.link;
            ssl_certificate /etc/letsencrypt/live/digital-office.link/cert.pem;
            ssl_certificate_key /etc/letsencrypt/live/digital-office.link/privkey.pem;
            location / {
                    include mime.types;
                    root /home/ubuntu/monterossa/iarchiv-client/dist/iarchiv-client;
            }
    }
}" > /etc/nginx/nginx.conf && \

cd ~ && \
echo "Done...";