#!/bin/bash

set -x
set -e

myOS=`uname -s`
APP_APT_DEP="python-mysqldb libmysqlclient-dev python-dev unzip python-virtualenv git python-pip libssl-dev"

if [ "${myOS}" == "Linux" ]; then
    echo "updating apt repos and installing dependencies ${APP_APT_DEP}"
    echo "sleeping 3 seconds in case this is not wanted, Ctrl-C to exit"
    sleep 3
    sudo apt-get update
    sudo apt-get install ${APP_APT_DEP}
fi


echo "Installing virtualenv"
virtualenv virtenv
if [ $? != 0 ]; then
    echo "Something went wrong with the virtual env, refusing to continue. Please make sure the command 'virtualenv' exists."
    break
fi

echo "Activating virtualenv"
. ./virtenv/bin/activate

echo "Making sure pip is up to date"
pip install -U pip

echo "Installing requirements via pip"
pip install -r requirements.txt


# Bootstrap-table & third party
rm -rf temp
mkdir temp

BOOTSTRAPTABLE_VERSION="1.4.0"
filename="bootstrap-table"
pathname="${filename}/archive/${BOOTSTRAPTABLE_VERSION}"
echo "Pulling bootstrap-table an other js/css goodies"
wget https://github.com/wenzhixin/${pathname}".zip" -O temp/${filename}".zip"

unzip temp/${filename} -d temp/

rm -rf ./static/css/${filename}".min.css"
rm -rf ./static/js/${filename}".min.js"

mv temp/${filename}"-"${BOOTSTRAPTABLE_VERSION}/dist/${filename}".min.css" ./static/css/
mv temp/${filename}"-"${BOOTSTRAPTABLE_VERSION}/dist/${filename}".min.js" ./static/js/

JQVERSION="1.11.1"
wget http://code.jquery.com/jquery-${JQVERSION}.js -O ./static/js/jquery.js

rm -rf temp/
