#!/usr/local/bin/bash

#
# Copyright (c) 2016 Joyent Inc., All rights reserved.
#
# formats the FreeeBSD secondary disk ( /dev/vtbd1p1 )
# also refered to as the /data disk

# Users can provision this disk how they like
# By default we provision the whole disk as one partition

# load common functions and vars
. /usr/local/lib/smartdc/common.lib

DATADISKDEVICE='vtbd1'
GPART=/sbin/gpart

checkformount() {
   fstest=$(df -h | grep "/dev/${DATADISKDEVICE}p1")
   if [[ -n "$fstest" ]] ; then
     fssize=$(echo $fstest | sed -E '/[[:space:]]{2,}/s// /g' | cut -d ' ' -f2)
     lib_triton_info "$fssize data disk is mounted on /dev/${DATADISKDEVICE}p1"
     return 1
   else
     lib_triton_info "no data disk is mounted on /dev/${DATADISKDEVICE}p1"
     return 0
   fi
}

# Start of Main

# check for device
if [[ ! -e /dev/${DATADISKDEVICE} ]] ; then
  lib_triton_fatal "secondary disk '/dev/${DATADISKDEVICE}' not found. exiting."
fi

if [[ -z $GPART ]] ; then
  lib_triton_fatal "gpart binary not found. exiting."
fi

## Sanity check
checkformount
return_val=$?
if [[ "$return_val" -eq 1 ]]; then
  lib_triton_fatal "data disk is already mounted"
else 
  lib_triton_info "no data disk is mounted"
fi

## make sure that a partiton does not already exist
## we do not want to delete any data
partexists=$($GPART show /dev/${DATADISKDEVICE} | grep "1  freebsd-ufs" | wc -l | tr -d ' ')
if [[ $partexists -ne 0 ]] ; then
    lib_triton_fatal "Data disk partition '/dev/${DATADISKDEVICE}p1' found. Exiting to prevent deleting data. You will have to format data disk partition manually."
    exit 0;
fi

## create the data disk

# making partition table
lib_triton_info "creating gpt partition type /dev/${DATADISKDEVICE}"
gpart create -s gpt $DATADISKDEVICE

DISKSIZE=$(gpart show /dev/${DATADISKDEVICE} | grep free | cut -d'-' -f3 | cut -d'(' -f2 | cut -d')' -f1)

lib_triton_info "disksize is $DISKSIZE on $DATADISKDEVICE"

lib_triton_info "creating partition /dev/${DATADISKDEVICE}"

# need this sleep to let partition table to update
# if not then /dev/vdb1 will not exist
lib_triton_info "sleeping for update of partition table for /dev/${DATADISKDEVICE}"
sleep 2

# creating a single partition for the whole disk, formatting it, and mounting it
gpart add -t freebsd-ufs ${DATADISKDEVICE} 

if [[ -e /dev/${DATADISKDEVICE}p1 ]] ; then
   lib_triton_info "creating new filesystem on /dev/${DATADISKDEVICE}p1"
   newfs -L DATA /dev/${DATADISKDEVICE}p1
else
   lib_triton_fatal "did not create filesystem on /dev/${DATADISKDEVICE}p1"
fi

# Check for /data and make it
if [[ ! -e /data ]]; then
    lib_triton_info "making /data dir mount point"
    mkdir /data
fi

# add entry to fstab so data disk is mounted on reboot
fsentry=$(grep "/dev/${DATADISKDEVICE}p1" /etc/fstab | wc -l | tr -d ' ')
echo "-- $fsentry --"
if [[ $fsentry -eq 0 ]] ; then
  lib_triton_info "adding fstab entry for /dev/${DATADISKDEVICE}p1"
  printf "/dev/${DATADISKDEVICE}p1\t/data\t\tufs\trw\t2\t2\n" >> /etc/fstab
else
  lib_triton_info "not adding as fstab entry already exist for /dev/${DATADISKDEVICE}p1"
fi

# fsck disk
lib_triton_info "fscking /dev/${DATADISKDEVICE}p1"
fsck -y -t ufs /dev/${DATADISKDEVICE}p1

# enable journeling
lib_triton_info "enabling journeling /dev/${DATADISKDEVICE}p1"
tunefs -j enable /dev/${DATADISKDEVICE}p1

# mount the data disk
lib_triton_info "mounting /dev/${DATADISKDEVICE}p1 as /data"
mount /data

checkformount
return_val=$?
if [[ "$return_val" -eq 1 ]]; then
  lib_triton_info "data disk is mounted"
else 
  lib_triton_fatal "no data disk is mounted"
fi

exit 0
