#!/bin/bash
#===================================================================
# Filename   : rclone_onedrive_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_onedrive_reset() {
    for i in `seq 3`; do
        fusermount -uzq /opt/webdav/onedrive > /dev/null 2>&1
        umount  /opt/webdav/onedrive > /dev/null 2>&1
        sleep 2
    done
    ps -ef | grep 'rclone mount Onedrive' | grep -v grep | awk '{print $2}' > /tmp/rclone/rclone_onedrive_pids
    for rclone_onedrive_pid in `cat /tmp/rclone/rclone_onedrive_pids`; do
        kill -9 $rclone_onedrive_pid;
    done
    nohup /usr/bin/rclone mount Onedrive:/ /opt/webdav/onedrive \
        --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_onedrive.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 &
}

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

ps -ef | grep '/usr/bin/rclone mount Onedrive' | grep -v grep | awk '{print $2}' > /tmp/rclone/rclone_onedrive_pids
num=`cat /tmp/rclone/rclone_onedrive_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_onedrive_automount.sh' | grep -v 'grep' > /tmp/rclone/rclone_onedrive_automount_pids
let num=`cat /tmp/rclone/rclone_onedrive_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` >> $onedrive_log
echo loaded = $loaded >> $onedrive_log
echo rclone_running = $rclone_running >> $onedrive_log
echo script_running = $script_running >> $onedrive_log
# exit 5

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

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

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



