#!/usr/bin/env perl

# Script:      install.pl
# Lang:        perl
# Description: This script installs my personal scripts from the git repo
#              found at $repo_url

# NOTE: THE UNLICENSE BELOW APPLIES TO THIS SCRIPT ONLY, NOT THE SOFTWARE WHICH
# IS INSTALLED BY EXECUTING IT.

# This is free and unencumbered software released into the public domain.

# Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
# software, either in source code form or as a compiled binary, for any purpose,
# commercial or non-commercial, and by any means.

# In jurisdictions that recognize copyright laws, the author or authors of this
# software dedicate any and all copyright interest in the software to the public
# domain. We make this dedication for the benefit of the public at large and to
# the detriment of our heirs and successors. We intend this dedication to be an
# overt act of relinquishment in perpetuity of all present and future rights to
# this software under copyright law.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#For more information, please refer to <http://unlicense.org/>


use strict;
use warnings;
use File::Copy;
use Digest::MD5;

my $repo_url = "https://git.fjla.uk/fred.boniface/scripts.git";
my $repo_dir = "/opt/fred_boniface_scripts_git";
my $branch_name = "main";
my $install_dir = "/usr/local/bin";

if (-d $repo_dir) {
    print "Updating existing local repo...\n";
    chdir $repo_dir or die "Unable to enter $repo_dir: $!";
    system("git pull origin $branch_name")
} else {
    print "Cloning repo...\n";
    system("git clone --branch $branch_name $repo_url $repo_dir");
    chdir $repo_dir or die "Unable to enter $repo_dir: $1";
}

my $scripts = "$repo_dir/scripts";

chdir $scripts or die "Unable to change into $scripts: $!";

print "Installing scripts\n";

opendir(my $repo_scripts, $scripts) or die "Unable to open directory: $!";
my @script_files = grep { -f "$scripts/$_" } readdir($repo_scripts);
closedir($repo_scripts);

foreach my $file (@script_files) {
    my $repo_file_path = "$scripts/$file";
    my $install_file_path = "$install_dir/$file";

    print "Repo file: $repo_file_path\n";
    print "Dest: $install_file_path\n";

    if (!-e $install_file_path || !compare_checksums($repo_file_path, $install_file_path)) {
        print "Copying $file...\n";
        copy($repo_file_path, $install_file_path) or die "Copy failed: $!";
        chmod(0755, $install_file_path) or die "Failed to set execute permission: $!";
    } else {
        print "Skipping $file (Checksum matched)\n";
    }
}

print "Installation of scripts complete\n";
print "Ensure that $install_dir is in your PATH\n";

sub compare_checksums {
    my ($file1, $file2) = @_;
    
    open my $fh1, '<', $file1 or die "Could not open file $file1: $!";
    open my $fh2, '<', $file2 or die "Could not open file $file2: $!";
    
    my $digest1 = Digest::MD5->new->addfile($fh1)->hexdigest;
    my $digest2 = Digest::MD5->new->addfile($fh2)->hexdigest;
    
    close $fh1;
    close $fh2;
    
    return $digest1 eq $digest2;
}