#!/bin/bash

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

lvm_group_prefix="fliwi_lvm_"
lvm_volume_drbd_prefix="hadrbdmeta_"
LVM_LOGICAL_VOLUME_PREFIX='service_'

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

run_mode=$1

do_remove_links=0
do_add_links=0
do_remove_all_auto_meta_disks=0


if [ "$run_mode" == 'deconfigure' ]; then
  do_remove_links=1
  do_remove_all_auto_meta_disks=1
elif [ "$run_mode" == 'configure' ]; then
  do_remove_links=1
  do_add_links=1
else
  echo "Usage: # $(basename $0) configure|deconfigure"
  exit 1
fi

if [ ! -d "/etc/drbd.d" ]; then
  echo "ERROR: Directory '/etc/drbd.d/' does not exists"
  exit 1
fi


if [ $do_remove_links -eq 1 ]; then
  rm -f /etc/drbd.d/fliwi_hadrbd_*.res
fi

if [ $do_remove_all_auto_meta_disks -eq 1 ]; then
  if [ -x "/etc/init.d/drbd" ]; then
    /etc/init.d/drbd stop
  fi
  lvremove -f /dev/$lvm_group_prefix*/$lvm_volume_drbd_prefix*
fi

if [ $do_add_links -eq 1 ]; then
  for my_numbered_service in $(fliwi-get-my-services)
  do
    my_service=$(echo $my_numbered_service | sed -r 's/-[0-9]+$//')
    if [ "$my_service" == "" ]; then
      continue
    fi

    fliwi-config-update --service $my_service || true

    source_config_file="/etc/fliwi/services/$my_service/ha_drbd/drbd.conf"
    if [ ! -f "$source_config_file" ]; then
      continue
    fi

    target_config_file="/etc/drbd.d/fliwi_hadrbd_$my_service.res"
    if [ -e "$target_config_file" ]; then
      rm -f $target_config_file || exit 1
    fi

    ln -s $source_config_file $target_config_file

    ### Check if we deal with a meta-disk under control of Fliwi
    fliwi_controlled_meta_disks=$(cat $target_config_file | \
                                       grep -E -e '^    flexible-meta-disk "/dev/'$lvm_group_prefix'[^/]+/'$lvm_volume_drbd_prefix''$my_service'_[a-z0-9-]+";' | \
                                       sort | uniq | \
                                       sed -r 's/"//g' | \
                                       cut -d '/' -f 2- | \
                                       cut -d ';' -f 1)
    for fliwi_controlled_meta_disk in $fliwi_controlled_meta_disks
    do
      fliwi_controlled_meta_disk='/'$fliwi_controlled_meta_disk
      if [ -b "$fliwi_controlled_meta_disk" ]; then
        continue
      fi

      if [ -e "$fliwi_controlled_meta_disk" ]; then
        echo "ERROR: Something exists at '$fliwi_controlled_meta_disk' but it is not a block-device - skipping..."
        continue
      fi

      if [ ! -e "$(dirname $fliwi_controlled_meta_disk)" ]; then
        echo "ERROR: LVM-group of '$fliwi_controlled_meta_disk' seems to be non-existing - skipping..."
        continue
      fi

      lvm_group=$(echo $fliwi_controlled_meta_disk | cut -d '/' -f 3)
      if [ "$lvm_group" == "" ]; then
        echo "ERROR: Can't determine the volume-group of '$fliwi_controlled_meta_disk' - skipping..."
        continue
      fi

      meta_disk_name=$(echo $fliwi_controlled_meta_disk | cut -d '/' -f 4)
      if [ "$meta_disk_name" == "" ]; then
        echo "ERROR: Can't determine the volume-name of '$fliwi_controlled_meta_disk' - skipping..."
        continue
      fi

      disk_name=$(echo $meta_disk_name | sed -r "s|^$lvm_volume_drbd_prefix|$LVM_LOGICAL_VOLUME_PREFIX|")
      if [ "$disk_name" == "" ] ||
         [ "$disk_name" == "$meta_disk_name" ]; then
        echo "ERROR: Can't determine the volume-name the meta disk '$fliwi_controlled_meta_disk' is used for - skipping..."
        continue
      fi

      fliwi_controlled_disk_without_dev="$lvm_group/$disk_name"
      if [ ! -b "/dev/$fliwi_controlled_disk_without_dev" ]; then
        echo "ERROR: The volume '/dev/$fliwi_controlled_disk_without_dev' is  not a block device - skipping..."
        continue
      fi

      resource_name=$(echo $fliwi_controlled_meta_disk | sed -r 's|^/dev/'$lvm_group'/'$lvm_volume_drbd_prefix'('$my_service'_.*)$|\1|')
      if [ "$resource_name" == "" ] || \
         [ "$resource_name" == "$fliwi_controlled_meta_disk" ]; then
        echo "ERROR: Can't determine the resource-name of '$fliwi_controlled_meta_disk' - skipping..."
        continue
      fi

      meta_disk_size=128
      minimum_meta_disk_size=$(ymc_simple_calc $(ymc_get_disk_size $fliwi_controlled_disk_without_dev) / 1024 / 1024 / 32768 + 2 | sed -r 's/\..*//')
      if [ $(ymc_simple_calc "($minimum_meta_disk_size * 4) > $meta_disk_size") -eq 1 ]; then
        meta_disk_size=$(ymc_simple_calc "($minimum_meta_disk_size - 2) * 4 + 2")
      fi

      lvcreate -L "$meta_disk_size"MB -n $meta_disk_name $lvm_group
      if [ $? -ne 0 ]; then
        echo "ERROR: Failed to create LVM volume '$meta_disk_name' in group '$lvm_group' with size '$meta_disk_size' MB - skipping..."
        continue
      fi
      dd if=/dev/zero of=/dev/$lvm_group/$meta_disk_name bs=1M count=$meta_disk_size
      drbdadm create-md $resource_name
    done


  done
fi

exit 0
