#!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Based upon bin/mk-freedombox-image from freedom-maker
# With non-free stuff removed
#
# 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/>.

set -e # Exit on first error

PROJECT_NAME='freedombone'

export TEXTDOMAIN=${PROJECT_NAME}-image-make
export TEXTDOMAINDIR="/usr/share/locale"

PROJECT_INSTALL_DIR=/usr/local/bin
if [ -f /usr/bin/${PROJECT_NAME} ]; then
    PROJECT_INSTALL_DIR=/usr/bin
fi

source /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-setup

#set -x # Enable debugging

IMAGE=$1
export ARCHITECTURE
export MACHINE
export SOURCE
export SUITE
export MYUSERNAME
export MYPASSWORD
export ROUTER_IP_ADDRESS
export BOX_IP_ADDRESS
export NAMESERVER1
export NAMESERVER2
export NAMESERVER3
export NAMESERVER4
export NAMESERVER5
export NAMESERVER6
export PROJECT_NAME
export CONFIG_FILENAME
export SSH_PUBKEY
export GENERIC_IMAGE
export MINIMAL_INSTALL
export SSH_PORT
export ONION_ONLY
export PROJECT_REPO
export DEBIAN_INSTALL_ONLY
export WIFI_INTERFACE
export WIFI_SSID
export WIFI_TYPE
export WIFI_PASSPHRASE
export WIFI_HOTSPOT
export WIFI_NETWORKS_FILE
export VARIANT
export MINIMUM_PASSWORD_LENGTH
export INSECURE
export AMNESIC
export SOCIALINSTANCE
export LOCAL_NAME

# Locate vmdebootstrap program fetched in Makefile
basedir=`pwd`
vendor_dir="${basedir}/vendor"
vmdebootstrap_dir="${vendor_dir}/vmdebootstrap"

if [ -z "$MIRROR" ] || [ -z "$SUITE" ] ; then
    echo $"error: Missing MIRROR and SUITE settings inherited from Makefile."
    exit 1
fi

# Packages to install in all Freedombone environments
base_pkgs="apt base-files ifupdown initramfs-tools \
logrotate kmod netbase rsyslog udev debian-archive-keyring"

# Packages needed on the beaglebone
beaglebone_pkgs="linux-image-armmp u-boot-tools u-boot"

# Packages needed on the Allwinner A20 devices:
a20_pkgs="linux-image-armmp-lpae u-boot-tools u-boot u-boot-sunxi"

# Packages needed for self-hosted development
dev_pkgs="build-essential devscripts make man-db emacs org-mode git mercurial"

echo Building $MACHINE $PROJECT_NAME for $ARCHITECTURE.

case "$MACHINE" in
    beaglebone)
        extra_pkgs="$beaglebone_pkgs"
        extra_opts="\
 --variant minbase \
 --bootoffset=2mib \
 --bootsize 128M \
 --boottype ext2 \
 --no-kernel \
 --no-extlinux \
 --foreign /usr/bin/qemu-arm-static \
 --roottype btrfs \
"
        ;;
    beaglebonewifi)
        extra_pkgs="$beaglebone_pkgs"
        extra_opts="\
 --variant minbase \
 --bootoffset=2mib \
 --bootsize 128M \
 --boottype ext2 \
 --no-kernel \
 --no-extlinux \
 --foreign /usr/bin/qemu-arm-static \
 --roottype btrfs \
"
        ;;
    cubietruck | a20-olinuxino-lime | a20-olinuxino-lime2 | a20-olinuxino-micro | cubieboard2)
        extra_pkgs="$a20_pkgs"
        extra_opts="\
 --variant minbase \
 --bootoffset=1mib \
 --bootsize 128M \
 --boottype vfat \
 --no-kernel \
 --no-extlinux \
 --foreign /usr/bin/qemu-arm-static \
 --roottype btrfs \
"
        ;;
    qemu)
        extra_opts="\
 --grub \
 --roottype btrfs \
"   ;;
    usb)
        extra_opts="\
 --grub \
 --roottype btrfs \
"   ;;
    meshclient)
        extra_opts="\
 --grub \
 --roottype btrfs \
"   ;;
    all)
        extra_opts="\
 --grub \
 --roottype btrfs \
"   ;;
esac

# allow for lots of extra fun customization options.
for customization in $CUSTOMIZATIONS
do
    case "$customization" in
        development)
            extra_pkgs="$extra_pkgs $dev_pkgs"
            ;;
    esac
done

for p in $base_pkgs $extra_pkgs; do
    pkgopts="$pkgopts --package $p"
done

# Make sure file is owned by current user, not root
touch $(dirname $IMAGE)/${PROJECT_NAME}.log

if [ -x vendor/vmdebootstrap/vmdebootstrap ] ; then
    VMDEBOOTSTRAP=vendor/vmdebootstrap/vmdebootstrap
else
    VMDEBOOTSTRAP=vmdebootstrap
fi

PROJECT_INSTALL_DIR=/usr/local/bin
if [ -f /usr/bin/${PROJECT_NAME} ]; then
    PROJECT_INSTALL_DIR=/usr/bin
fi

echo $'Making customised customisation script'
TEMP_CUSTOMISE=/etc/${PROJECT_NAME}/image-customise
TEMP_CUSTOMISE2=/tmp/${PROJECT_NAME}-image-customise2
TEMP_CUSTOMISE3=/tmp/${PROJECT_NAME}-image-customise3
TEMP_CUSTOMISE4=/tmp/${PROJECT_NAME}-image-customise4

# cat all the things together
combine_all_scripts $TEMP_CUSTOMISE2
if [ ! -f $TEMP_CUSTOMISE2 ]; then
    echo $'Could not combine scripts'
    exit 627219
fi

echo $'Changing values within customised customisation script'
cp $PROJECT_INSTALL_DIR/${PROJECT_NAME}-image-customise $TEMP_CUSTOMISE3
if [ $MYUSERNAME ]; then
    sed -i "s|MY_USERNAME=.*|MY_USERNAME=${MYUSERNAME}|g" $TEMP_CUSTOMISE3
fi
if [ $MYPASSWORD ]; then
    sed -i "s|MY_PASSWORD=.*|MY_PASSWORD=${MYPASSWORD}|g" $TEMP_CUSTOMISE3
fi
sed -i "s|ROUTER_IP_ADDRESS=.*|ROUTER_IP_ADDRESS=${ROUTER_IP_ADDRESS}|g" $TEMP_CUSTOMISE3
sed -i "s|BOX_IP_ADDRESS=.*|BOX_IP_ADDRESS=${BOX_IP_ADDRESS}|g" $TEMP_CUSTOMISE3
sed -i "s|NAMESERVER1=.*|NAMESERVER1=${NAMESERVER1}|g" $TEMP_CUSTOMISE3
sed -i "s|NAMESERVER2=.*|NAMESERVER2=${NAMESERVER2}|g" $TEMP_CUSTOMISE3
sed -i "s|NAMESERVER3=.*|NAMESERVER3=${NAMESERVER3}|g" $TEMP_CUSTOMISE3
sed -i "s|NAMESERVER4=.*|NAMESERVER4=${NAMESERVER4}|g" $TEMP_CUSTOMISE3
sed -i "s|NAMESERVER5=.*|NAMESERVER5=${NAMESERVER5}|g" $TEMP_CUSTOMISE3
sed -i "s|NAMESERVER6=.*|NAMESERVER6=${NAMESERVER6}|g" $TEMP_CUSTOMISE3
sed -i "s|PROJECT_NAME=.*|PROJECT_NAME=${PROJECT_NAME}|g" $TEMP_CUSTOMISE3
sed -i "s|CONFIG_FILENAME=.*|CONFIG_FILENAME=${CONFIG_FILENAME}|g" $TEMP_CUSTOMISE3
sed -i "s|SSH_PUBKEY=.*|SSH_PUBKEY=${SSH_PUBKEY}|g" $TEMP_CUSTOMISE3
sed -i "s|GENERIC_IMAGE=.*|GENERIC_IMAGE=${GENERIC_IMAGE}|g" $TEMP_CUSTOMISE3
sed -i "s|MINIMAL_INSTALL=.*|MINIMAL_INSTALL=\"${MINIMAL_INSTALL}\"|g" $TEMP_CUSTOMISE3
sed -i "s|SSH_PORT=.*|SSH_PORT=\"${SSH_PORT}\"|g" $TEMP_CUSTOMISE3
sed -i "s|ONION_ONLY=.*|ONION_ONLY=\"${ONION_ONLY}\"|g" $TEMP_CUSTOMISE3
sed -i "s|PROJECT_REPO=.*|PROJECT_REPO=\"${PROJECT_REPO}\"|g" $TEMP_CUSTOMISE3
sed -i "s|DEBIAN_INSTALL_ONLY=.*|DEBIAN_INSTALL_ONLY=\"${DEBIAN_INSTALL_ONLY}\"|g" $TEMP_CUSTOMISE3
sed -i "s|WIFI_INTERFACE=.*|WIFI_INTERFACE=\"${WIFI_INTERFACE}\"|g" $TEMP_CUSTOMISE3
sed -i "s|WIFI_SSID=.*|WIFI_SSID=\"${WIFI_SSID}\"|g" $TEMP_CUSTOMISE3
sed -i "s|WIFI_TYPE=.*|WIFI_TYPE=\"${WIFI_TYPE}\"|g" $TEMP_CUSTOMISE3
sed -i "s|WIFI_PASSPHRASE=.*|WIFI_PASSPHRASE=\"${WIFI_PASSPHRASE}\"|g" $TEMP_CUSTOMISE3
sed -i "s|WIFI_HOTSPOT=.*|WIFI_HOTSPOT=\"${WIFI_HOTSPOT}\"|g" $TEMP_CUSTOMISE3
sed -i "s|WIFI_NETWORKS_FILE=.*|WIFI_NETWORKS_FILE=\"${WIFI_NETWORKS_FILE}\"|g" $TEMP_CUSTOMISE3
sed -i "s|VARIANT=.*|VARIANT=\"${VARIANT}\"|g" $TEMP_CUSTOMISE3
sed -i "s|MINIMUM_PASSWORD_LENGTH=.*|MINIMUM_PASSWORD_LENGTH=\"${MINIMUM_PASSWORD_LENGTH}\"|g" $TEMP_CUSTOMISE3
sed -i "s|INSECURE=.*|INSECURE=\"${INSECURE}\"|g" $TEMP_CUSTOMISE3
sed -i "s|AMNESIC=.*|AMNESIC=\"${AMNESIC}\"|g" $TEMP_CUSTOMISE3
sed -i "s|SOCIALINSTANCE=.*|SOCIALINSTANCE=\"${SOCIALINSTANCE}\"|g" $TEMP_CUSTOMISE3
sed -i "s|LOCAL_NAME=.*|LOCAL_NAME=\"${LOCAL_NAME}\"|g" $TEMP_CUSTOMISE3
sed -i 's|#!/bin/bash||g' $TEMP_CUSTOMISE3

cat $TEMP_CUSTOMISE2 $TEMP_CUSTOMISE3 > $TEMP_CUSTOMISE4
if [ -f $TEMP_CUSTOMISE ]; then
    sudo rm $TEMP_CUSTOMISE
fi
sudo mv $TEMP_CUSTOMISE4 $TEMP_CUSTOMISE
rm $TEMP_CUSTOMISE2 $TEMP_CUSTOMISE3
if [ ! -f $TEMP_CUSTOMISE ]; then
    echo $'Customised customisation script could not be created'
    exit 735892
fi
sudo chmod +x $TEMP_CUSTOMISE
echo $'Customised customisation script created'


echo $"starting $VMDEBOOTSTRAP"

# Run vmdebootstrap script to create image
vmdebootstrap_failed=
sudo -H \
     SUITE="$SUITE" \
     MIRROR="$MIRROR" \
     BUILD_MIRROR="$BUILD_MIRROR"\
     MACHINE="$MACHINE" \
     ARCHITECTURE="$ARCHITECTURE" \
     SOURCE="$SOURCE" \
     CUSTOM_SETUP="$CUSTOM_SETUP" \
     $VMDEBOOTSTRAP \
     --log $(dirname $IMAGE)/${PROJECT_NAME}.log \
     --log-level debug \
     --size $IMAGE_SIZE \
     --image $IMAGE.img \
     --hostname ${PROJECT_NAME} \
     --verbose \
     --mirror $BUILD_MIRROR \
     --customize "$TEMP_CUSTOMISE" \
     --lock-root-password \
     --arch $ARCHITECTURE \
     --distribution $SUITE \
     $extra_opts \
     $pkgopts

echo $'Removing customised customisation script'
sudo shred -zu $TEMP_CUSTOMISE
