#!/bin/bash

export RCLONE_CONFIG=/opt/rclone/rclone.conf 
SYSTEMD_PATH="/etc/systemd/system"
SYSTEMD_ENV_PATH="/etc/systemd/env_variables"
SHARE_NAMES="dbc_media vmx-nas kur"

# ****************
# Install mergerfs
# ****************
PACKAGE_DOWNLOAD="trapexit/mergerfs" 
CODENAME=$(dpkg --status tzdata|grep Provides|cut -f2 -d'-')
ARCHI=$(dpkg --print-architecture)
DOWNLOAD_URL=$(curl -s https://api.github.com/repos/{$PACKAGE_DOWNLOAD}/releases/latest | grep browser_download_url | grep debian-bookworm_amd64.deb | grep -wv sha256 | cut -d '"' -f 4)
wget $DOWNLOAD_URL -O app.deb
sudo dpkg -i app.deb
sudo rm -f app.deb
mergerfs --version

# ****************
# Install rclone
# ****************
curl https://rclone.org/install.sh | sudo bash 

# ****************
# Dirs and Files
# ****************
sudo apt install fuse3 
sudo sed -r -i.orig 's/#user_allow_other/user_allow_other/g' /etc/fuse.conf 

sudo mkdir -p /etc/systemd/env_variables

sudo mkdir -p /opt/rclone \
    /mnt/rclone_mount/rclone_{vmx-nas,dbc_media,kur} \
    /mnt/unionfs  \
    /rclone_cache \
    /var/log/rclone

sudo touch /var/log/rclone/rclone_{vmx-nas,dbc_media,kur}.log 

sudo chown root:sysadmin -R /opt/rclone \
    /mnt/rclone_mount \
    /mnt/unionfs  \
    /rclone_cache \
    /var/log/rclone

sudo chmod 770 -R /opt/rclone \
    /mnt/rclone_mount \
    /mnt/unionfs  \
    /rclone_cache \
    /var/log/rclone

# ****************
# rclone config
# ****************
sudo bash -c "cat > /opt/rclone/rclone.conf" << EOF
[db_media]
type = dropbox
client_id = hctzhzddd4ogjb5
client_secret = 19hlbaaqqlwbmfd
token = {"access_token":"sl.BjSEX-a_GDwPlykGUWw4kSDdB-TWJ_OtDdbSmNfb2oSYxbdN927eQpG82ldpExzWijfa3kkfWl62qqBc1s4w3oOfipp-3TzApUSKjhjIIEY0PX217Iazn0d5cF8V-L9WqLS3VSuwgauUuSM","token_type":"bearer","refresh_token":"QWoUGdBf7qEAAAAAAAAAAaTTCHl5Rs5icHFhuBmA1QsyNTlmcFCrkRCvmF7hrgct","expiry":"2023-08-01T20:49:04.366296+02:00"}

[dbc_media]
type = crypt
remote = db_media:
password = m4z9aehA9h9fMEtS-mdjuPgG6dVUhyR6H9MCSnEIDu1H6GzN4Hw0zOwEc3vTXjnJQItWaDf79cwSPE6AVQLXWA4OFBNShY9_NGEgKmfaGRQvyC8ZZqZNNVvedesgO5X-ZiorF8pbWMAyoQJoEt-LJxcDHAtFLg
password2 = nTHL7MSirwYzVTiCf_RIbbcxXrD6PT-8DzaBj2QD0YJA_Lwfpx20fp5YJ63aitprQSFzkuu4DcubAw2OEf4gJcXQCLKotRNxI_bu0-8Yv9kUGZeFNzuRW-Tm7e7i2X88AWIbvC_imS6CCC0k114Ll-rDifjs_y9B

[kur]
type = smb
host = kur.swallow-grue.ts.net
user = smb_user
pass = imHo72GY93bIXnxDqT4Nm_I0W_JQvMCAjXpt0OxxR7BOHYJl0CYxjkDFta40vJrJnuiQRDZPT2rwKiOwjJrXug
domain = VMX

[vmx-nas]
type = smb
host = vmx-nas.swallow-grue.ts.net
user = smb_user
pass = 2z3ki1ZRY_xW7RpY46jPgCF8R4M3gsShmuszY_DouqFOZ-P9xwHk5Wpz5sZb8dFwULS6PnnB5OXmX7L9HrQDFA
domain = VMX
EOF

# ****************
# services
# ****************
sudo bash -c "cat > /etc/systemd/env_variables/rclone.env" << EOF
RCLONE_LOG_DIR="/var/log/rclone"
RCLONE_CONFIG_DIR="/opt/rclone"
RCLONE_CACHE_DIR="/rclone_cache"
RCLONE_CONFIG="/opt/rclone/rclone.conf"
RCLONE_USER="root"
RCLONE_GROUP="sysadmin"
RCLONE_LOG_LEVEL="NOTICE"
RCLONE_DIR_CACHE_TIME="30m"
RCLONE_CACHE_MAX_SIZE="1G"
RCLONE_CACHE_MAX_AGE="30m"
RCLONE_CACHE_MODE="full"

RCLONE_UMASK=000
EOF

SHARE_NAMES="dbc_media vmx-nas kur"
for SHARE_NAME in $SHARE_NAMES
do

sudo bash -c "cat > '$SYSTEMD_ENV_PATH'/rclone_'$SHARE_NAME'.env" << EOF
RCLONE_MOUNTDRIVE="$SHARE_NAME:"
RCLONE_MOUNTPOINT="/mnt/rclone_mount/rclone_$SHARE_NAME"
RCLONE_LOGFILE="/var/log/rclone/rclone_$SHARE_NAME.log"
RCLONE_CACHE_DIR="/rclone_cache/rclone_$SHARE_NAME"
EOF

sudo bash -c "cat > '$SYSTEMD_PATH'/rclone_'$SHARE_NAME'.service"<< \EOF
[Unit]
Description=Rclone MYSHARENAME service
Wants=network-online.target
After=network-online.target

[Service]
Type=notify
EnvironmentFile=/etc/systemd/env_variables/rclone.env
EnvironmentFile=/etc/systemd/env_variables/rclone_MYSHARENAME.env
Environment=RCLONE_CONFIG=${RCLONE_CONFIG}
RestartSec=60
ExecStart=/usr/bin/rclone mount ${RCLONE_MOUNTDRIVE} ${RCLONE_MOUNTPOINT} \
--log-file ${RCLONE_LOG_FILE} \
--log-level ${RCLONE_LOG_LEVEL} \
--cache-dir=${RCLONE_CACHE_DIR} \
--vfs-cache-max-size ${RCLONE_CACHE_MAX_SIZE} \
--vfs-cache-max-age ${RCLONE_CACHE_MAX_AGE} \
--dir-cache-time ${RCLONE_DIR_CACHE_TIME} \
--vfs-cache-mode ${RCLONE_CACHE_MODE} \
--umask ${RCLONE_UMASK} \
--allow-other \
--allow-non-empty \
--poll-interval 10s \
--user-agent apigateway111 \
--drive-pacer-min-sleep 10ms \
--drive-pacer-burst 200 \
--vfs-cache-poll-interval 5m \
--vfs-read-ahead 2G

ExecStop=/bin/fusermount -uz ${RCLONE_MOUNTPOINT}
Restart=on-failure
User=root
Group=sysadmin

[Install]
WantedBy=multi-user.target
EOF
sudo sed -i "s|MYSHARENAME|$SHARE_NAME|g" "$SYSTEMD_PATH/rclone_$SHARE_NAME.service"
sudo systemctl enable "rclone_$SHARE_NAME.service"
done

# ------------------------------ Mergerfs

sudo bash -c "cat > /etc/systemd/system/mergerfs.service" << \EOF
[Unit]
Description=/mnt/unionfs mergerfs mount
After=rclone_vmx-nas.service rclone_kom.service rclone_dbc_media.service 

[Service]
Type=forking
ExecStart=/usr/bin/mergerfs /mnt/rclone_mount/rclone_kom/media=NC:/mnt/rclone_mount/rclone_vmx-nas=RW:/mnt/rclone_mount/rclone_kur/move=NC:/mnt/rclone_mount/rclone_dbc_media=NC /mnt/unionfs -o rw,use_ino,allow_other,func.getattr=newest,category.action=all,category.create=ff,cache.files=partial,dropcacheonclose=true
KillMode=process
Restart=on-failure
User=root
Group=sysadmin

[Install]
WantedBy=multi-user.target
EOF

svcena mergerfs

svcdae
svcsta {rclone_dbc_media,rclone_kom,rclone_vmx-nas,rclone_onedrive,rclone_kur}
svcsta mergerfs