#!/bin/sh

MODE=$1
MEDIA=$2
UPGR=$3

. /opt/hmc/bin/functions

echo "Initializing filesystems..."
# Determine partition table type in order
# to decide what to do with Swap when it
# comes to upgrade
ptype=$(getPartitionTableType)

# Make sure all udev devices are created
SettleDisk
waitForDevices

RunCmd -noerr mkfs.ext3 -b 4096 -L rootfs ${ROOTPARTITION}
RunCmd -noerr mkfs.ext3 -b 4096 -L var ${VARPARTITION}
# Upgrade from Version lower than V5
if [ "$UPGR" != "Yes" ]; then
   if [ "$MEDIA" != "disk" ]; then
     RunCmd -noerr mkfs.ext3  -b 4096 -L /hmcdump ${HMCDUMPPARTITION}
   else
     # Make sure hmcboot partition is labeled properly
     RunCmd -noerr tune2fs -L /hmcboot ${HMCBOOTPARTITION}
   fi
fi

# Remove VG to start fresh if not doing an upgrade
# otherwise if VG found, activate it
create_new_vg=0
pvs | grep -q "HMCDataVG"
if [ $? -eq 0 ]; then
  if [ "$UPGR" != "Yes" ]; then
     removeVolumeGroup
     create_new_vg=1
  else
     activateVolumeGroup
  fi
else
# This is the condition where you come from a brand new installation
# with a disk without a prior HMCDataVG or you can upgrade from an
# HMC level without a Volume Group.
  create_new_vg=1
fi
if [ $create_new_vg -eq 1 ]; then
    echo "Creating Physical Volumes.."
    RunCmd pvcreate -ff -y --zero y ${LVMPV1}
    RunCmd pvcreate -ff -y --zero y ${LVMPV2}

    echo "Creating Volume Group..."
    RunCmd vgcreate HMCDataVG ${LVMPV1} ${LVMPV2}
    RunCmd echo "RC: $?"
    RunCmd vgscan
    RunCmd vgdisplay
    CreateLogicalVolumes
fi

waitForDevices
if [ "$UPGR" != "Yes" ]; then
    # Disk boot and install cannot switch to parted
    # because we are using partition 5 and 6
    if [[ "$MEDIA" == "disk" && "$ptype" = "msdos" && "$MARCH" != "ppc64le" ]]; then
       RunCmd mkswap -L Swap ${OLD_SWAPPARTITION}
    else
       RunCmd mkswap -L Swap ${SWAPPARTITION}
    fi
else
    # Upgrade from gpt base partition use new swap value
    # else stick with the old value
    if [ "$ptype" = "gpt" ]; then
       RunCmd mkswap -L Swap ${SWAPPARTITION}
    else
       RunCmd mkswap -L Swap ${OLD_SWAPPARTITION}
    fi
fi

if [ "$MODE" = "Install" ]; then
    echo "Initializing logical volumes .."
    if [ "$MEDIA" != "disk" ]; then
      RunCmd -noerr mkfs.ext3  -b 4096 -L /hmcboot ${HMCBOOTPARTITION}
    fi
    RunCmd -noerr mkfs.ext4  -b 4096 -L upgrade /dev/HMCDataVG/UpgradeLV
    RunCmd -noerr mkfs.ext4  -b 4096 -L data /dev/HMCDataVG/DataLV
else
# see if there is upgrade data present in the old partition
# if so see if you are upgrading from an older version
   mkdir -p /upgr
   mount -t ext3 ${HMCBOOTPARTITION} /upgr
   if [[ -f /upgr/SaveHSCSystemUpgradeData.tar && -f /upgr/doRestore ]]; then
# Format these 2 partitions prior to copy data over
# This is the only time that upgrade and data will be initialized
# subsequent upgrade from this level onward, upgrade and data will not be
# formatted unless on an Install
      RunCmd -noerr mkfs.ext4  -b 4096 -L upgrade /dev/HMCDataVG/UpgradeLV
      RunCmd -noerr mkfs.ext4  -b 4096 -L data /dev/HMCDataVG/DataLV
      echo "Copying data to new upgrade partition.."
      mkdir -p /newupgr
      mount -t ext4 /dev/HMCDataVG/UpgradeLV /newupgr
      cp -a /upgr/* /newupgr
      if [ $? -eq 0 ]; then
	 rm -rf /upgr/*
      fi
      umount /newupgr
   fi
   umount /upgr
# Now for upgrade make sure we re-label partitions that we are not
# reformatting due to presence of data. hmcboot and hmcdump partitions
# retain the '/' in the label because we need to support installing
# older version of HMC from disk, in which case the older install
# code relies on /hmcboot and /hmcdump labels
   for lbl in '/upgrade' '/data'; do
      partname=$(blkid -L $lbl)
      if [ -n "$partname" ]; then
          newlbl=$(echo $lbl | tr -d "/")
          tune2fs -L $newlbl $partname
      fi
   done
fi
echo "Creating additional filesystems..."
RunCmd -noerr mkfs.ext4  -b 4096 -L dump /dev/HMCDataVG/DumpLV
# Still initialize home as we will restore content back
# from upgrade data
RunCmd -noerr mkfs.ext4  -b 4096 -L home /dev/HMCDataVG/HomeLV
RunCmd -noerr mkfs.ext4  -b 4096 -L extra /dev/HMCDataVG/ExtraLV
RunCmd -noerr mkfs.ext4  -b 4096 -L log /dev/HMCDataVG/LogLV

RunCmd -noerr mkdir -p /hmc
RunCmd -noerr mount -t ext3 ${ROOTPARTITION} /hmc
RunCmd -noerr mkdir -p /hmc/var
RunCmd -noerr mkdir -p /hmc/var/hsc
RunCmd -noerr mkdir -p /hmc/hmcboot
RunCmd -noerr mkdir -p /hmc/data
RunCmd -noerr mkdir -p /hmc/extra
RunCmd -noerr mkdir -p /hmc/hmcdump
RunCmd -noerr mkdir -p /hmc/dump
RunCmd -noerr mount -t ext3 ${VARPARTITION} /hmc/var
diskSize=`getDiskSize`
if [[ $diskSize -ge 80 && $diskSize -lt 120 ]]; then
    MOUNT_HOME_LV=0
else
    MOUNT_HOME_LV=1
fi
if [ $MOUNT_HOME_LV -eq 1 ]; then
  RunCmd -noerr mkdir -p /hmc/home
  RunCmd -noerr mount -t ext4 /dev/HMCDataVG/HomeLV /hmc/home
else
  # Update fstab here to remove home mount point if needed
  if [ -f /opt/hmc/data/fstab ]; then
     sed -e "/^LABEL=home/d" /opt/hmc/data/fstab > /opt/hmc/data/fstab.new
     mv /opt/hmc/data/fstab.new /opt/hmc/data/fstab
  fi
fi
mkdir -p /hmc/var/hsc/log
RunCmd -noerr mount -t ext4 /dev/HMCDataVG/LogLV /hmc/var/hsc/log
RunCmd -noerr mount -t ext4 /dev/HMCDataVG/ExtraLV /hmc/extra
RunCmd -noerr mount -t ext4 /dev/HMCDataVG/DumpLV /hmc/dump
RunCmd -noerr mount -t ext4 /dev/HMCDataVG/DataLV /hmc/data
RunCmd -noerr mkdir -p /hmc/data/hmc/profiles
RunCmd -noerr ln -sf /data/hmc/profiles /hmc/var/hsc/
# Check for presence of external xData
# First mount and see if we are successful

mkdir -p /hmc/xData
mount -t ext4 -L xData /hmc/xData 2>/dev/null
if [ $? -eq 0 ]; then
  mount -o bind /hmc/xData /hmc/data
  grep -q "LABEL=xData" /opt/hmc/data/fstab
  if [ $? -ne 0 ]; then
     echo "LABEL=xData  /xData        ext4     defaults 1 2" >> /opt/hmc/data/fstab
     echo "/xData       /data         none     bind  0 0" >> /opt/hmc/data/fstab
  fi
else
  rmdir /hmc/xData
fi
