#!/bin/bash

set -x

echo "automount ...";
LABEL=boot2docker-data
MAGIC="boot2docker, please format-me"

# If there is a partition with `boot2docker-data` as its label, use it and be
# very happy. Thus, you can come along if you feel like a room without a roof.
BOOT2DOCKER_DATA=`blkid -o device -l -t LABEL=$LABEL`
UNPARTITIONED_HD="/dev/$(lsblk | grep disk | cut -f1 -d' ')"
echo $BOOT2DOCKER_DATA
if [ ! -n "$BOOT2DOCKER_DATA" ]; then
    echo "Is the disk unpartitioned?, test for the 'boot2docker format-me' string"

    # Is the disk unpartitioned?, test for the 'boot2docker format-me' string
    parted --script "$UNPARTITIONED_HD" print | grep "Partition Table: unknown"

    if [ $? -eq 0 ]; then
        # Test for our magic string (it means that the disk was made by ./boot2docker init)
        HEADER=`dd if=$UNPARTITIONED_HD bs=1 count=${#MAGIC} 2>/dev/null`

        if [ "$HEADER" = "$MAGIC" ]; then
            # save the preload userdata.tar file
            dd if=$UNPARTITIONED_HD of=/userdata.tar bs=1 count=4096 2>/dev/null
            # Create the partition, format it and then mount it
            echo "NEW boot2docker managed disk image ($UNPARTITIONED_HD): formatting it for use"
            # Let kernel re-read partition table
            partprobe
            # Add the data partition
            (echo n; echo p; echo 1; echo ; echo ; echo w) | fdisk $UNPARTITIONED_HD
            # Let kernel re-read partition table
            partprobe
            # wait for the partition to actually exist, timeout after about 5 seconds
            timer=0
            while [ "$timer" -lt 10 ]; do
                if [ -b "${UNPARTITIONED_HD}1" ]; then
                    BOOT2DOCKER_DATA=`echo "${UNPARTITIONED_HD}1"`
                    mkfs.ext4 -i 2048 -L $LABEL $BOOT2DOCKER_DATA && break
                fi
                echo "Waiting for ${UNPARTITIONED_HD}1 to exist."
                timer=$((timer + 1))
                sleep 0.5
            done
        fi

        DISK_VENDOR=$(cat /sys/class/block/$(basename $UNPARTITIONED_HD /dev/)/device/vendor /sys/class/block/$(basename $UNPARTITIONED_HD /dev/)/device/model | tr -d "\n")
        # Test if disk is "VMware, VMware Virtual S" and empty.
        if [ "$DISK_VENDOR" = "VMware, VMware Virtual S" ] || [ "$DISK_VENDOR" = "VMware  Virtual disk    " ]; then
            # Check whether the disk has any known partitions on it
            blkid -o device $UNPARTITIONED_HD
            if [ $? == 2 ]; then
                # As there are no partitions, let's make sure the disk is empty for real
                dd if=$UNPARTITIONED_HD of=device_test_file bs=1k count=256 > /dev/null 2>&1
                NON_NUL=$(<device_test_file tr -d '\0\n' | wc -c)
                if [ $NON_NUL == 0 ]; then
                    # Create the partition, format it and then mount it
                    echo "NEW VMware boot2docker managed disk image ($UNPARTITIONED_HD): formatting it for use"
                    (echo n; echo p; echo 1; echo ; echo ; echo w) | fdisk $UNPARTITIONED_HD
                    BOOT2DOCKER_DATA=`echo "${UNPARTITIONED_HD}1"`
                    mkfs.ext4 -i 2048 -L $LABEL $BOOT2DOCKER_DATA
                else
                    echo "Disk unpartitioned but something is there... not doing anything"
                fi
            else
                echo "Partition table found on disk, not doing anything"
            fi
        fi
    else
        # Pick the first ext4 as a fallback
        # TODO: mount all Linux partitions and look for a /var/lib/docker...
        BOOT2DOCKER_DATA=`blkid | grep -e 'TYPE="btrfs"'  -e 'TYPE="ext4"' | head -n 1 | sed 's/:.*//'`
    fi
fi

echo $BOOT2DOCKER_DATA

if [ -n "$BOOT2DOCKER_DATA" ]; then
    PARTNAME=`echo "$BOOT2DOCKER_DATA" | sed 's/.*\///'`
    echo "mount p:$PARTNAME ..."
    mkdir -p /mnt/$PARTNAME
    if ! mount $BOOT2DOCKER_DATA /mnt/$PARTNAME 2>/dev/null; then
        # for some reason, mount doesn't like to modprobe btrfs
        BOOT2DOCKER_FSTYPE=`blkid -o export $BOOT2DOCKER_DATA | grep TYPE= | cut -d= -f2`
        modprobe $BOOT2DOCKER_FSTYPE || true
        umount -f /mnt/$PARTNAME || true
        mount $BOOT2DOCKER_DATA /mnt/$PARTNAME
    fi

    # Just in case, the links will fail if not
    umount -f /var/lib/docker || true
    rm -rf /var/lib/docker /var/lib/boot2docker
    mkdir -p /var/lib

    mkdir -p /mnt/$PARTNAME/var/lib/boot2docker
    mkdir /var/lib/boot2docker
    mount --bind /mnt/$PARTNAME/var/lib/boot2docker /var/lib/boot2docker

    mkdir -p /mnt/$PARTNAME/var/lib/docker
    mkdir -p /var/lib/docker
    mount --bind /mnt/$PARTNAME/var/lib/docker /var/lib/docker

    mkdir -p /mnt/$PARTNAME/var/lib/containers
    mkdir -p /var/lib/containers
    mount --bind /mnt/$PARTNAME/var/lib/containers /var/lib/containers

    mkdir -p /mnt/$PARTNAME/var/log
    mkdir /var/log
    mount --bind /mnt/$PARTNAME/var/log /var/log

    mkdir -p /mnt/$PARTNAME/var/lib/kubelet
    mkdir /var/lib/kubelet
    mount --bind /mnt/$PARTNAME/var/lib/kubelet /var/lib/kubelet

    mkdir -p /mnt/$PARTNAME/var/lib/cni
    mkdir /var/lib/cni
    mount --bind /mnt/$PARTNAME/var/lib/cni /var/lib/cni

    mkdir -p /mnt/$PARTNAME/data
    mkdir /data
    mount --bind /mnt/$PARTNAME/data /data

    mkdir -p /mnt/$PARTNAME/hostpath_pv
    mkdir /tmp/hostpath_pv
    mount --bind /mnt/$PARTNAME/hostpath_pv /tmp/hostpath_pv

    mkdir -p /mnt/$PARTNAME/hostpath-provisioner
    mkdir /tmp/hostpath-provisioner
    mount --bind /mnt/$PARTNAME/hostpath-provisioner /tmp/hostpath-provisioner

    if [ -e "/userdata.tar" ]; then
        mv /userdata.tar /var/lib/boot2docker/
    fi

    tar xf /var/lib/boot2docker/userdata.tar -C /home/docker/
    chown -R docker:docker /home/docker/.ssh
    rm -f '/home/docker/boot2docker, please format-me'

    mkdir -p /mnt/$PARTNAME/var/lib/minikube
    mkdir /var/lib/minikube
    mount --bind /mnt/$PARTNAME/var/lib/minikube /var/lib/minikube

    mkdir -p /mnt/$PARTNAME/var/lib/toolbox
    mkdir /var/lib/toolbox
    mount --bind /mnt/$PARTNAME/var/lib/toolbox /var/lib/toolbox

    mkdir -p /mnt/$PARTNAME/var/lib/minishift
    mkdir /var/lib/minishift
    mount --bind /mnt/$PARTNAME/var/lib/minishift /var/lib/minishift

    ## make an env file for other services to discover this PARTNAME easier
    mkdir -p /var/run/minikube
    echo "PERSISTENT_DIR=\"/mnt/$PARTNAME\"" > /var/run/minikube/env
fi

# /etc dirs are initialised from /usr/local, to allow the user/admin to customise
mkdir -p /var/lib/boot2docker/etc/

# Below code is taken from: https://github.com/boot2docker/boot2docker/blob/master/rootfs/rootfs/etc/rc.d/vbox
# VirtualBox Host Mounting
# - this will bail quickly and gracefully if we're not in VBox
if modprobe vboxguest &> /dev/null && modprobe vboxsf &> /dev/null; then
	mountOptions='defaults,iocharset=utf8'
	if grep -q '^docker:' /etc/passwd; then
		mountOptions="${mountOptions},uid=$(id -u docker),gid=$(id -g docker)"
	fi

	# try mounting "$name" (which defaults to "$dir") at "$dir",
	# but quietly clean up empty directories if it fails
	try_mount_share() {
		dir="$1"
		name="${2:-$dir}"

		# normalize "dir" to be definitively root-relative
		# ie, "/Users" and "Users" will both translate to "/Users" explicitly
		dir="/${dir#/}"

		mkdir -p "$dir" 2>/dev/null
		if ! mount -t vboxsf -o "$mountOptions" "$name" "$dir" 2>/dev/null; then
			rmdir "$dir" 2>/dev/null || true
			while [ "$(dirname "$dir")" != "$dir" ]; do
				dir="$(dirname "$dir")"
				rmdir "$dir" 2>/dev/null || break
			done

			return 1
		fi

		return 0
	}

	for line in $(VBoxControl --nologo sharedfolder list -automount | tail -n+3 | cut  -d ' ' -f 3); do
		try_mount_share "$line"
	done
fi

# This script will get called when the machine is started up
# Feel free to add any initialization you need to be ran here
if [ -e /var/lib/boot2docker/bootlocal.sh ]; then
    /bin/sh /var/lib/boot2docker/bootlocal.sh
fi
