#!/bin/bash

set -e

cd ~/dev/

echo "checking out sources..."

if [[ ! -d kicad ]]; then
    git clone https://github.com/KiCad/kicad-source-mirror.git kicad --recursive
    cd kicad
else
    cd kicad
    git pull
    git submodule update --init --recursive
fi

mkdir -p  build
cd build
echo "compiling...."
cmake -DKICAD_STABLE_VERSION=ON ../
make clean
benice make -j`nproc`
echo "installing..."
sudo make install

# install component libraries
cd ~/dev/
echo "checking out libraries..."
if [[ ! -d kicad-library ]]; then
    git clone https://github.com/KiCad/kicad-library.git --recursive
    cd kicad-library
else
    cd kicad-library
    git pull
    git submodule update --init --recursive
fi
mkdir build 
cd build/ 
echo "installing libraries"
cmake ../ 
sudo make install -j`nproc`