#!/bin/bash
#===================================================================
# Filename   : rclone_alist_automount.sh
# Function   :
# Usage      :
# Author     : Manford Fan
# Date       : 2022-10-19 14:05
# Version    : Version 0.1
# Disclaimer : The author is NOT responsible for any loss caused
#              by the user's own operations.
# And More   : If you find there are some bugs in this script
#              Or you have better ideas, please do contact me
#              via E-mail -- mffan0922@163.com
#===================================================================


function rclone_alist_reset() {
    systemctl restart alist.service
    for i in `seq 3`; do
        fusermount -uzq /opt/webdav/alist > /dev/null 2>&1
        umount  /opt/webdav/alist > /dev/null 2>&1
        sleep 2
    done
    ps -ef | grep 'rclone mount Alist' | grep -v grep | awk '{print $2}' > /tmp/rclone/rclone_alist_pids
    for rclone_alist_pid in `cat /tmp/rclone/rclone_alist_pids`; do
        kill -9 $rclone_alist_pid;
    done
    nohup /usr/bin/rclone mount Alist:/ /opt/webdav/alist \
        --allow-other --vfs-cache-mode full --vfs-cache-max-size 10G --vfs-read-ahead 100M --dir-cache-time 2m --poll-interval 0 \
        --vfs-cache-max-age 4h --cache-dir /tmp/vfs-cache --bwlimit-file 20M --bwlimit 100M --no-update-modtime --contimeout 30m \
        --log-file /opt/logs/rclone/rclone_alist.log --log-level NOTICE --vfs-read-chunk-size 100M --vfs-read-chunk-size-limit 500M \
        --buffer-size 200M --attr-timeout 5m --transfers=6 --multi-thread-streams=6 --fast-list --timeout 30m \
        --allow-non-empty --no-modtime --max-duration 3h --vfs-read-wait 2s --vfs-write-wait 5s > /dev/null 2>&1 &
}

alist_log='/opt/logs/rclone/rclone_alist.log'
pid_self=$$
# get all kinds of states for later decision
num=`cat /proc/mounts | grep /opt/webdav/alist | wc -l`
[[ $num -eq 0 ]] && loaded=0
[[ $num -eq 1 ]] && loaded=1
[[ $num -gt 1 ]] && loaded=2

ps -ef | grep '/usr/bin/rclone mount Alist' | grep -v grep | awk '{print $2}' > /tmp/rclone/rclone_alist_pids
num=`cat /tmp/rclone/rclone_alist_pids | wc -l`
[[ $num -eq 0 ]] && rclone_running=0
[[ $num -eq 1 ]] && rclone_running=1
[[ $num -gt 1 ]] && rclone_running=2

sleep 2
ps -ef | grep '/usr/bin/bash /opt/scripts/rclone/rclone_alist_automount.sh' | grep -v 'grep' > /tmp/rclone/rclone_alist_automount_pids
let num=`cat /tmp/rclone/rclone_alist_automount_pids | sed -e '/^$/d' | wc -l`
[[ $num -eq 1 ]] && script_running=1
[[ $num -gt 1 ]] && script_running=2

# print the states for debug
echo `date` >> $alist_log
echo loaded = $loaded >> $alist_log
echo rclone_running = $rclone_running >> $alist_log
echo script_running = $script_running >> $alist_log
# exit 5

# decide if `rclone` command function normally
if [[ $1 == '-f' ]]; then
    echo -e "Happening @ $(date) [Alist] Executing BY Hands.\n" >> $alist_log

    if [[ $script_running -eq 1 ]]; then
        rclone_alist_reset
    elif [[ script_running -eq 2 ]]; then
        echo "Happening @ $(date) [Alist] Script rclone_alist_automount.sh -f has already been executing..." | tee -a $alist_log
        echo "Happening @ $(date) [Alist] Alist RESET will be done with -f option" | tee -a $alist_log
        for rclone_alist_automount_pid in `cat /tmp/rclone/rclone_alist_automount_pids`; do
            [[ $rclone_alist_automount_pid != $pid_self ]] && kill -9 $rclone_alist_automount_pid > /dev/null 2>&1
        done
        rclone_alist_reset
    else
        echo "Happening @ $(date) [Alist] In general, this -f case will NOT happen" >> $alist_log
    fi
elif [[ $1 == '-c' ]]; then
    echo -e "Happening @ $(date) [Alist] Executing BY Cron Service.\n" >> $alist_log

    if [[ $script_running -eq 1 ]]; then
        rclone_alist_reset
    elif [[ script_running -eq 2 ]]; then
        echo "Happening @ $(date) [Alist] Script rclone_alist_automount.sh -c has already been executing..." | tee -a $alist_log
        echo "Happening @ $(date) [Alist] Alist RESET will be done on CRON condition." | tee -a $alist_log
        for rclone_alist_automount_pid in `cat /tmp/rclone/rclone_alist_automount_pids`; do
            [[ $rclone_alist_automount_pid != $pid_self ]] && kill -9 $rclone_alist_automount_pid > /dev/null 2>&1
        done
        rclone_alist_reset
    else
        echo "Happening @ $(date) [Alist] In general, this -c case will NOT happen" >> $alist_log
    fi
elif [[ $1 == '' ]]; then
    sleep 10
    if [[ script_running -eq 1 ]]; then
        if [[ $loaded -ne 1 || $rclone_running -ne 1 ]]; then
            echo "Happening @ $(date) [Alist] Executing automatically." >> $alist_log
            rclone_alist_reset
        fi
    elif [[ $script_running -eq 2 ]]; then
        echo "Happening @ $(date) [Alist] Script rclone_alist_automount.sh auto has already been executing..." | tee -a $alist_log
        echo "Happening @ $(date) [Alist] Nothing will be done at this auto-situation" | tee -a $alist_log
        # for rclone_alist_automount_pid in `cat /tmp/rclone_alist_automount_pids`; do
        #     [[ $rclone_alist_automount_pid != $pid_self ]] && kill -9 $rclone_alist_automount_pid > /dev/null 2>&1
        # done
        # rclone_alist_reset
    else
        echo "Happening @ $(date) [Alist] In general, this auto case will NOT happen" >> $alist_log
    fi
else
    echo "Happening @ $(date) [Alist] Wrong usage of script/tool, only accept -f | -c or nothing as argument." >> $alist_log
fi



