From 6183b6a3f1fd87f2c8564e6e4bbdf902479dd483 Mon Sep 17 00:00:00 2001
From: Jens Steidl <Jens.Steidl@slub-dresden.de>
Date: Tue, 30 Jul 2019 17:36:30 +0200
Subject: [PATCH] - make script OS-independent (incomplete, just part one)

---
 bin/slubsipbuilder.pl | 46 ++++++++++++++++++++-----------------------
 1 file changed, 21 insertions(+), 25 deletions(-)

diff --git a/bin/slubsipbuilder.pl b/bin/slubsipbuilder.pl
index 447d7db..be608c3 100755
--- a/bin/slubsipbuilder.pl
+++ b/bin/slubsipbuilder.pl
@@ -24,13 +24,11 @@ use 5.20.0;
 use strict;
 use warnings;
 use Archive::Zip::SimpleZip qw($SimpleZipError);
-use Cwd qw(abs_path);
 use DateTime::Format::ISO8601;
 use Digest::MD5 qw(md5);
 use File::Basename;
 use File::Copy qw(cp);
 use File::Find;
-use File::Path;
 use Path::Tiny;
 use Getopt::Long;
 use LWP::UserAgent; # to get MARC data
@@ -82,7 +80,9 @@ if ((!defined $ppn) && (!defined $noppn))   { confess("you need to specify a PPN
 if (!defined $output)                       { confess("you need to specify an output path, where the SIP will be stored"); }
 if (!defined $external_conservation_flag)   { $external_conservation_flag="false"; } else { $external_conservation_flag="true"; }
 if (! -d $directory)                        { confess("you need to specify an IE directory, which needs to be archived, $!"); }
-$directory = abs_path($directory);
+$directory = path($directory)->realpath->stringify;
+path($output)->mkpath;
+$output = path($output)->realpath->stringify;
 if ($external_id !~ m#^[a-z0-9]+$#)         { confess("you need to specify a valid external ID (^[a-z0-9]+\$)"); }
 if ($external_workflow !~ m#^[a-z0-9]+$#)   { confess("you need to specify a valid external workflow (^[a-z0-9]+\$)"); }
 if (!$external_value_descr)                 { confess("you need to specify an external value description (reason for archiving)"); }
@@ -176,19 +176,18 @@ sub get_mods_from ($$$$) { # $mods = ($url, $ppn, $searchkey, $recordschema)
     my $schema = shift;
 
     # check xsl directory
-    my $xsl_dir = dirname(__FILE__) . '/../xsl/';
+    my $xsl_dir = path( dirname(__FILE__) )->realpath->parent->child("xsl")->stringify;
     if (! -d $xsl_dir) {
         say "Rebuilding XSL directory '$xsl_dir'";
         mkpath($xsl_dir) || confess ("could not mkdir '$xsl_dir', $!");;
     }
-    $xsl_dir = abs_path($xsl_dir);
 
     #### where to find XSLT
     my $marc_mods_url           = 'http://www.loc.gov/standards/mods/v3/MARC21slim2MODS3-6.xsl';
-    my $marc_mods_path          = "$xsl_dir/" . basename($marc_mods_url);
-    my $marc_mods_patched_path  = "$xsl_dir/" . basename($marc_mods_url, ".xsl") . ".patched.xsl";
+    my $marc_mods_path          = path($xsl_dir)->child( basename($marc_mods_url) )->stringify;
+    my $marc_mods_patched_path  = path($xsl_dir)->child( basename($marc_mods_url,".xsl").".patched.xsl" )->stringify;
     my $marc_utils_url          = 'http://www.loc.gov/standards/marcxml/xslt/MARC21slimUtils.xsl';
-    my $marc_utils_path         = "$xsl_dir/" . basename($marc_utils_url);
+    my $marc_utils_path         = path($xsl_dir)->child( basename($marc_utils_url) )->stringify;
 
     my $ua = LWP::UserAgent->new;
     $ua->agent("MyApp/0.1 ");
@@ -276,15 +275,14 @@ sub main {
     $file_date =~ s/T/_/g; # replace 'T' with '_'
     $file_date =~ s/:/-/g; # replace ':' with '-'
     # create output dir
-    if (! -d $output) {
-        mkpath("$output") || confess("could not create SIP directory for '$output', $!");
-    }
-    $output = abs_path($output);
+    #~ if (! -d $output) {
+        #~ mkpath("$output") || confess("could not create SIP directory for '$output', $!");
+    #~ }
+    #~ $output = abs_path($output);
     my $sip_root_dir = (defined $ppn)? "PPN-${ppn}_${file_date}" : "ID-${noppn}_${file_date}";
-    my $content = "$output/$sip_root_dir/data";
+    my $content = path($output)->child($sip_root_dir)->child("data")->stringify;
     if (!defined $as_zip) {
-        mkpath("$output/$sip_root_dir") || confess("could not create SIP directory for '$output/$sip_root_dir', $!");
-        mkpath("$content") || confess("could not create SIP subdirectory for '$content', $!");
+        path($content)->mkpath;
     }
 
 
@@ -425,28 +423,26 @@ METS
 
     # compress if needed
     if (!defined $as_zip) {
-        write_file( "${output}/${sip_root_dir}/sip.xml", $sip );
+        write_file( path($output)->child($sip_root_dir)->child("sip.xml")->stringify, $sip );
         # copy source to target
         foreach my $source (sort keys (%filecopyhash)) {
-            my $target = $filecopyhash{$source}->{"target"};
-            my $basename = dirname($target);
-            #say "cp $source, $target ($basename)";
+            my $target = path($filecopyhash{$source}->{"target"})->stringify; # CHECK ON WINDOWS
+            my $basename = path($target)->parent->stringify;
             if (! -d $basename) {
-                mkpath($basename) || confess ("could not mkdir '$basename', $!");
+                path($basename)->mkpath;
             }
             cp($source, $target, buffer) || confess ("could not copy from '$source' to '$target', $!");
         }
         say "SIP '$sip_root_dir' build successfully in '$output'";
     } else {
         # compress it
-        my $zip_file_path = "$output/$sip_root_dir.zip";
+        my $zip_file_path = path($output)->child("$sip_root_dir.zip")->stringify;
         my $zip = Archive::Zip::SimpleZip->new( $zip_file_path, Zip64=>1 );
-        $zip->addString($sip, Name=> "$sip_root_dir/sip.xml" );
+        $zip->addString($sip, Name=>path($sip_root_dir)->child("sip.xml")->stringify);
         # copy source to target
         foreach my $source (sort keys (%filecopyhash)) {
-            my $target = "$sip_root_dir/".$filecopyhash{$source}->{"relative"};
-            my $basename = dirname($target);
-            #say "cp $source, $target ($basename)";
+            my $target = path($sip_root_dir)->child($filecopyhash{$source}->{"relative"})->stringify; # CHECK ON WINDOWS
+            my $basename = path($target)->parent->stringify;
             $zip->add( $source, Name=> $target) || confess ("could not zip copy from '$source' to '$target', $!");
         }
         unless ( $zip->close()) {
-- 
GitLab