#!/bin/bash



####################################
# NOTE: There is currently no
#       support for host-based
#       mount definitions
#
# WARNING: There is no support for
#          unmounting devices!
####################################


. /usr/lib/lib-fliwi/ymc-common.sh || exit 1


### Include ymclibnettools
. /usr/lib/lib-fliwi/ymc-networktools.bash

if [ $(ymc_is_chroot) -ne 0 ]; then
  echo "WARNING: $0 can not be run inside a chroot..."
  exit 0
fi

ymc_is_device_mounted()
{
  temp_device_to_check=$1
  if [ -L "$temp_device_to_check" ]; then
    temp_resolved_device_path=$(/bin/readlink -e $temp_device_to_check)
  else
    temp_resolved_device_path=$temp_device_to_check
  fi

  if [ $(cat /proc/mounts | cut -d " " -f 1 | grep -c -E -e "^$temp_resolved_device_path\$") -gt 0 ]; then
    echo 1
    return 0
  else
    echo 0
    return 1
  fi
}

ymc_is_mountpoint_mounted()
{
  mountpoint_to_check=$1
  if [ $(cat /proc/mounts | cut -d " " -f 2 | grep -c -E -e "^$mountpoint_to_check\$") -gt 0 ]; then
    echo 1
    return 0
  else
    echo 0
    return 1
  fi
}

ymc_get_mountpoint_for_device()
{
  temp_device_to_check=$1
  if [ -L "$temp_device_to_check" ]; then
    temp_resolved_device_path=$(/bin/readlink -e $temp_device_to_check)
  else
    temp_resolved_device_path=$temp_device_to_check
  fi

  output_mountpoint=$(cat /proc/mounts | cut -d ' ' -f 1-2 | grep -E -e "^$temp_resolved_device_path " | cut -d ' ' -f 2)
  if [ "$output_mountpoint" != "" ] && \
     [ -e "$output_mountpoint" ]; then
    echo "$output_mountpoint"
    return 0
  else
    return 1
  fi
}


fstab="/etc/fstab"
fliwiScriptName=$(basename $0)


### Wash out possibly existing entries added by this script in /etc/fstab
fliwi_modify_file_remove_added_block $fstab "Added by $fliwiScriptName"

### Add start-flag in /etc/fstab
fliwi_modify_file_add_block_start $fstab "Added by $fliwiScriptName"

### handle host-based mounts
for mount_name in $(fliwi-get-my-mounts --show-name)
do
  echo "INFO: Processing (host-)mount '$mount_name'..."
  mount_device=$(fliwi-get-my-mounts --show-device --limit=$mount_name) && \
  mount_mountpoint=$(fliwi-get-my-mounts --show-mountpoint --limit=$mount_name)  && \
  mount_filesystem=$(fliwi-get-my-mounts --show-filesystem --limit=$mount_name) && \
  mount_options=$(fliwi-get-my-mounts --show-options --limit=$mount_name) && \
  mount_dump=$(fliwi-get-my-mounts --show-dump --limit=$mount_name) && \
  mount_pass=$(fliwi-get-my-mounts --show-pass --limit=$mount_name) || false
  if [ $? -ne 0 ] || \
     [ "$mount_device" == "" ] || \
     [ "$mount_mountpoint" == "" ] || \
     [ "$mount_filesystem" == "" ] || \
     [ "$mount_options" == "" ] || \
     [ "$mount_dump" == "" ] || \
     [ "$mount_pass" == "" ]; then
    echo "ERROR: Can not get all required mount-definition from cluster-config for (host-)mount '$mount_name' - skipping"
    continue
  fi

  check_mount_move=0
  need_remount_by_device=0
  need_remount_by_mountpoint=0
  need_mount=0
  if [ $(ymc_is_device_mounted $mount_device) -eq 1 ]; then
    need_remount_by_device=1
    check_mount_move=1
  elif [ $(ymc_is_mountpoint_mounted $mount_mountpoint) -eq 1 ]; then
    need_remount_by_mountpoint=1
    check_mount_move=1
  elif [ $(ymc_is_device_mounted $mount_device) -eq 0 ] || \
       [ $(ymc_is_mountpoint_mounted $mount_mountpoint) -eq 0 ]; then
    if [ $(echo "$mount_options" | grep -c 'noauto') -eq 0 ]; then
      need_mount=1
    fi
  fi

  if [ $check_mount_move -eq 1 ]; then
    currently_mounted_mountpoint=$(ymc_get_mountpoint_for_device $mount_device)
    if [ $? -eq 0 ] && \
       [ "$mount_mountpoint" != "$currently_mounted_mountpoint" ]; then
      if [ ! -d "$mount_mountpoint" ]; then
        mkdir -p "$mount_mountpoint"
        if [ $? -ne 0 ]; then
          echo "WARNING: Failed to create new mountpoint at '$mount_mountpoint' for mount-move while processing (host-)mount '$mount_name'"
          continue
        fi
      fi
      mount --move $currently_mounted_mountpoint $mount_mountpoint
      if [ $? -ne 0 ]; then
        echo "ERROR: Can not move mountpoint from '$currently_mounted_mountpoint' to '$mount_mountpoint' while processing (host-)mount '$mount_name'..."
        continue
      fi
    fi
  fi


  echo "$mount_device $mount_mountpoint $mount_filesystem $mount_options $mount_dump $mount_pass" >> $fstab
  if [ $need_remount_by_device -eq 1 ]; then
    mount -o remount $mount_device || echo "WARNING: Failed to remount '$mount_device' (host-)mount '$mount_name'"
  elif [ $need_remount_by_mountpoint -eq 1 ]; then
    mount -o remount $mount_mountpoint || echo "WARNING: Failed to remount '$mount_mountpoint' (host-)mount '$mount_name'"
  elif [ $need_mount -eq 1 ]; then
    if [ ! -d "$mount_mountpoint" ]; then
      mkdir -p "$mount_mountpoint"
      if [ $? -ne 0 ]; then
        echo "WARNING: Failed to create mountpoint at '$mount_mountpoint' while processing (host-)mount '$mount_name'"
        continue
      fi
    fi
    mount $mount_mountpoint || echo "WARNING: Failed to mount '$mount_name' for current host"
  fi
done


### handle service-based mounts
for my_service in $(fliwi-get-my-services)
do
  for mount_name in $(fliwi-get-mounts -s --show-name $my_service)
  do
    echo "INFO: Processing mount '$mount_name' for services '$my_service'..."
    mount_device=$(fliwi-get-mounts -s --show-device --limit=$mount_name $my_service) && \
    mount_mountpoint=$(fliwi-get-mounts -s --show-mountpoint --limit=$mount_name $my_service)  && \
    mount_filesystem=$(fliwi-get-mounts -s --show-filesystem --limit=$mount_name $my_service) && \
    mount_options=$(fliwi-get-mounts -s --show-options --limit=$mount_name $my_service) && \
    mount_dump=$(fliwi-get-mounts -s --show-dump --limit=$mount_name $my_service) && \
    mount_pass=$(fliwi-get-mounts -s --show-pass --limit=$mount_name $my_service) || false
    if [ $? -ne 0 ] || \
       [ "$mount_device" == "" ] || \
       [ "$mount_mountpoint" == "" ] || \
       [ "$mount_filesystem" == "" ] || \
       [ "$mount_options" == "" ] || \
       [ "$mount_dump" == "" ] || \
       [ "$mount_pass" == "" ]; then
      echo "ERROR: Can not get all required mount-definition from cluster-config for mount '$mount_name' of services '$my_service' - skipping"
      continue
    fi



    check_mount_move=0
    need_remount_by_device=0
    need_remount_by_mountpoint=0
    need_mount=0
    if [ $(ymc_is_device_mounted $mount_device) -eq 1 ]; then
      need_remount_by_device=1
      check_mount_move=1
    elif [ $(ymc_is_mountpoint_mounted $mount_mountpoint) -eq 1 ]; then
      need_remount_by_mountpoint=1
      check_mount_move=1
    elif [ $(ymc_is_device_mounted $mount_device) -eq 0 ] || \
         [ $(ymc_is_mountpoint_mounted $mount_mountpoint) -eq 0 ]; then
      if [ $(echo "$mount_options" | grep -c 'noauto') -eq 0 ]; then
        need_mount=1
      fi
    fi


    if [ $check_mount_move -eq 1 ]; then
      currently_mounted_mountpoint=$(ymc_get_mountpoint_for_device $mount_device)
      if [ $? -eq 0 ] && \
         [ "$mount_mountpoint" != "$currently_mounted_mountpoint" ]; then
        if [ ! -d "$mount_mountpoint" ]; then
          mkdir -p "$mount_mountpoint"
          if [ $? -ne 0 ]; then
            echo "WARNING: Failed to create new mountpoint at '$mount_mountpoint' for mount-move while processing '$mount_name' for services '$my_service'"
            continue
          fi
        fi
        mount --move $currently_mounted_mountpoint $mount_mountpoint
        if [ $? -ne 0 ]; then
          echo "ERROR: Can not move mountpoint from '$currently_mounted_mountpoint' to '$mount_mountpoint' while processing mount '$mount_name' of services '$my_service'..."
          continue
        fi
      fi
    fi


    echo "$mount_device $mount_mountpoint $mount_filesystem $mount_options $mount_dump $mount_pass" >> $fstab
    if [ $need_remount_by_device -eq 1 ]; then
      mount -o remount $mount_device || echo "WARNING: Failed to remount '$mount_device' for services '$my_service'"
    elif [ $need_remount_by_mountpoint -eq 1 ]; then
      mount -o remount $mount_mountpoint || echo "WARNING: Failed to remount '$mount_mountpoint' for services '$my_service'"
    elif [ $need_mount -eq 1 ]; then
      if [ ! -d "$mount_mountpoint" ]; then
        mkdir -p "$mount_mountpoint"
        if [ $? -ne 0 ]; then
          echo "WARNING: Failed to create mountpoint at '$mount_mountpoint' while processing '$mount_name' for services '$my_service'"
          continue
        fi
      fi
      mount $mount_mountpoint || echo "WARNING: Failed to mount '$mount_name' for services '$my_service'"
    fi
  done
done

### Add stop-flag in /etc/fstab
fliwi_modify_file_add_block_end $fstab "Added by $fliwiScriptName"

exit 0
