#! /bin/bash

cloud(){
  mkdir -p ~/cloud;
  if [ "$1" == "nfs" ]; then
    sudo mount -t nfs gameroom:/srv/disks ~/cloud
  else
    sshfs -o reconnect,ServerAliveInterval=15,ServerAliveCountMax=3,Compression=no,auto_cache "$SERVER_USR"@"$SERVER_NAME":/mnt/disks ~/cloud
  fi
}

install_cloud(){
  username=`whoami`
  read -s -p "[sudo] password for $username: " sudoPW
  echo ""
  read -s -p "Password for $SERVER_USR@$SERVER_NAME: " masterPW
  ## Download dependencies
  if ! $(dpkg -l | grep -q "sshfs"); then
    echo "$sudoPW" | sudo -S apt update && sudo apt install sshfs -y
  fi
  ## Necessary to generate /etc/exports
  if ! $(dpkg -l | grep -q "nfs-kernel-server"); then
    echo "$sudoPW" | sudo -S apt update && sudo apt install nfs-server -y
  fi
  ## emulates RAID 0
  if ! $(dpkg -l | grep -q "mhddfs"); then
    echo "$sudoPW" | sudo -S apt update && sudo apt install mhddfs -y
  fi

  ## share keys with master
  echo "$sudoPW" | sudo -S -u $SERVER_USR echo "$masterPW" | upload_key
  rm -f /tmp/cloud_fstab
  rm -f /tmp/cloud_exports
  wget inscribe.productions/misc/cloud_fstab -P /tmp/
  wget inscribe.productions/misc/cloud_exports -P /tmp/
  echo "$sudoPW" | sudo -S cp /etc/fstab /etc/fstab.old
  echo "$sudoPW" | sudo -S cp /etc/fstab /etc/fstab.new
  echo "$sudoPW" | sudo -S cp /etc/exports /etc/exports.old
  echo "$sudoPW" | sudo -S cp /etc/exports /etc/exports.new

  echo "$sudoPW" | sudo -S chmod 777 /etc/fstab.new
  echo "$sudoPW" | sudo -S chmod 777 /etc/exports.new
  echo "$sudoPW" | sudo -S sed -i '/# <cloud>/,/# <\/cloud>/d' /etc/fstab.new
  echo "$sudoPW" | sudo -S sed -i '/# <cloud>/,/# <\/cloud>/d' /etc/exports.new
 
  echo "$sudoPW" | sudo -S cat /tmp/cloud_fstab >> /etc/fstab.new
  echo "$sudoPW" | sudo -S cat /tmp/cloud_exports >> /etc/exports.new
  if $(df | grep -q "/srv/disks"); then
    echo "$sudoPW" | sudo -S umount /srv/disks
  fi
  echo "$sudoPW" | sudo -S mkdir -p /srv/disks
  echo "$sudoPW" | sudo -S chmod 755 /srv/disks
  
  if $(df | grep -q "/mnt/disks"); then
    echo "$sudoPW" | sudo -S umount /mnt/disks/*
  fi
  echo "$sudoPW" | sudo -S mkdir -p /mnt/disks
  echo "$sudoPW" | sudo -S chown nobody /mnt/disks
  echo "$sudoPW" | sudo -S mkdir -p /mnt/disks/disk_1
  echo "$sudoPW" | sudo -S mkdir -p /mnt/disks/disk_2
  echo "$sudoPW" | sudo -S mkdir -p /mnt/disks/disk_3
  echo "$sudoPW" | sudo -S chmod -R 755 /mnt/disks

  if $(grep -q "<cloud>" /etc/fstab.new); then
    echo "$sudoPW" | sudo -S mv /etc/fstab.new /etc/fstab
    echo "$sudoPW" | sudo -S chmod 664 /etc/fstab
    echo "$sudoPW" | sudo -S mount -a
    rm -f ~/cloud
    ln -s /srv/disks ~/cloud
  else
    echo "Failed to update fstab"
    exit 1
  fi
  if $(grep -q "<cloud>" /etc/exports.new); then
    echo "$sudoPW" | sudo -S mv /etc/exports.new /etc/exports
    echo "$sudoPW" | sudo -S chmod 644 /etc/exports
  else
    echo "Failed to update exports"
    exit 1
  fi
  echo "$sudoPW" | sudo -S exportfs -a
  ssh $SERVER_USR@$SERVER_NAME "echo $masterPW | sudo -S mount -a"
}
ucloud(){
  fusermount -u ~/cloud
}
