#!/bin/bash
CONF_LINK=https://dl.dropboxusercontent.com/u/32823955/scripts/nginx_for_express.conf

echo "Start installing app1"
sudo apt-get update > /dev/null
curl -sL https://deb.nodesource.com/setup | sudo bash -
sudo apt-get install -y nodejs > /dev/null
sudo apt-get -y install nginx cloud-guest-utils > /dev/null

DNS_NAME=$(ec2metadata --public-hostname)

sudo rm /ect/nginx/sites-available/default
sudo rm /ect/nginx/sites-enabled/default

echo "Start configure nginx"
sudo wget $CONF_LINK -O /etc/nginx/sites-available/$DNS_NAME
sudo sed -i "s/localhost/$DNS_NAME:80/g" /etc/nginx/sites-available/$DNS_NAME
sudo ln -s /etc/nginx/sites-available/$DNS_NAME /etc/nginx/sites-enabled/$DNS_NAME

sudo npm install forever -g > /dev/null
mkdir myapp
cd myapp

wget https://dl.dropboxusercontent.com/u/32823955/scripts/ex2/app2/app.js
wget https://dl.dropboxusercontent.com/u/32823955/scripts/ex2/app2/package.json

sudo npm install > /dev/null
echo "Start node app.js"
forever start app.js
sudo service nginx restart