#!/bin/bash

######################################################################
# Auxiliary functions

# Log a string via the syslog facility.
log()
{
    if [ $1 != debug ] || expr "$VERBOSE" : "[yY]" > /dev/null; then
        logger -p user.$1 -t "dualcontroller.sync_conf[$$]" -- "$2"
    fi
}

######################################################################

# Main worker

# Count
loop=0
starting_up=1
JOB_QUEUE="$RUN_DIR/dualcontroller_queue"
exec 6<$JOB_QUEUE

#######################################################################
# Tool functions


start_job() {
    # Jobs to do on start.
    :
}

finish_job() {
    # Jobs to do on exit.
    systemctl daemon-reload
}
#######################################################################


is_monitor_file() {
    f=$1
    for d in "${monitor_list[@]}"
    do
        if [[ " $f" =~ " $d" ]];then
            return 0
        fi
    done
    return 1
}
monitor_list=($@)

start_job

while read -u6 info; do
    action=`echo "$info" | jq -cr .action`
    path=`echo "$info" | jq -cr .path`
    file=`echo "$info" | jq -cr .file`
    full_path="$path$file"
    monitor_file_directly=0
    if [ -z "$file" ];then
        monitor_file_directly=1
        path=`dirname "$full_path"`
        file=`basename "$full_path"`
    fi
    case "$action" in
        "CREATE"|"MOVED_TO"|"MODIFY")
            if is_monitor_file "$full_path";then
                FILE_TYPE=$(file -b --mime-type "$full_path")
                target_dir="$BAK_DIR$path"
                if [[ "$FILE_TYPE" == inode/* ]] && [[ "$FILE_TYPE" == inode/x-empty ]]; then
                    truncate -s 0 "$BAK_DIR$full_path"
                    continue
                fi
                [ -d "$target_dir" ] || mkdir -p "$target_dir"

                if [ $NAME == "iscsi" ];then
                    /unas/sbin/dualcontroller/conf_sync.d/iscsi "$full_path"
                    if [ "$RUNMODE" == "aa" ];then
                        SKIP_SELF=1 /unas/sbin/dualcontroller/tool/nodes /unas/sbin/dualcontroller/conf_sync.d/iscsi "$BAK_DIR$full_path"
                    fi
                else
                    $RSYNC "$full_path" "$target_dir"
                    if [ "$RUNMODE" == "aa" ];then
                        /unas/sbin/dualcontroller/tool/distribute "$full_path"
                    fi
                fi
            fi
            if [ "$RUNMODE" == "aa" ];then
                SKIP_SELF=1 /unas/sbin/dualcontroller/tool/nodes systemctl reload $SERVICE
            fi
            ;;
        "CREATE,ISDIR"|"MOVED_TO,ISDIR")
            if is_monitor_file "$full_path";then
                target_dir="$BAK_DIR$path"
                [ -d "$target_dir" ] || mkdir -p "$target_dir"
                $RSYNC "$full_path" "$target_dir"
                if [ "$RUNMODE" == "aa" ];then
                    /unas/sbin/dualcontroller/tool/distribute "$full_path"
                fi
            fi
            if [ "$RUNMODE" == "aa" ];then
                SKIP_SELF=1 /unas/sbin/dualcontroller/tool/nodes systemctl reload $SERVICE
            fi
            ;;
        "ATTRIB"|"ATTRIB,ISDIR")
            # 配置文件不是很需要同步权限？
            continue
            if is_monitor_file "$full_path";then
                target_dir="$BAK_DIR$path"
                [ -d "$target_dir" ] || mkdir -p "$target_dir"
                chmod --reference="$full_path" "$BAK_DIR$full_path"
                chown --reference="$full_path" "$BAK_DIR$full_path"
                touch -r "$full_path" "$BAK_DIR$full_path"
                if [ "$RUNMODE" == "aa" ];then
                    /unas/sbin/dualcontroller/tool/distribute "$full_path"
                fi
            fi
            if [ "$RUNMODE" == "aa" ];then
                SKIP_SELF=1 /unas/sbin/dualcontroller/tool/nodes systemctl reload $SERVICE
            fi
            ;;
        "DELETE"|"MOVED_FROM"|"DELETE_SELF")
            rm -f $BAK_DIR$full_path
            # 直接监控的文件，只覆盖不删除，只删除监控文件夹的子文件
            if [ "$RUNMODE" == "aa" ];then
                if [ "$monitor_file_directly"n == 0n ] && is_monitor_file "$full_path";then
                    SKIP_SELF=1 /unas/sbin/dualcontroller/tool/nodes rm -f "$full_path"
                fi
                SKIP_SELF=1 /unas/sbin/dualcontroller/tool/nodes systemctl reload $SERVICE
            fi
            ;;
        "DELETE,ISDIR"|"MOVED_FROM,ISDIR")
            rm -f $BAK_DIR$full_path
            if [ "$RUNMODE" == "aa" ];then
                if is_monitor_file "$full_path";then
                    SKIP_SELF=1 /unas/sbin/dualcontroller/tool/nodes rm -rf "$full_path"
                fi
                SKIP_SELF=1 /unas/sbin/dualcontroller/tool/nodes systemctl reload $SERVICE
            fi
            ;;
        *)
            echo Unknown action $action on $full_path
            ;;
    esac
done

finish_job

exit 0
