#!/bin/bash
#===================================================================
# Filename   : koel_update.sh
# Function   :
# Usage      :
# Author     : Manford Fan
# Date       : 2022-10-15 23:34
# 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
#===================================================================

[[ ! -e /tmp/files_now ]] && touch /tmp/files_now
[[ ! -e /tmp/files_pre_60s ]] && touch /tmp/files_pre_60s
success_flg=1
ls -aR /opt/media/Music | grep -E "*.(mp3|flac|opus|aac|ogg|m4a)" | sort > /tmp/files_now
diff /tmp/files_now /tmp/files_pre_60s >> /opt/logs/koel_update.log
if [[ $? -ne 0 ]]; then
    chown -R www-data:www-data /opt/media/Music
    for i in `seq 10`; do
        php /opt/source-code/koel/artisan koel:sync > /dev/null
        if [[ $? -eq 0 ]]; then
            php /opt/source-code/koel/artisan koel:sync >> /opt/logs/koel_update.log
            success_flg=0
            break
        fi
        sleep 2
    done
    [[ success_flg -eq 1 ]] && echo "Happening @ $(date) Failed scanning the media dir, need processing that by hand." >> /opt/logs/koel_update.log
    echo -e "Happening @ $(date) Sync koel music successfully." >> /opt/logs/koel_update.log
fi
cp /tmp/files_now /tmp/files_pre_60s

