Create install script for the Milagro MFA project

A install.sh that can be run on compatable machine (Ubuntu 14.04+ at the
moment)

Update the README.md to describe what the script do and how to be used
diff --git a/.gitignore b/.gitignore
index 68d56e7..812f591 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,5 +7,7 @@
 config.py
 coverage.xml
 credentials.json
+install
 M_Pin_Backend.egg-info/
 mpin_*_storage.json
+servers/demo/public/mpin
diff --git a/README.md b/README.md
index e18610a..02104c1 100644
--- a/README.md
+++ b/README.md
@@ -15,6 +15,42 @@
 The rest of this document assumes that the repository is cloned into `<mpin-backend>`.
 Wherever `<mpin-backend>` appears, it should be replaced with the real location on your machine.
 
+### Install with provided script
+
+`install.sh` bash script is provided to ease the install process.
+You can run it from `<mpin-backend>` base directory like this
+```
+ > ./install.sh
+```
+you can provide optional argument to the script <mpin-front-end-location> where this is an absolute path of
+the location `https://github.com/miracl/milagro-mfa-js-client` is downloaded.
+```
+ > ./install.sh <mpin-front-end-location>
+```
+If location provided the install script can initiate the frontend build and link the Pin Pad  in the Demo, so the demo is fully operational.
+
+If no location is provided you should change the `mpinJSURL` yourself in demo/config.py. Which may include building the frontend and serve it from some web server.
+
+The script is tested and should work without any issue on Ubuntu 14.04,
+other versions of Ubuntu or Debian based distributions may work but is not guaranteed.
+
+The script install some dependencies from apt-get as packages:
+    * python-dev
+    * python-pip
+    * libffi-dev
+    * git ( We use it to get Milagro crypto library)
+    * cmake ( We use it to build Milagro crypto library)
+
+Several pip packages would be installed too. They can be reviewed in \`<mpin-backend>/requirements/common.txt` and `<mpin-backend>/requirements/dev.txt`
+`INSTALL_TYPE` environment variable is used is control  which file is used. You can use “common” or “dev” as values. “common” is used by default
+The script try its best to detect virtualenv and install python packages inside one if it is founded.
+
+The script build the needed Milagro Crypto ( Version 1.0.0 )
+
+NOTE: While running the script if config file is already found in dta, rps or demo the user would have the option to override it or keep it as it is
+
+Review the script for more details.
+
 ### Installing Prerequisites
 
 **1** Update your package manager tool
@@ -162,11 +198,11 @@
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
-	. . .
+    . . .
 </head>
 <body>
 </body>
-	. . .
+    . . .
 </html>
 ```
 Finally, open a browser on any machine that has network access to the machine on which the Milagro MFA Services are running. Browse to `http://<mpin-server-ip>:8005`.
diff --git a/install.sh b/install.sh
new file mode 100755
index 0000000..5f1bc7d
--- /dev/null
+++ b/install.sh
@@ -0,0 +1,219 @@
+#!/usr/bin/env bash
+
+BASE_DIR=$(pwd)
+
+# Specify where the frontend repo is located
+FRONTEND_LOCATION=${FRONTEND_LOCATION:-$1}
+
+INSTALL_TYPE=${INSTALL_TYPE:-"common"}
+
+function install_dependencies {
+    echo "Install needed packages"
+
+    # Update packages cache
+    sudo apt-get update
+
+    # Install needed packages
+    sudo apt-get install python-dev python-pip libffi-dev git cmake
+
+    # Install the required packages
+    VIRTUALENV=$(python -c 'import sys; print hasattr(sys, "real_prefix")' 2>/dev/null)
+    if [[ $VIRTUALENV ]]
+    then
+        # If in virtualenv use the local provided pip
+        # We assume that activation script is used and pip as command points to virtualenv pip
+        pip install -r "requirements/$INSTALL_TYPE.txt"
+    else
+        # If we are not in virtualenv we just install global
+        sudo pip install -r "requirements/$INSTALL_TYPE.txt"
+    fi
+}
+
+function get_crypto {
+    echo "Get Milagro Crypto Libraries"
+    mkdir -p install
+    cd install || exit
+    git clone https://github.com/miracl/milagro-crypto.git
+
+    cd milagro-crypto || exit
+    git checkout tags/1.0.0
+}
+
+function build_crypto {
+    echo "Build Milagro crypto library"
+
+    mkdir Release
+    cd Release || exit
+    cmake ..
+    make
+    make test
+    sudo make install
+}
+
+function get_credentials {
+    cd "$BASE_DIR" || exit
+    python scripts/getCommunityCredentials.py .
+}
+
+function build_frontend {
+    echo "build frontend js"
+
+    # Go to frontend code location
+    cd "$FRONTEND_LOCATION" || exit
+    # run frontend build script
+    ./build.sh
+
+    # Create the needed directory and link the frontend
+    cd "$BASE_DIR/servers/demo/public/" || exit
+
+    ln -sf "$FRONTEND_LOCATION/build/out/browser" mpin
+}
+
+function get_dependencies {
+    install_dependencies
+
+    get_crypto
+
+    build_crypto
+
+    get_credentials
+
+}
+
+# Configure services
+
+function configure_dta {
+    echo "Configure DTA"
+
+    cd "$BASE_DIR/servers/dta" || exit
+
+    CONFIGURE=1
+    if [ -f config.py ]
+    then
+        echo "NOTE: Config file for DTA already exist"
+        read -p "Do you want to override it (y/n)?" choice
+        case "$choice" in 
+          y|Y ) echo "yes"; CONFIGURE=1;;
+          n|N ) echo "no"; CONFIGURE=0;;
+          * ) echo "invalid";;
+        esac
+    fi
+
+    if [ $CONFIGURE ]
+    then
+        cp config_default.py config.py
+        CONFIG_FILE=$(pwd)/config.py
+        SALT=$(python -c "import os; print os.urandom(8).encode('hex')")
+        PASSPHRASE=$(python -c "import os; print os.urandom(8).encode('hex')")
+
+        CREDENTIALSFILE="$BASE_DIR/credentials.json"
+        BACKUP_FILE="$BASE_DIR/backup_dta.json"
+        rm -f "$BASE_DIR/backup_dta.json"
+
+        sed -i "s/\(%SALT%\)/$SALT/" "$CONFIG_FILE"
+        sed -i "s/\(%PASSPHRASE%\)/$PASSPHRASE/" "$CONFIG_FILE"
+        sed -i "s#\(%CREDENTIALSFILE%\)#$CREDENTIALSFILE#" "$CONFIG_FILE"
+        sed -i "s#\(%BACKUP_FILE%\)#$BACKUP_FILE#" "$CONFIG_FILE"
+    fi
+}
+
+function configure_rps {
+    echo "Configure RPS"
+
+    cd "$BASE_DIR/servers/rps" || exit
+
+    CONFIGURE=1
+    if [ -f config.py ]
+    then
+        echo "NOTE: Config file for DTA already exist"
+        read -p "Do you want to override it (y/n)?" choice
+        case "$choice" in 
+          y|Y ) echo "yes"; CONFIGURE=1;;
+          n|N ) echo "no"; CONFIGURE=0;;
+          * ) echo "invalid";;
+        esac
+    fi
+
+    if [ $CONFIGURE ]
+    then
+        cp config_default.py config.py
+
+        CONFIG_FILE=$(pwd)/config.py
+        CREDENTIALSFILE="$BASE_DIR/credentials.json"
+
+        sed -i "s#\(%CREDENTIALSFILE%\)#$CREDENTIALSFILE#" "$CONFIG_FILE"
+    fi
+}
+
+function configure_demo {
+    echo "Configure Demo RPA"
+
+    cd "$BASE_DIR/servers/demo" || exit
+
+    CONFIGURE=1
+    if [ -f config.py ]
+    then
+        echo "NOTE: Config file for DTA already exist"
+        read -p "Do you want to override it (y/n)?" choice
+        case "$choice" in 
+          y|Y ) echo "yes"; CONFIGURE=1;;
+          n|N ) echo "no"; CONFIGURE=0;;
+          * ) echo "invalid";;
+        esac
+    fi
+
+    if [ $CONFIGURE ]
+    then
+        cp config_default.py config.py
+
+        CONFIG_FILE=$(pwd)/config.py
+        # We need to remove new line, sed does not like it
+        COOKIESECRET=$(python -c "import os; print os.urandom(64).encode('base64').replace(\"\n\", '')")
+
+        sed -i "s#\(%COOKIESECRET%\)#$COOKIESECRET#" "$CONFIG_FILE"
+
+        if [ "$FRONTEND_LOCATION" ]
+        then
+            build_frontend
+
+            # mpin.js can be downloaded or copied/linked from localy build milagro-js-client
+            MPINJSURL="/public/mpin/mpin.js"
+            sed -i "s#\(%MPINJSURL%\)#$MPINJSURL#" "$CONFIG_FILE"
+        else
+            echo $'\n'
+            echo "NOTE: No location for the frontend provided you will need to configure %MPINJSURL% yourself"
+        fi;
+    fi
+}
+
+function run_instructions {
+    echo "
+    For development purposes you might run the services from command line.
+    Open 3 terminals and set the following two environment variables as shown below:
+
+    export PYTHONPATH=<mpin-backend>/lib:/usr/local/lib/python2.7/site-packages
+    export LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:/usr/local/lib
+    To run the services, perform the following commands, each in separate terminal:
+
+     > python servers/dta/dta.py
+     > python servers/rps/rps.py
+     > python servers/demo/mpinDemo.py
+    For more automated execution, you might need to write start/stop scripts in /etc/init.d
+    "
+}
+
+function configure_services {
+    echo "Configure the services"
+
+    configure_dta
+
+    configure_rps
+
+    configure_demo
+
+    run_instructions
+}
+
+get_dependencies
+
+configure_services