#!/bin/bash

#
# Read this script and modify it before running. It's not tested as is and is not
# meant to be run unsupervised. 
#

hostname='YOUR_DESIRED_HOSTNAME'
user='YOUR_USERNAME'
hdd_spindown_timeout=1800

###### with bionic already installed, ssh as rock64, pass rock64
# gain superuser privilege
sudo su

# set timezone
timedatectl set-timezone America/New_York

# adduser
adduser "$user"

# add to sudo group
usermod -aG sudo "$user"

###### login as user
# generate ssh folder and keys
ssh-keygen -t rsa -b 4096 -C "rock64"

# add your remote host key to authorized_keys
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
vi ~/.ssh/authorized_keys

# gain superuser access
sudo su

# lock default rock64 account
passwd -l rock64

# update hostname
hostnamectl set-hostname "$hostname"

# install packages
apt-get install -y \
  zfsutils-linux zfs-dkms spl \
  samba \
  debhelper hdparm

#### fix for kernel module builds
#### https://forum.armbian.com/topic/6789-build-zfs-on-rk3328/?tab=comments#comment-53681
# copy kernel source
# NOTE: you may need to change these paths for your kernel version
wget -qO- https://github.com/ayufan-rock64/linux-kernel/archive/4.4.167-1169-rockchip-ayufan.tar.gz | tar xvz -C /usr/src

# copy kernel source tools + scripts folder for builds to work
# NOTE: you may need to change these paths for your kernel version
rsync -a /usr/src/linux-kernel-4.4.167-1169-rockchip-ayufan/tools/ /usr/src/linux-headers-4.4.167-1169-rockchip-ayufan-g3cde5c624c9c/tools/
rsync -a /usr/src/linux-kernel-4.4.167-1169-rockchip-ayufan/scripts/ /usr/src/linux-headers-4.4.167-1169-rockchip-ayufan-g3cde5c624c9c/scripts/

# comment out selinux
# NOTE: read instructions at link, this sed command doesn't work
# sed -i 's/subdir\-\$\(CONFIG_SECURITY_SELINUX\)/#subdir-$(CONFIG_SECURITY_SELINUX)/' /usr/src/linux-headers-4.4.167-1169-rockchip-ayufan-g3cde5c624c9c/scripts/Makefile

# make kernel scripts
# NOTE: you may need to change these paths for your kernel version
cd /usr/src/linux-headers-4.4.167-1169-rockchip-ayufan-g3cde5c624c9c/
make scripts

#### fix for spl kernel module
#### https://forum.armbian.com/topic/6789-build-zfs-on-rk3328/?tab=comments#comment-53681
# remove some KUID lines
# NOTE: read instructions at link, didn't get a chance to script it
# vi /var/lib/dkms/spl/0.7.5/source/configure

# NOTE: path may vary based on spl version
dkms build spl/0.7.5
dkms install --force spl/
modprobe spl

# attempt to install again
apt-get install -y \
  zfsutils-linux zfs-dkms spl \
  samba \
  debhelper hdparm

#### configure samba
#### https://www.computerbeginnersguides.com/blog/2018/04/27/install-and-configure-samba-in-ubuntu-18-04-bionic-beaver/
# add curiouser user
smbpasswd -a "$user"

# add share
cat <<EOF >> /etc/samba/smb.conf
[curiouser-backup]
path = SOME_PATH
available = yes
valid users = SOME_USER
read only = no
browseable = yes
public = no
writable = yes
access based share enum = yes
EOF

#### HDD Power management
## Allow disks to standby after 5 minutes of inactivty
# NOTE: this may be all you need depending on your disks + enclosure, but for my use case
# this doesn't spin the disks down, just uses less power
cat <<EOF >> /etc/rc.local
#!/bin/bash
hdparm -B127 -S60 /dev/sda
hdparm -B127 -S60 /dev/sdb
exit 0
EOF
chmod u+x /etc/rc.local

## Spin down disks after 30 minutes of inactivity using hd-idle
# download, extract and install
wget https://github.com/h31/hd-idle/archive/master.zip \
  && unzip master.zip \
  && cd hd-idle-master \
  && dpkg-buildpackage \
  && dpkg -i ../hd-idle_*.deb

# configure hd-idle to start on boot
sed -i 's/START_HD_IDLE=.*/START_HD_IDLE=true/' /etc/default/hd-idle
# configure hd-idle to not spin down disks by default, but spin down our curiouser-backup after 30 minutes
# NOTE: you'll want to edit the next command to target your disks and for your desired timeout
echo 'HD_IDLE_OPTS="-i 0 -a sda -i $hdd_spindown_timeout -a sdb -i $hdd_spindown_timeout -l /var/log/hd-idle.log"' >> /etc/default/hd-idle

#### Reboot
systemctl reboot