#!/bin/bash

# use the static IP directly to avoid DNS failures
VER=1.0.0
SSD=sda
ROOTFS_UUID=4be968ba-b0f2-45c7-b571-18ef21074ee3
RECOVERY_UUID=2767b855-a084-4ac5-9238-1c2529d98214
ROOTFS_START_SECTOR=46759936
ROOTFS_END_MIN_SECTOR=120000000
ROOTFS_FS_MIN_FS=36

serial_number() {
  echo $(dmidecode -s system-serial-number | awk '{print substr($1,6)}')
}

SERIAL_NO=$(serial_number)
# the DMI table is not accessible from chroot, use the hostname
if test -z ${SERIAL_NO}; then
  HOSTNAME=$(hostname)
  SERIAL_NO="${HOSTNAME:1}"
fi

PCASTER_URL=https://plasmacaster.kaijubox.com
EVENT_URL=${PCASTER_URL}/hosts/r${SERIAL_NO}/event

post_info_msg() {
  curl -f -H 'Content-Type: application/json' --data "{\"level\": \"info\", \"name\": \"recovery\",  \"message\": \"$1\"}" -X PUT $EVENT_URL
  sleep 2
}

post_error_msg() {
  curl -f -H 'Content-Type: application/json' --data "{\"level\": \"error\", \"name\": \"recovery\",  \"message\": \"$1\"}" -X PUT $EVENT_URL
  sleep 2
}

# get rootfs block device
ROOTFSPART=`blkid -U $ROOTFS_UUID | grep $SSD`
ROOTFSDISK=${ROOTFSPART::-1}
ROOTFSNUM=${ROOTFSPART: -1}

echo "Rootfs found on disk : $ROOTFSDISK, partition : $ROOTFSNUM"
post_info_msg "[`date`] Rootfs found on disk : $ROOTFSDISK, partition : $ROOTFSNUM"

# validate the size of the rootfs
mount $ROOTFSPART /mnt

ROOTFS_FS_SZ_BEFORE=`df -h -BG --output=size $ROOTFSPART | tail -1 | sed 's/[^0-9]*//g'`
if [ $ROOTFS_FS_SZ_BEFORE -ge $ROOTFS_FS_MIN_FS ]; then
  umount $ROOTFSPART
  echo "ROOTFS [${ROOTFSPART}] validated. Size : ${ROOTFS_FS_SZ_BEFORE}GB"
  post_info_msg "[`date`] ROOTFS [${ROOTFSPART}] validated. Size : ${ROOTFS_FS_SZ_BEFORE}GB"
  exit 0
fi

umount $ROOTFSPART


# the pcspkr and LCD are not accessible from chroot
beep -f 659 -l 460 -n -f 784 -l 340 &>/dev/null
rae-itx-lcd; rae-itx-lcd 1= "Resizing Filesys" 2= "Please Wait!" &>/dev/null

#download partprobe
curl -o /bin/partprobe http://52.71.224.4/rescue/partprobe
chmod 755 /bin/partprobe

echo "Expanding rootfs. Please wait!"
post_info_msg "[`date`] Expanding rootfs. Please wait!"

### check if the rootfs partition is found
if [ -z "$ROOTFSPART" ]; then
  # rootfs not found
  echo "ERROR: ROOTFS not found. Exiting!"
  post_error_msg "[`date`] ERROR: ROOTFS not found. Exiting!"
  exit 1
else
  echo "Resizing rootfs $ROOTFSPART"
  post_info_msg "[`date`] Resizing rootfs $ROOTFSPART"
  sleep 2
  fdisk -u $ROOTFSDISK <<EOF
d                             
$ROOTFSNUM        
                                             
n                                             
                                             
N                        
                                             
w                        
                
EOF

echo "Resize rootfs filesystem to new partition size"
post_info_msg "[`date`] Resize rootfs filesystem to new partition size"

sleep 1
partprobe ${ROOTFSDISK}
sleep 1
e2fsck -fp $ROOTFSPART
sleep 1
resize2fs $ROOTFSPART

fi

echo "Rootfs $ROOTFSPART resized successfullly"
post_info_msg "[`date`] Rootfs $ROOTFSPART resized successfullly"
sleep 2

# verify that the ROOTFS partition was resized
ROOTFS_START=`fdisk -l $ROOTFSDISK | grep ${ROOTFSDISK}7 | tail -1 | awk '{print $2}'`
ROOTFS_END=`fdisk -l $ROOTFSDISK | grep ${ROOTFSDISK}7 | tail -1 | awk '{print $3}'`
if [ $ROOTFS_START -ne $ROOTFS_START_SECTOR ]; then
  echo "ERROR: Invalid ROOTFS start sector."
  post_error_msg "[`date`] ERROR: Invalid ROOTFS start sector."
  echo "$ROOTFS_START_SECTOR expected, $ROOTFS_START found"
  post_error_msg "[`date`] $ROOTFS_START_SECTOR expected, $ROOTFS_START found"
  exit 1
else
  echo "ROOTFS [$ROOTFSDISK] start sector validated : $ROOTFS_START"
  post_info_msg "[`date`] ROOTFS [$ROOTFSDISK] start sector validated : $ROOTFS_START"
fi

if [ $ROOTFS_END -lt $ROOTFS_END_MIN_SECTOR ]; then
  echo "ERROR: Invalid ROOTFS end sector."
  post_error_msg "[`date`] ERROR: Invalid ROOTFS end sector."
  echo "$ROOTFS_END_MIN_SECTOR expected, $ROOTFS_END found"
  post_error_msg "[`date`] $ROOTFS_END_MIN_SECTOR expected, $ROOTFS_END found"
  exit 1
else
  echo "ROOTFS [$ROOTFSDISK] end sector validated : $ROOTFS_END"
  post_info_msg "[`date`] ROOTFS [$ROOTFSDISK] end sector validated : $ROOTFS_END"
fi

# (number of sectors * sector size ) -> convert to GB
# 2097152 = (1024*1024*1024) / 512 = GB / sector size
let ROOTFS_SIZE=($ROOTFS_END-$ROOTFS_START)/2097152
echo -e "\nROOTFS partition size : ${ROOTFS_SIZE}GB"
post_info_msg "[`date`] ROOTFS partition size : ${ROOTFS_SIZE}GB"

# mount ROOTFS for size verification
mount $ROOTFSPART /mnt

# verify that the ROOTFS filesystem was resized
ROOTFS_FS_SZ=`df -h -BG --output=size $ROOTFSPART | tail -1 | sed 's/[^0-9]*//g'`
if [ $ROOTFS_FS_SZ -lt $ROOTFS_FS_MIN_FS ]; then
  echo "ERROR: Invalid ROOTFS file system size."
  post_error_msg "[`date`] ERROR: Invalid ROOTFS file system size."
  echo "Minimum filesystem size $ROOTFS_FS_MIN_FS expected, $ROOTFS_FS_SZ found"
  post_error_msg "[`date`] Minimum filesystem size $ROOTFS_FS_MIN_FS expected, $ROOTFS_FS_SZ found"
  exit 1
else
  echo -e "Mounted ROOTFS [$ROOTFSPART] file system size validated : ${ROOTFS_FS_SZ}GB >= [$ROOTFS_FS_MIN_FS]\n"
  post_info_msg "[`date`] Mounted ROOTFS [$ROOTFSPART] file system size validated : ${ROOTFS_FS_SZ}GB >= [$ROOTFS_FS_MIN_FS]"
fi

umount $ROOTFSPART
rae-itx-lcd; rae-itx-lcd 1= "Filesys Resized" 2= "Please Wait!"
sleep 2
