#!/bin/bash

. /usr/share/dosis2/bin/shell-support-functions

mountpoint="$1"

STORE_MOUNT_POINT=/mnt/usb
SYS_MOUNT_POINT=/mnt/temp
IMAGE_NAME_PREFIX=image_dosis_2_ubuntu1804_x86_64

function usage() {
    echo -e "\033[31m$1\033[0m"
    echo ""
    echo "bash build_image.bash <mount>"
    echo ""
    echo "to specify a version default (if a version is not found),"
    echo "you may use the VERSION environment variable"
    echo "note that it is always better to use the "
    echo "installed version for the image version"
    echo ""
    echo "Examples:"
    echo "    build-image 10.106.0.180:/opt/images"
    echo "    build-image /dev/sda1"
    echo "    VERSION=2.10.0-1 build-image 10.104.0.180:/opt/images"
    echo "    VERSION=2.10.0-1 build-image /dev/sda1"
    echo ""
}

enforce_root

if [ -z "${mountpoint}" ]; then
    usage "Mount point must not be empty"
    exit 1
fi

function __cleanup() {
    echo "Unmounting paths: ${SYS_MOUNT_POINT}/boot ${SYS_MOUNT_POINT} ${STORE_MOUNT_POINT}"
    if mount | grep -q "${SYS_MOUNT_POINT}/boot"; then
        umount -r "${SYS_MOUNT_POINT}/boot"
    fi
    if mount | grep -q "${SYS_MOUNT_POINT}"; then
        umount -r "${SYS_MOUNT_POINT}"
    fi
    if mount | grep -q "${STORE_MOUNT_POINT}"; then
        umount -r "${STORE_MOUNT_POINT}"
    fi

    echo "Removing paths: ${STORE_MOUNT_POINT} ${SYS_MOUNT_POINT}"
    if [ -d "${STORE_MOUNT_POINT}" ]; then
        rmdir "${STORE_MOUNT_POINT}"
    fi
    if [ -d "${SYS_MOUNT_POINT}" ]; then
        rmdir "${SYS_MOUNT_POINT}"
    fi
}
trap __cleanup EXIT
echo "Unmounting: ${STORE_MOUNT_POINT}"
umount ${STORE_MOUNT_POINT} > /dev/null 2>&1 || true

mkdir -vp ${SYS_MOUNT_POINT} ${STORE_MOUNT_POINT}
echo "Mounting: ${mountpoint} -> ${STORE_MOUNT_POINT}"
mount ${mountpoint} ${STORE_MOUNT_POINT} || abort $? "Failed to mount ${mountpoint} -> ${STORE_MOUNT_POINT}"

echo "Cleaning system..."

echo "Cleaning APT cache..."
apt-get clean

echo "Cleaning network configuration"
mkdir -p /tmp/netplansave
mv -f /etc/netplan/*.yaml /tmp/netplansave

echo "Removing mlocate database..."
rm -f "/var/lib/mlocate/mlocate.db"

echo "Stopping some services..."
systemctl stop apache2
systemctl stop rsyslog

echo "Removing log files..."
find /var/log -type f -exec rm -f {} \;

echo "Disabling swapfile..."
swapoff "/swapfile" > /dev/null 2>&1 || true

echo "Removing swapfile..."
rm -f "/swapfile"

echo "Removing old backups..."
rm -rf /opt/dosis2/backup/*

# mount root filesystem
echo "Mounting: ${SYS_MOUNT_POINT}"
mount --bind / ${SYS_MOUNT_POINT}


VERSION="${VERSION:-`dosis-version`}"
if [ "${VERSION}" = "" ]; then
    abort 2 "Cannot find package version"
fi

echo "Creating tar archive for: ${VERSION}..."
tar c -C "${SYS_MOUNT_POINT}" . | \
	pv -s `du -sb "${SYS_MOUNT_POINT}" | awk '{print $1}'` | \
	bzip2 > "${STORE_MOUNT_POINT}/.${IMAGE_NAME_PREFIX}.${VERSION}.tar.bz2"
mv "${STORE_MOUNT_POINT}/.${IMAGE_NAME_PREFIX}.${VERSION}.tar.bz2" \
	"${STORE_MOUNT_POINT}/${IMAGE_NAME_PREFIX}.${VERSION}.tar.bz2"

echo "Restoring network configuration"
mv -f /tmp/netplansave/*.yaml /etc/netplan
