76 lines
1.9 KiB
Perl
Executable File
76 lines
1.9 KiB
Perl
Executable File
#!/bin/perl
|
|
use strict;
|
|
use warnings;
|
|
use feature("signatures");
|
|
|
|
use File::Path("make_path");
|
|
use List::Util("max");
|
|
|
|
sub fs_root ($file_name) {
|
|
chomp( my $ret = `stat -c %m "$file_name"` );
|
|
return $ret;
|
|
}
|
|
|
|
sub file_info ($file) {
|
|
my $file_name = `basename "$file"`;
|
|
my $file_path = `realpath -s "$file"`;
|
|
chomp( $file_name, $file_path );
|
|
my $file_fs_root = fs_root($file_name);
|
|
|
|
return ( $file_name, $file_path, $file_fs_root );
|
|
}
|
|
|
|
sub trash_dirs ($file_fs_root) {
|
|
my $data_path = "$ENV{'XDG_DATA_HOME'}" // "$ENV{'HOME'}/.local/share";
|
|
|
|
my $trash_dir;
|
|
if ( $file_fs_root eq fs_root($data_path) ) {
|
|
$trash_dir = "$data_path/Trash/";
|
|
}
|
|
else {
|
|
$trash_dir = "$file_fs_root/.Trash/";
|
|
}
|
|
|
|
return "${trash_dir}info", "${trash_dir}files";
|
|
}
|
|
|
|
sub target_file_name ( $file_name, $trash_file_path ) {
|
|
opendir( my $dir, "$trash_file_path" ) or die "$!";
|
|
my $existing_suffix_nums =
|
|
map( ( $_ =~ /^${file_name}\.~(\d)~/ ), readdir $dir );
|
|
my $suffix_num = max($existing_suffix_nums) + 1;
|
|
my $suffix = "~" . $suffix_num . "~";
|
|
closedir($dir);
|
|
|
|
return "${file_name}.${suffix}";
|
|
}
|
|
|
|
sub write_file ( $file_path, $contents ) {
|
|
open( FH, '>', $file_path ) or die "$!";
|
|
print FH $contents;
|
|
}
|
|
|
|
# NOTE: Execution starts here
|
|
|
|
if ( $#ARGV + 1 != 1 ) {
|
|
die "Bad Arguments";
|
|
}
|
|
|
|
stat $ARGV[0] or die "$!";
|
|
my ( $file_name, $file_src, $file_fs_root ) = file_info( $ARGV[0] );
|
|
|
|
my ( $trash_info_path, $trash_file_path ) = trash_dirs($file_fs_root);
|
|
make_path( $trash_info_path, $trash_file_path ) or die "$!";
|
|
|
|
my $target_name = target_file_name( $file_name, $trash_file_path );
|
|
chomp( my $deletion_time = `date -u +%Y%m%dUTC%T` );
|
|
|
|
my $info_contents = <<EOF;
|
|
[Trash Info]
|
|
Path=$file_src
|
|
DeletionDate=$deletion_time
|
|
EOF
|
|
|
|
rename( $file_src, "$trash_file_path/$target_name" ) or die "$!";
|
|
write_file( "$trash_info_path/$target_name", $info_contents ) or die "$!";
|