#!/bin/ash
# Paths to monitor and synchronize
WATCH_DIR="/etc/rc.d"
TARGET_DIR1="/real_rootfs/etc/rc.d"
TARGET_DIR2="/usrdata/etc/rc.d"

# Function to check if a path is a mountpoint
is_mountpoint() {
    grep -q " $1 " /proc/mounts
    return $?
}

# Function to synchronize init scripts
synchronize_init_scripts() {
    # Ensure /real_rootfs is writable for updates
    mount -o remount,rw /real_rootfs
    
    # Synchronize with TARGET_DIR1
    echo "Synchronizing $WATCH_DIR with $TARGET_DIR1..."
    for link in "$WATCH_DIR"/*; do
        if [ -L "$link" ]; then
            link_name=$(basename "$link")
            if [ ! -e "$TARGET_DIR1/$link_name" ] || [ "$link" -nt "$TARGET_DIR1/$link_name" ]; then
                cp -af "$link" "$TARGET_DIR1/$link_name"
            fi
        fi
    done
    
    for link in "$TARGET_DIR1"/*; do
        if [ -L "$link" ]; then
            link_name=$(basename "$link")
            if [ ! -e "$WATCH_DIR/$link_name" ]; then
                rm -f "$TARGET_DIR1/$link_name"
            fi
        fi
    done

    # Synchronize with TARGET_DIR2 only if /usrdata is a mountpoint
    if is_mountpoint "/usrdata"; then
        echo "Synchronizing $WATCH_DIR with $TARGET_DIR2..."
        for link in "$WATCH_DIR"/*; do
            if [ -L "$link" ]; then
                link_name=$(basename "$link")
                if [ ! -e "$TARGET_DIR2/$link_name" ] || [ "$link" -nt "$TARGET_DIR2/$link_name" ]; then
                    cp -af "$link" "$TARGET_DIR2/$link_name"
                fi
            fi
        done
        
        for link in "$TARGET_DIR2"/*; do
            if [ -L "$link" ]; then
                link_name=$(basename "$link")
                if [ ! -e "$WATCH_DIR/$link_name" ]; then
                    rm -f "$TARGET_DIR2/$link_name"
                fi
            fi
        done
    fi

    # Restore /real_rootfs to read-only
    mount -o remount,ro /real_rootfs
}

# Run synchronization
synchronize_init_scripts
exit 0
