#!/bin/bash

set -e
if [[ ! -d ~/tmp/citramanik/build ]]; then
mkdir -p ~/tmp/citramanik/build
fi
cd ~/tmp/citramanik/build

# Checking Build Deps
echo "stage 1 of 5"
virtualenv --version &> /dev/null
venv_stats=$?
if [ $venv_stats -eq 0 ]; then
    echo Virtualenv found in $(which virtualenv)
else
    echo Virtualenv not found! Installing ...
    $(which python3) -m pip install virtualenv
fi

git --version &> /dev/null
git_stats=$?
if [ $git_stats -eq 0 ]; then
    echo git found in $(which git)
else
    echo git not found! Installing ...
    brew install git
fi

# Get Source code
echo echo "stage 2 of 5"
if [[ ! -d citramanik-macOS ]]; then
    git clone https://github.com/devlovers-id/citramanik-qt.git citramanik-macOS
    cd citramanik-macOS
else
    cd citramanik-macOS
    git pull
fi

# Setup Virtualenv
echo "stage 3 of 5"
if [[ ! -d venv ]]; then
    virtualenv venv
fi
source venv/bin/activate
pip install -r requirement.txt

# Building Citramanik
echo "stage 4 of 5"
pyinstaller --clean Citramanik.spec

# Post Build
echo "stage 5 of 5"
if [[ ! -d ~/tmp/citramanik/out ]]; then
mkdir -p ~/tmp/citramanik/out
fi
cp -r dist/citramanik-qt.app ~/tmp/citramanik/out/citramanik-qt.app

echo "Citramanik-qt already saved in ~/tmp/citramanik/out"
deactivate
exit 0
