#!/usr/bin/env bash

set -e

function getuuid () {
    local mountpath="$1"
    eval `cat /etc/fstab | grep -e "${mountpath}" | cut -d' ' -f1`
    echo "${UUID}"
}

function recreatefs () {
    local name="$1"
    local partition="$2"
    local mountpath="$3"
    local rootdev="$4"
    local uuid=`getuuid "${mountpath}"`
    
    echo "recreating ${name} filesystem"
    mkfs.ext4 "${rootdev}p${partition}"
    tune2fs "${rootdev}p${partition}" -U "${uuid}"
    sync
    blockdev --flushbufs "${rootdev}"
}

# check filesystems to ensure they exist and are not corrupt
# if they are corrupt or do not exist for some reason, create
# them and then mount them
rootpart=`df -T | grep -re '/$' - | cut -d' ' -f1`

# remove the last 2 characters which should be p1
rootdev=${rootpart::-2}

# check if we need to recreate the log partition
if ! file -s ${rootdev}p2 | grep filesystem > /dev/null 2>&1; then
    logger "filesystem was dead for /var/log, recreating..."

    # attempt to unmount the filesystem if it is somehow mounted
    umount -lr ${rootdev}p2 || true

    recreatefs log 2 /var/log "${rootdev}"
fi

# check if we need to recreate the settings partition
if ! file -s ${rootdev}p3 | grep filesystem > /dev/null 2>&1; then
    logger "filesystem was dead for /opt/stpc/var/lib/etc, recreating..."

    # attempt to unmount the filesystem if it is somehow mounted
    umount -lr ${rootdev}p3 || true

    recreatefs settings 3 /opt/stpc/var/lib/etc "${rootdev}"
fi

# check if we have the filesystem mounted and if not, try to mount it
# if the mount fails, then try to recreate and mount once more
if ! mount | grep "/var/log" > /dev/null 2>&1; then
    if ! mount -U `getuuid "/var/log"` > /dev/null 2>&1; then
        logger "filesystem was dead for /var/log, recreating..."
        recreatefs log 2 "/var/log" "${rootdev}"
        if ! mount -U `getuuid "/var/log"` > /dev/null 2>&1; then
            die "Failed to recreate log partition and remount! I tried everything I could :("
        fi
    fi
fi
if ! mount | grep "/opt/stpc/var/lib/etc" > /dev/null 2>&1; then
    if ! mount -U `getuuid "/opt/stpc/var/lib/etc"` > /dev/null 2>&1; then
        logger "filesystem was dead for /opt/stpc/var/lib/etc, recreating..."
        recreatefs settings 3 "/opt/stpc/var/lib/etc" "${rootdev}"
        if ! mount -U `getuuid "/opt/stpc/var/lib/etc"` > /dev/null 2>&1; then
            die "Failed to recreate settings partition and remount! I tried everything I could :("
        fi
    fi
fi

# Recreate original ethernet configuration if it does not exist
if ! [ -f "/opt/stpc/var/lib/etc/network/interfaces.d/eth0" ]; then
    install -m 0775 -o dPG -g dosis -dv "/opt/stpc/var/lib/etc"
    install -m 0775 -o dPG -g dosis -dv "/opt/stpc/var/lib/etc/network/"
    install -m 0775 -o dPG -g dosis -dv "/opt/stpc/var/lib/etc/network/interfaces.d"

    echo "allow-hotplug eth0
iface eth0 inet static
    address 172.20.4.101
    netmask 255.255.255.0
    gateway 172.20.4.1
" > "/opt/stpc/var/lib/etc/network/interfaces.d/eth0"

    chown dPG.dosis "/opt/stpc/var/lib/etc/network/interfaces.d/eth0"
    chmod 0664 "/opt/stpc/var/lib/etc/network/interfaces.d/eth0"
fi

# Recreate original alignment label if it does not exist
if ! [ -f "/opt/stpc/var/lib/etc/alignment.bmp" ]; then
    install -m 0664 -o dPG -g dosis -v "/opt/stpc/etc/alignment.bmp" "/opt/stpc/var/lib/etc/alignment.bmp"
fi

# Recreate logrotate dir for logrotate status
if ! [ -d "/opt/stpc/var/lib/etc/logrotate.status.d" ]; then
	install -m 0775 -o dPG -g dosis -dv "/opt/stpc/var/lib/etc/logrotate.status.d"
fi

# ensure appropriate permissions exist always
chown dPG.dosis "/opt/stpc/var/lib/etc"
chown dPG.dosis "/opt/stpc/var/lib/etc/network"
chown dPG.dosis "/opt/stpc/var/lib/etc/network/interfaces.d"
chown dPG.dosis "/opt/stpc/var/lib/etc/network/interfaces.d/eth0"
chown dPG.dosis "/opt/stpc/var/lib/etc/logrotate.status.d"

systemctl restart rsyslog
