#!/bin/bash -e

export LANG=en_US.UTF-8

function unix_timestamp {
    date "+%s"
}

function kill_all_jobs { jobs -p | xargs kill; }
trap kill_all_jobs SIGINT SIGTERM


function build_all {
    PATH=$HOME/.cabal/bin:$HOME/usr/bin:$HOME/bin:$PATH

    ## run the script
    cd "/mnt/store/melpa"
    make -j8 $(grep --files-without-match ':fetcher\s*wiki' $(find recipes/ -type f -not -name python-info -and -not -name python3-info | sort)) &
    wait

    # python-info and python3-info are too big to run while other are
    # running. Running these separately should speed up other builds and
    # make this package actually build.
    make recipes/python-info
    make recipes/python3-info

    echo '{"completed":' "$(unix_timestamp)" '}' > html/build-status.json
}

build_all
