#!/usr/bin/with-contenv sh

cd /etc/swift
DEV_SIZE="1GB"
# POLICIES="object container account"
MY_STORAGE_TYPE=${STORAGE_TYPE:-"internal_dirs"}
MY_DEVICE_COUNT=${DEVICE_COUNT:-6}

echo "[[ checking --privileged ]]"
ip link add dummy0 type dummy >/dev/null
if [[ $? -eq 0 ]]; then
  PRIVILEGED=true
  # clean the dummy0 link
  ip link delete dummy0 >/dev/null
else
  PRIVILEGED=false
fi

echo "storage type is $MY_STORAGE_TYPE. container is privileged? $PRIVILEGED"

echo "[[ checking what to use as storage devices ]]"
DEVICE_LIST=""
if [[ $MY_STORAGE_TYPE == "external_devices" ]]; then
  DEVICE_LIST=$(ls /dev/ | grep -i "swift-d")
  MY_DEVICE_COUNT=$(wc -w $DEVICE_LIST)
  echo "  using external device. devices found: $DEVICE_LIST"
elif [[ $MY_DEVICE_COUNT -le 0 ]]; then
  echo "Device count must be greater than 0"
  exit -1
else
  for i in $(seq 0 $(( MY_DEVICE_COUNT-1 ))); do
    DEVICE_LIST="$DEVICE_LIST swift-d$i"
  done
  # echo "  using internal devices. devices to create: $DEVICE_LIST"
fi

if [[ $MY_STORAGE_TYPE == "internal_devices" ]]; then
  for device in $DEVICE_LIST; do
    truncate -s $DEV_SIZE /dev/$device;
    echo "    created storage device /dev/swift-d$i of $DEV_SIZE";
  done
fi

export PATH=$PATH:/opt/python/usr/local/bin/

echo "[[ creating directories ]]"
for dir in $DEVICE_LIST; do
  mkdir -p /srv/node/$dir;
  echo "  created /srv/node/$dir";
done

if [[ $MY_STORAGE_TYPE == "internal_devices" ]] || [[ $MY_STORAGE_TYPE == "external_devices" ]]; then
  echo "[[ formating and mounting storage devices ]] "
  for device in $DEVICE_LIST; do
    # truncate -s $DEV_SIZE /dev/swift-d$i;
    # echo "created storage device /dev/swift-d$i of $DEV_SIZE";
    mkfs.xfs -f -L D$i -i size=512 /dev/$device;
    echo "  created XFS file system on device /dev/$device";
    mount -t xfs -o noatime /dev/$device /srv/node/$device;
    echo "  mounted /dev/$device as /srv/node/$device";
  done
fi
