#!/bin/bash
#===================================================================
# Filename   : rclone_cloudreve_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_cloudreve_reset() {
    for i in `seq 3`; do
        fusermount -uzq /opt/webdav/cloudreve > /dev/null 2>&1
        umount  /opt/webdav/cloudreve > /dev/null 2>&1
        sleep 2
    done
    ps -ef | grep 'rclone mount Cloudreve' | grep -v grep | awk '{print $2}' > /tmp/rclone/rclone_cloudreve_pids
    for rclone_cloudreve_pid in `cat /tmp/rclone/rclone_cloudreve_pids`; do
        kill -9 $rclone_cloudreve_pid;
    done
    nohup /usr/bin/rclone mount Cloudreve:/ /opt/webdav/cloudreve \
        --allow-other --vfs-cache-mode full --vfs-cache-max-size 10G --vfs-read-ahead 100M --dir-cache-time 2m\
        --vfs-cache-max-age 4h --cache-dir /tmp/vfs-cache --bwlimit-file 20M --bwlimit 100M --no-update-modtime \
        --log-file /opt/logs/rclone/rclone_cloudreve.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 \
        --allow-non-empty --no-modtime --max-duration 3h --vfs-read-wait 2s --vfs-write-wait 5s > /dev/null 2>&1 &
}

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

ps -ef | grep '/usr/bin/rclone mount Cloudreve' | grep -v grep | awk '{print $2}' > /tmp/rclone/rclone_cloudreve_pids
num=`cat /tmp/rclone/rclone_cloudreve_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_cloudreve_automount.sh' | grep -v 'grep' > /tmp/rclone/rclone_cloudreve_automount_pids
let num=`cat /tmp/rclone/rclone_cloudreve_automount_pids | sed -e '/^$/d' | wc -l`
[[ $num -eq 1 ]] && script_running=1
if [[ $num -gt 1 ]]; then
    script_running=2
    echo `date` >> /tmp/rclone/rclone_cloudreve_abnormal.log
    ps -ef | grep '/usr/bin/bash /opt/scripts/rclone/rclone_cloudreve_automount.sh' | grep -v 'grep' >> /tmp/rclone/rclone_cloudreve_abnormal.log
fi

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

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

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

    if [[ $script_running -eq 1 ]]; then
        rclone_cloudreve_reset
    elif [[ script_running -eq 2 ]]; then
        echo "Happening @ $(date) [Cloudreve] Script rclone_cloudreve_automount.sh -c has already been executing..." | tee -a $cloudreve_log
        echo "Happening @ $(date) [Cloudreve] Cloudreve RESET will be done on CRON condition." | tee -a $cloudreve_log
        for rclone_cloudreve_automount_pid in `cat /tmp/rclone/rclone_cloudreve_automount_pids`; do
            [[ $rclone_cloudreve_automount_pid != $pid_self ]] && kill -9 $rclone_cloudreve_automount_pid > /dev/null 2>&1
        done
        rclone_cloudreve_reset
    else
        echo "Happening @ $(date) [Cloudreve] In general, this case will NOT happen" >> $cloudreve_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) [Cloudreve] Executing automatically." >> $cloudreve_log
            rclone_cloudreve_reset
        fi
    elif [[ $script_running -eq 2 ]]; then
        echo "Happening @ $(date) [Cloudreve] Script rclone_cloudreve_automount.sh auto has already been executing..." | tee -a $cloudreve_log
        echo "Happening @ $(date) [Cloudreve] Nothing will be done at this auto-situation" | tee -a $cloudreve_log
        # for rclone_cloudreve_automount_pid in `cat /tmp/rclone_cloudreve_automount_pids`; do
        #     [[ $rclone_cloudreve_automount_pid != $pid_self ]] && kill -9 $rclone_cloudreve_automount_pid > /dev/null 2>&1
        # done
        # rclone_cloudreve_reset
    else
        echo "Happening @ $(date) [Cloudreve] In general, this case will NOT happen" >> $cloudreve_log
    fi
else
    echo "Happening @ $(date) [Cloudreve] Wrong usage of script/tool, only accept -f | -c or nothing as argument." >> $cloudreve_log
fi



