#!/bin/bash
#
# Set up a Raspberry Pi using Debian Jessie as a batman-adv mesh node.

# Based on https://kmp.or.at/~klaus/raspberry/build_rpi_sd_card.sh

set -e

if [ $EUID -ne 0 ]; then
  echo "this tool must be run as root"
  exit 1
fi

# Avoid problems caused by libpam-tmpdir 
unset TMPDIR TEMP TEMPDIR TMP

basedir=$(dirname $0)
builddir=$basedir/build
options=

if [[ ! -e $builddir ]]; then
  mkdir $builddir
fi

apt-get install binfmt-support python-cliapp qemu-user-static \
    qemu-utils debootstrap kpartx parted mbr vmdebootstrap

vmdebootstrap \
    --log=$builddir/debug.log \
    --log-level=debug \
    --variant minbase \
    --arch armel \
    --distribution jessie \
    --mirror http://httpredir.debian.org/debian \
    --image $builddir/test.img \
    --size 800M \
    --bootsize 64M \
    --boottype vfat \
    --log-level debug \
    --verbose \
    --no-kernel \
    --no-extlinux \
    --root-password raspberry \
    --hostname raspberrypi \
    --foreign /usr/bin/qemu-arm-static \
    --customize $basedir/build-rpi-mesh-node-customize \
    $options