#!/usr/bin/env bash

# sanity checks...
if [[ ! "$1" ]]
then
    printf "Need an argument!\n Usage: ./creatpisd block-device\n";
    exit 1;
elif [[ ! -e "$1" ]]
then
    printf "Block device does not exist!\n Usage: ./creatpisd block-device\n";
    exit 2;
elif [[ ! -b "$1" ]]
then
    printf "Argument is not a block device!\n Usage: ./creatpisd block-device\n";
    exit 3;
fi

printf "Getting pi image zip file 2018-06-27-raspbian-stretch-lite.zip..";
pitmp=$(mktemp -d /tmp/pitmp.XXXXXXXX);
cd "$pitmp";
wget http://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2018-06-29/2018-06-27-raspbian-stretch-lite.zip &>/dev/null;
unzip 2018-06-27-raspbian-stretch-lite.zip &>/dev/null;
printf " done!\n";

printf "Copying bits to the block device..";
dd bs=4M if="${pitmp}/2018-06-27-raspbian-stretch-lite.img" of="$1" &>/dev/null;
printf " done!\n";

printf "Resizing rootfs to fit the medium..";
rootoff="$(sfdisk -d "$1" | grep p2 | awk '{print $4}' | sed 's/,//')";
sfdisk --delete "$1" 2 &>/dev/null;
printf "%s" "$rootoff" | sfdisk --append "$1" &>/dev/null;
e2fsck -f "${1}p2" &>/dev/null;
resize2fs "${1}p2" &>/dev/null;
sync;
printf " done!\n";

printf "Mounting boot partition to enable SSH on boot time..";
mkdir tmp;
mount "${1}p1" tmp;
touch tmp/ssh;
umount "${1}p1";
printf " done!\n";

printf "Mounting rootfs to add first boot script to cron..";
mount "${1}p2" tmp;
controlip="$(hostname -I | sed 's/ //')";

cat >tmp/root/rdv <<PINITRDV
#!/usr/bin/env bash

sleep 60;

exec 3>"/dev/tcp/$controlip/31337";
printf "%s\n" "\$(hostname -I)" >&3;
exec 3>&-;

exit 0;
PINITRDV
chmod 700 tmp/root/rdv;

cat > tmp/var/spool/cron/crontabs/root <<PINITCRON
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.OQHz9Q/crontab installed on Thu Aug 23 14:09:50 2018)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
@reboot /root/rdv
PINITCRON
chmod 600 tmp/var/spool/cron/crontabs/root;

umount "${1}p2";
sync;
printf " done!\nYou can now remove the block device medium and boot up the pi!\nWaiting for incoming IP..";
remotepi="$(socat tcp-listen:31337,reuseaddr -)";
printf " done!\nRemote pi IP: %s\n" "$remotepi";

cd /tmp;
rm -rf "$pitmp";

printf "All done!\n";

