#!/usr/bin/make
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# A debian image builder, based upon freedom-maker Makefile
#
# License
# =======
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Where to fetch packages
MIRROR ?= http://httpredir.debian.org/debian
BUILD_MIRROR ?= http://httpredir.debian.org/debian
IMAGE_SIZE ?= 8G
IMAGE_NAME ?= 'full'
SUITE ?= stretch
# include source packages in image?
SOURCE ?= false

# yes no
BUILD = $(MACHINE)-$(ARCHITECTURE)
TODAY := $(shell date +%Y-%m-%d)
NAME = build/$(PROJECT_NAME)-$(IMAGE_NAME)-$(TODAY)_$(BUILD)
IMAGE = $(NAME).img
ARCHIVE = $(IMAGE).xz
SIGNATURE = $(ARCHIVE).sig
OWNER = 1000
XZ = xz --no-warn --best --verbose --keep
SIGN = -gpg --output $(SIGNATURE) --detach-sig $(ARCHIVE)

# settings for `make test`
TEST_SSH_PORT = 2222
TEST_FIRSTRUN_WAIT_TIME = 120 # seconds

USERNAME ?= $(echo $USER)
PASSWORD ?= $(PROJECT_NAME)

# IP address of the router (gateway)
ROUTER_IP_ADDRESS ?= "192.168.1.254"

# The fixed IP address of the Beaglebone Black (or other SBC) on your local network
BOX_IP_ADDRESS ?= "192.168.1.55"

# DNS
NAMESERVER1 ?= '213.73.91.35'
NAMESERVER2 ?= '85.214.20.141'
NAMESERVER3 ?= '213.73.91.35'
NAMESERVER4 ?= '85.214.73.63'
NAMESERVER5 ?= '84.200.69.80'
NAMESERVER6 ?= '84.200.70.40'

# Using taskset to pin build process to single core. This is a
# workaround for a qemu-user-static issue that causes builds to
# hang. (See Debian bug #769983 for details.)
MAKE_IMAGE = ARCHITECTURE=$(ARCHITECTURE) MACHINE=$(MACHINE) SOURCE=$(SOURCE) \
    MIRROR=$(MIRROR) SUITE=$(SUITE) OWNER=$(OWNER) \
    BUILD_MIRROR=$(BUILD_MIRROR) \
    MY_USERNAME=$(USERNAME) \
    MY_PASSWORD=$(PASSWORD) \
    ROUTER_IP_ADDRESS=$(ROUTER_IP_ADDRESS) \
    BOX_IP_ADDRESS=$(BOX_IP_ADDRESS) \
    NAMESERVER1=$(NAMESERVER1) \
    NAMESERVER2=$(NAMESERVER2) \
    NAMESERVER3=$(NAMESERVER3) \
    NAMESERVER4=$(NAMESERVER4) \
    NAMESERVER5=$(NAMESERVER5) \
    NAMESERVER6=$(NAMESERVER6) \
    CUSTOM_SETUP=$(CUSTOM_SETUP) \
    IMAGE_SIZE=$(IMAGE_SIZE) taskset 0x01 $(PROJECT_NAME)-image-make $(NAME)

# build Beaglebone SD card image
beaglebone: prep
	$(eval ARCHITECTURE = armhf)
	$(eval MACHINE = beaglebone)
	$(MAKE_IMAGE)
	@rm -f $(ARCHIVE)
	$(XZ) $(IMAGE)
	@echo ""
	$(SIGN)
	@echo "Build complete."

# build Beaglebone Black Wireless SD card image
beaglebonewifi: prep
	$(eval ARCHITECTURE = armhf)
	$(eval MACHINE = beaglebonewifi)
	$(MAKE_IMAGE)
	@rm -f $(ARCHIVE)
	$(XZ) $(IMAGE)
	@echo ""
	$(SIGN)
	@echo "Build complete."

# build Cubieboard2 SD card image
cubieboard2: prep
	$(eval ARCHITECTURE = armhf)
	$(eval MACHINE = cubieboard2)
	$(MAKE_IMAGE)
	@rm -f $(ARCHIVE)
	$(XZ) $(IMAGE)
	@echo ""
	$(SIGN)
	@echo "Build complete."

# build PCDuino3 SD card image
pcduino3: prep
	$(eval ARCHITECTURE = armhf)
	$(eval MACHINE = pcduino3)
	$(MAKE_IMAGE)
	@rm -f $(ARCHIVE)
	$(XZ) $(IMAGE)
	@echo ""
	$(SIGN)
	@echo "Build complete."

# build CubieTruck SD card image
cubietruck: prep
	$(eval ARCHITECTURE = armhf)
	$(eval MACHINE = cubietruck)
	$(MAKE_IMAGE)
	@rm -f $(ARCHIVE)
	$(XZ) $(IMAGE)
	@echo ""
	$(SIGN)
	@echo "Build complete."

# build A20 OLinuXino Lime SD card image
a20-olinuxino-lime: prep
	$(eval ARCHITECTURE = armhf)
	$(eval MACHINE = a20-olinuxino-lime)
	$(eval IMAGE = $(NAME).img)
	$(MAKE_IMAGE)
	rm -f $(ARCHIVE)
	$(XZ) $(IMAGE)
	@echo ""
	$(SIGN)

# build A20 OLinuXino Lime2 SD card image
a20-olinuxino-lime2: prep
	$(eval ARCHITECTURE = armhf)
	$(eval MACHINE = a20-olinuxino-lime2)
	$(MAKE_IMAGE)
	@rm -f $(ARCHIVE)
	$(XZ) $(IMAGE)
	@echo ""
	$(SIGN)
	@echo "Build complete."

# build A20 OLinuXino Micro SD card image
a20-olinuxino-micro: prep
	$(eval ARCHITECTURE = armhf)
	$(eval MACHINE = a20-olinuxino-micro)
	$(MAKE_IMAGE)
	@rm -f $(ARCHIVE)
	$(XZ) $(IMAGE)
	@echo ""
	$(SIGN)
	@echo "Build complete."

# build an i386 image
i386: prep
	$(eval ARCHITECTURE = i386)
	$(eval MACHINE = all)
	$(MAKE_IMAGE)
	@rm -f $(ARCHIVE)
	$(XZ) $(IMAGE)
	@echo ""
	$(SIGN)
	@echo "Build complete."

# build an i686 image
i686: prep
	$(eval ARCHITECTURE = i686)
	$(eval MACHINE = all)
	$(MAKE_IMAGE)
	@rm -f $(ARCHIVE)
	$(XZ) $(IMAGE)
	@echo ""
	$(SIGN)
	@echo "Build complete."

# build an amd64 image
amd64: prep
	$(eval ARCHITECTURE = amd64)
	$(eval MACHINE = all)
	$(MAKE_IMAGE)
	@rm -f $(ARCHIVE)
	$(XZ) $(IMAGE)
	@echo ""
	$(SIGN)
	@echo "Build complete."

# build an PC Engines APU image
apu: prep
	$(eval ARCHITECTURE = amd64)
	$(eval MACHINE = all)
	$(MAKE_IMAGE)
	@rm -f $(ARCHIVE)
	$(XZ) $(IMAGE)
	@echo ""
	$(SIGN)
	@echo "Build complete."

# build a qemu image
qemu: qemu-i386

qemu-i386: prep
	$(eval ARCHITECTURE = i386)
	$(eval MACHINE = qemu)
	$(MAKE_IMAGE)
	# Convert image to qemu format
	qemu-img convert -O qcow2 $(NAME).img $(NAME).qcow2
	@rm -f $(ARCHIVE)
	$(XZ) $(IMAGE)
	@echo ""
	$(SIGN)
	@echo "Build complete."

qemu-x86_64: prep
	$(eval ARCHITECTURE = x86_64)
	$(eval MACHINE = qemu)
	$(MAKE_IMAGE)
	# Convert image to qemu format
	qemu-img convert -O qcow2 $(NAME).img $(NAME).qcow2
	@rm -f $(ARCHIVE)
	$(XZ) $(IMAGE)
	@echo ""
	$(SIGN)
	@echo "Build complete."

vendor/vmdebootstrap/vmdebootstrap: vendor-patches/vmdebootstrap/*.patch
	freedombone-image-vmdebootstrap

prep: vendor/vmdebootstrap/vmdebootstrap
	mkdir -p build

clean:
	-rm -f build/$(PROJECT_NAME).log

distclean: clean
	sudo rm -rf build
