#!/usr/bin/env bash

# Permissions
chmod 755 ./$1/uninstall_mysql.sh
chmod 755 ./$1/uninstall_mongodb.sh
chmod 755 ./$1/uninstall_redis.sh
chmod 755 ./$1/uninstall_beanstalkd.sh
chmod 755 ./$1/uninstall_ruby_and_rails.sh
chmod 755 ./$1/uninstall_yarn.sh
chmod 755 ./$1/install_tools.sh
chmod 755 ./$1/install_yarn.sh
chmod 755 ./$1/install_ruby_and_rails.sh

./$1/uninstall_mysql.sh
./$1/uninstall_mongodb.sh
./$1/uninstall_redis.sh
./$1/uninstall_beanstalkd.sh
./$1/uninstall_ruby_and_rails.sh
./$1/uninstall_yarn.sh
./$1/install_tools.sh
./$1/install_yarn.sh
./$1/install_ruby_and_rails.sh

# remove already injected / previous same hosts
cd
sudo sed -i 's/# START: svsbank-cli inject for required host map//g' /etc/hosts
sudo sed -i 's/127.0.0.1 local.host//g' /etc/hosts
sudo sed -i 's/127.0.0.1 www.local.host//g' /etc/hosts
sudo sed -i 's/127.0.0.1 forum.local.host//g' /etc/hosts
sudo sed -i 's/127.0.0.1 guide.local.host//g' /etc/hosts
sudo sed -i 's/127.0.0.1 api.local.host//g' /etc/hosts
sudo sed -i 's/127.0.0.1 m.local.host//g' /etc/hosts
sudo sed -i 's/127.0.0.1 bbm.local.host//g' /etc/hosts
sudo sed -i 's/127.0.0.1 0ur4w34z0m31n7rn4ls3rv1c3z.local.host//g' /etc/hosts
sudo sed -i 's/127.0.0.1 accounts.local.host//g' /etc/hosts
sudo sed -i 's/127.0.0.1 agen.local.host//g' /etc/hosts
sudo sed -i 's/# END: svsbank-cli inject for required host map//g' /etc/hosts

# delete end of file blang line
sudo sed -i -e :a -e '/^\n*$/{$d;N;ba' -e '}' /etc/hosts

# add hosts
sudo sh -c "echo '' >> /etc/hosts"
sudo sh -c "echo '# START: svsbank-cli inject for required host map' >> /etc/hosts"
sudo sh -c "echo '127.0.0.1 local.host' >> /etc/hosts"
sudo sh -c "echo '127.0.0.1 www.local.host' >> /etc/hosts"
sudo sh -c "echo '127.0.0.1 forum.local.host' >> /etc/hosts"
sudo sh -c "echo '127.0.0.1 guide.local.host' >> /etc/hosts"
sudo sh -c "echo '127.0.0.1 api.local.host' >> /etc/hosts"
sudo sh -c "echo '127.0.0.1 m.local.host' >> /etc/hosts"
sudo sh -c "echo '127.0.0.1 bbm.local.host' >> /etc/hosts"
sudo sh -c "echo '127.0.0.1 0ur4w34z0m31n7rn4ls3rv1c3z.local.host' >> /etc/hosts"
sudo sh -c "echo '127.0.0.1 accounts.local.host' >> /etc/hosts"
sudo sh -c "echo '127.0.0.1 agen.local.host' >> /etc/hosts"
sudo sh -c "echo '# END: svsbank-cli inject for required host map' >> /etc/hosts"
sudo sh -c "echo '' >> /etc/hosts"

cd ~/svsbank/mothership

source ~/.svsbank_profile

# Install Bundler and Foreman
gem install bundler
gem install foreman

# Pulling necessery images
docker pull mysql:5.7
docker pull mongo:latest
docker pull elasticsearch:2-alpine
docker pull docker.elastic.co/elasticsearch/elasticsearch:5.6.4
docker pull redis:alpine
docker pull schickling/beanstalkd
docker pull memcached:alpine
docker pull rabbitmq:alpine

# Konfigurasi .env
cp env.sample .env
sed -i 's/DATABASE_PASSWORD=/DATABASE_PASSWORD=rootpw/g' .env
sed -i 's/DATABASE_SECONDBASE_PASSWORD=/DATABASE_SECONDBASE_PASSWORD=rootpw/g' .env
sed -i 's/DATABASE_TEST_PASSWORD=/DATABASE_TEST_PASSWORD=rootpw/g' .env
sed -i 's/DATABASE_TEST_SECONDBASE_PASSWORD=/DATABASE_TEST_SECONDBASE_PASSWORD=rootpw/g' .env
sed -i 's/ELASTICSEARCH_PRODUCT_CLUSTER=http:\/\/127.0.0.1:9200/ELASTICSEARCH_PRODUCT_CLUSTER=http:\/\/elastic:elastic@127.0.0.1:9201/g' .env

# Running services
cd ~/svsbank
docker-compose up -d

# Install Gems dan NPM Dependencies
cd ~/svsbank/mothership
bundle install
yarn

# Inisialisasi data di database
rake db:create
rake db:schema:load
rake db:seed
rake db:test:prepare

# Persiapkan elasticsearch index
rake elasticsearch:reset_index
rake searcheror:create_indices
rake searcheror:reindex

# Jalankan script yang diperlukan
rails r script/payment_fee.rb
rails r script/o2o_agent/agent_cashback.rb
rails r script/o2o_agent/populate_agent_virtual_product_cashback_settings.rb
rails r script/inits/init_premium_packages.rb
# rails r script/inits/init_popular_section.rb
rails r script/inits/init_priority_buyer_packages.rb

# Stopping Services
# cd ~/svsbank
# docker-compose stop
