--- /dev/null
+
+DEFAULT: _target
+.PHONY: _target _qemu _partition _format _mount _bootstrap _install _sparse _copy _config _syslinux _umount _partcopy _squash _mbr
+
+ARCH := amd64
+KEYRING := keys/debian-archive-stretch-stable.gpg
+BOOTSTRAP := http://cdn.debian.net/debian
+
+define SOURCES =
+deb http://cdn.debian.net/debian stretch main
+endef
+PACKAGES := ${PACKAGES} apt btrfs-tools busybox-static bzip2 ca-certificates cron debian-archive-keyring deborphan firmware-linux-free gzip htop ifupdown init iputils-ping irqbalance isc-dhcp-client less make net-tools nmap ntpdate openssh-client psmisc rsync sshfs sudo systemd traceroute unzip vim wget wireless-tools wpasupplicant xz-utils zip
+
+ifdef disk
+VOLUME := ${disk}
+PBOOT := ${disk}1
+PROOT := ${disk}2
+SYSROOT := hdd_mount/
+CFGROOT := hdd_mount/
+
+_target: _mbr _umount _config _install _partition
+
+_partition:
+_format: _partition
+_mount: _format
+_bootstrap: _mount
+_install: _bootstrap _mount
+_config: _install _mount
+_syslinux: _mount
+_umount: _mount _config _syslinux
+_mbr: _umount _syslinux
+
+else ifdef image
+VOLUME := ${image}
+PBOOT := part_boot.img
+PROOT := part_root.img
+SYSROOT := sys_root/
+CFGROOT := img_mount/
+
+_target: _mbr _umount _partcopy _config _install
+
+_bootstrap:
+_install: _bootstrap
+_sparse:
+_format: _sparse
+_mount: _format
+_copy: _install _mount
+_config: _copy _mount
+_syslinux: _mount
+_umount: _mount _config _syslinux
+_partition:
+_partcopy: _partition _umount
+_mbr: _partcopy _syslinux
+
+else ifdef squash
+SQUASH := ${squash}
+SYSROOT := sys_root/
+CFGROOT := cfg_root/
+
+_target: _squash _install _config
+
+_bootstrap:
+_install: _bootstrap
+_copy: _install
+_config: _copy
+_squash: _config
+
+else ifdef raspi
+VOLUME := ${image}
+PBOOT := rpi_boot.img
+PROOT := rpi_root.img
+SYSROOT := rpi_root/
+CFGROOT := img_mount/
+
+ARCH := armhf
+KEYRING := keys/raspbian-archive-keyring.gpg
+BOOTSTRAP := http://raspbian.raspberrypi.org/raspbian
+
+define SOURCES =
+deb http://raspbian.raspberrypi.org/raspbian stretch main non-free firmware rpi
+deb http://archive.raspberrypi.org/debian stretch main
+endef
+PACKAGES := ${PACKAGES} firmware-brcm8011 libraspberrypi-bin libraspberrypi0 pi-bluetooth raspberrypi-bootloader raspberrypi-kernel
+
+_target: _umount _partcopy _config _install
+
+_qemu:
+_bootstrap: _qemu
+_install: _bootstrap
+_sparse:
+_format: _sparse
+_mount: _format
+_copy: _install _mount
+_config: _copy _mount
+_umount: _mount _config
+_partition:
+_partcopy: _partition _umount
+
+else
+_target:
+ false
+
+endif
+
+ifeq "${ARCH}" "amd64"
+PACKAGES := ${PACKAGES} linux-image-amd64
+else ifeq "${ARCH}" "i386"
+PACKAGES := ${PACKAGES} linux-image-686
+endif
+
+export SOURCES SYSROOT CFGROOT VOLUME PBOOT PROOT SQUASH ARCH KEYRING BOOTSTRAP
+
+${SYSROOT}:
+ btrfs subvol create "$$SYSROOT" || mkdir -p "$$SYSROOT}"
+ chmod 755 "$@"
+
+_qemu: ${SYSROOT}/usr/bin/qemu-arm-static
+${SYSROOT}/usr/bin/qemu-arm-static: /usr/bin/qemu-arm-static ${SYSROOT}
+ mkdir -p "$${SYSROOT}"/usr/bin
+ chmod 755 "$${SYSROOT}/usr" "$${SYSROOT}/usr/bin"
+ cp -p "$<" "$@"
+
+_bootstrap: ${SYSROOT}/bin/sh
+${SYSROOT}/bin/sh: | ${SYSROOT}
+ debootstrap --variant=minbase --arch="$${ARCH}" \
+ --keyring="$${KEYRING}" \
+ stretch "$${SYSROOT}" "$${BOOTSTRAP}"
+
+.PHONY: _aptkeys _norecommends
+_aptkeys: ${SYSROOT}/etc/apt/trusted.gpg.d/
+_norecommends: ${SYSROOT}/etc/apt/apt.conf.d/10norecommends
+
+${SYSROOT}/etc/apt/apt.conf.d/10norecommends: ${SYSROOT}
+ mkdir -p "$(dir $@)"
+ printf 'APT::Install-Recommends "false";\n' >"$@"
+ chmod 644 "$@"
+
+${SYSROOT}/etc/apt/trusted.gpg.d/: ${SYSROOT}
+ mkdir -p "$@"
+ cp keys/*.gpg "$@"
+
+_install: ${SYSROOT} _aptkeys _norecommends
+ printf %s "$${SOURCES}" >"$${SYSROOT}/etc/apt/sources.list"
+ -cp /etc/resolv.conf "$${SYSROOT}/etc/"
+ -chroot "$${SYSROOT}" sh -c 'apt-mark showmanual |xargs apt-mark auto'
+ -chroot "$${SYSROOT}" apt-get update
+ chroot "$${SYSROOT}" ln -sf /bin/true /usr/local/sbin/invoke-rc.d
+ chroot "$${SYSROOT}" apt-get --yes install ${PACKAGES}
+ chroot "$${SYSROOT}" apt-get --yes --auto-remove purge
+ -chroot "$${SYSROOT}" apt-get --yes --auto-remove upgrade
+ chroot "$${SYSROOT}" apt-get clean
+ chroot "$${SYSROOT}" rm /usr/local/sbin/invoke-rc.d
+
+_copy: ${SYSROOT}
+ btrfs sub snap "$${SYSROOT}" "$${CFGROOT}" || \
+ cp -a "$${SYSROOT}/." "$${CFGROOT}/"
+
+.PHONY: _main_config
+_config: _main_config
+_main_config: ${CFGROOT} cfg_seed/
+ cp -a cfg_seed/. "$${CFGROOT}"
+
+${SQUASH}: ${CFGROOT}
+ -rm "$@"
+ mksquashfs "$$CFGROOT" "$$SQUASH" -comp xz
+
+_squash: ${SQUASH}
+
+clean:
+ [ "$${SYSROOT#/}" ] && [ "$${CFGROOT#/}" ] || false # (abort receipe)
+ -mount |grep -qwF "$${SYSROOT}/boot" && umount "$${SYSROOT}/boot"
+ -mount |grep -qwF "$${SYSROOT}/proc" && umount "$${SYSROOT}/proc"
+ -mount |grep -qwF "$${SYSROOT}/sys" && umount "$${SYSROOT}/sys"
+ -mount |grep -qwF "$${SYSROOT}/dev" && umount "$${SYSROOT}/dev"
+ -mount |grep -qwF "$${SYSROOT}" && umount "$${SYSROOT}"
+ -mount |grep -qwF "$${CFGROOT}" && umount "$${CFGROOT}"
+ if [ -d "$${SYSROOT}" ] && ! mount |grep -qwFe "$${SYSROOT}"; then \
+ btrfs sub del "$${SYSROOT}" || echo rm -r "$${SYSROOT}"; \
+ fi
+ if [ -d "$${CFGROOT}" ] && ! mount |grep -qwFe "$${CFGROOT}"; then \
+ btrfs sub del "$${CFGROOT}" || echo rm -r "$${CFGROOT}"; \
+ fi
+ -rm "$${SQUASH}"