#!/bin/bash
[[ -d .config ]] || mkdir .config
if ! which pip > /dev/null
then
  (yum clean all; yum install -y epel-release) || \
    (apt-get update)
  yum install -y python-pip || \
    apt-get install -y python-pip || \
    ports install -y python-pip || \
    easy_install pip
fi
pip install -r build-requirements.txt
[[ -f buildenv/bin/activate ]] || virtualenv buildenv
source buildenv/bin/activate
cat > .config/configure.yaml << 'YAML'
- name: Configure this host for building lMaPS
  hosts: localhost
  connection: local
  become: true
  vars:
    build_dir: '{{ playbook_dir|dirname }}'
  pre_tasks:
    - setup:
  tasks:
    - name: Install yum packages
      yum:
        name: '{{ item }}'
        update_cache: true
        state: present
      with_items:
        - make
        - rpm-build
        - devscripts
        - vim
      when: "ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux' or ansible_distribution == 'Fedora'"
    - name: Install deb packages
      apt:
        name: '{{ item }}'
        update_cache: true
        state: present
      with_items:
        - make
        - rpm-build
        - devscripts
        - vim
      when: "ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'"
    - name: Install pip requirements
      make:
        target: install-reqs
        chdir: '{{ build_dir }}'
YAML
ansible-playbook .config/configure.yaml && touch .vagrant/up.flag
echo 'run `source buildenv/bin/activate` and `make` to build'