#!/bin/sh

doreplicator()
 {
  echo ""
  echo "###########################################################################"
  echo "## Now, you will have to create a user called \"replicator\" that is       ##"
  echo "## going to do the actual replicating for you. Type in \"wheel\" when      ##"
  echo "## asked for \"Login group\"                                               ##"
  echo "###########################################################################"
  echo ""
  echo -n "Press [Enter] key to continue..." && read null
  adduser
  touch /var/log/replicate.log
  chown replicator /var/log/replicate.log
  echo ""
  while true
     do
      read -r -p 'Are you going to be doing remote replication? "Y|n"' choice
       case "$choice" in
        y|Y) echo ""
             echo "###########################################################################"
             echo "## Creating ssh-key for replicator to use to log into target machine     ##"
             echo "## with. Just press enter when it asks for anything and keep the         ##"
             echo "## password blank.                                                       ##"
             echo "###########################################################################"
             echo ""
             echo -n "Press [Enter] key to continue..." && read null
             su replicator -c ssh-keygen
             echo ""
             echo "###########################################################################"
             echo "## You will also need to create a \"replicator\"-user on the target        ##"
             echo "## system and insert this into \"~/.ssh/authorized_keys\"                  ##"
             echo "###########################################################################"
             su replicator -c "cat ~/.ssh/id_rsa.pub"
             echo ""
             echo -n "Press [Enter] key to continue..." && read null
             echo "###########################################################################"
             echo "## Lastly, you will have add this into the target machine sudoers file,  ##"
             echo "## to allow \"replicator\" to do everything it is supposed to. Change      ##"
             echo "## the name of the pool accordingly!                                     ##"
             echo "##                                                                       ##"
             echo "## Worst case, this allows replicator to destroy EVERYTHING it has       ##"
             echo "## replicated into the target, with the \"root\" file system as an         ##"
             echo "## exception, so that the target system itself never is at risk, only    ##"
             echo "## what has been replicated into it, which can always be replicated back ##"
             echo "## again, in case of a breach.                                           ##"
             echo "###########################################################################"
             echo ""
             echo "replicator ALL=(ALL) NOPASSWD: /sbin/zfs recv *"
             echo "replicator ALL=(ALL) NOPASSWD: /sbin/zfs destroy *@remote_replicate.base"
             echo "replicator ALL=(ALL) NOPASSWD: /sbin/zfs rename *@remote_replicate.delta *@remote_replicate.base"
             echo "replicator ALL=(ALL) NOPASSWD: /sbin/zfs destroy pool/*"
             echo "replicator ALL=(ALL) NOPASSWD: /sbin/zfs destroy * pool/*"
             echo "replicator ALL=(ALL) NOPASSWD: !/sbin/zfs destroy pool/root*"
             echo "replicator ALL=(ALL) NOPASSWD: !/sbin/zfs destroy * pool/root*"
             echo ""
             echo -n "Press [Enter] key to continue..." && read null
             break ;;
        n|N) break ;;
          *) break ;;
       esac
     done
 }

docron()
 {
  echo "###########################################################################"
  echo "## This has now been inserted into your \"/etc/crontab\". To have          ##"
  echo "## automatic, scheduled replication, uncomment that line and it will run ##"
  echo "## in the background five minutes past every hour. Enjoy!                ##"
  echo "###########################################################################"
  echo "#5       */1     *       *       *       root    /usr/local/bin/replicate" | tee -a /etc/crontab
 }

if [ ! `whoami` = "root" ]
 then
  echo "You must be root to install this."
  exit
 else
  read -r -p "To what email address do you want replicate to send error alerts?: " address
  sed -i '' "s/address=\"\"/address=\"$address\"/" ./work/replicate
  cp ./work/replicate /usr/local/bin/replicate
  chmod 755 /usr/local/bin/replicate
  mkdir /usr/local/etc/replicatejobs >> /dev/null 2>&1
  cp ./work/sample_job ./work/job1 /usr/local/etc/replicatejobs
  chmod -R 644 /usr/local/etc/replicatejobs
  cp ./work/replicate.1.gz /usr/share/man/man1/
  makewhatis
  echo ""
  echo "###########################################################################"
  echo "## Beginning by showing off the sample job so you get a chance to see an ##"
  echo "## example of how its supposed to look. When done, press \"q\" on the      ##"
  echo "## keyboard to take you into editing your first job-file.                ##"
  echo "###########################################################################"
  echo -n "Press [Enter] key to continue..." && read null
  less /usr/local/etc/replicatejobs/sample_job
  ee /usr/local/etc/replicatejobs/job1
  if grep -q "\replicator\b" /etc/passwd
   then
    echo ""
    echo "###########################################################################"
    echo "## Found another user called \"replicator\". We have to remove that        ##"
    echo "## before we can continue, to make it sure it gets done right.           ##"
    echo "###########################################################################"
    echo ""
    echo -n "Press [Enter] key to continue..." && read null
    rmuser replicator
    doreplicator
   else
    doreplicator
  fi
fi

if grep -q "\replicate\b" /etc/crontab
 then
  sed -i '' '/replicate/d' /etc/crontab
  docron
 else
  docron
fi
