From ccf1d55fcf454fffb62226012bd244e8ac7cfb39 Mon Sep 17 00:00:00 2001
From: Jens Steidl <Jens.Steidl@slub-dresden.de>
Date: Thu, 8 Apr 2021 16:36:32 +0200
Subject: [PATCH] - prevent unfinished SIP directories by checking catalog
 first

---
 bin/slubsipbuilderbagit.pl | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)

diff --git a/bin/slubsipbuilderbagit.pl b/bin/slubsipbuilderbagit.pl
index 109418f..68e2bf7 100644
--- a/bin/slubsipbuilderbagit.pl
+++ b/bin/slubsipbuilderbagit.pl
@@ -585,6 +585,11 @@ PATCH
         my @addBagInfo = @{ $refAddBagInfo };
         my ($mods, $marc21);
 
+        # get descriptive metadata from catalog
+        if ($ppn) {
+            $marc21 = get_marc21_from_catalogue($ppn);
+            $mods = get_mods_from_marc21($marc21);
+        }
         # create bag dir
         SLUB::LZA::SIPBuilderBagIt::createDir($bagPath);
         # create data dir
@@ -592,11 +597,6 @@ PATCH
         # copy payload files to data
         print_scalar_data("", "INFO: copying original data...", "", "white");
         dircopy($ieDirectory, $dataPath) || die ("Error coping $ieDirectory to $dataPath");
-        # get descriptive metadata from catalog
-        if ($ppn) {
-            $marc21 = get_marc21_from_catalogue($ppn);
-            $mods = get_mods_from_marc21($marc21);
-        }
         # create meta dir
         SLUB::LZA::SIPBuilderBagIt::createDir($metaPath);
         # copy not payload files to meta
@@ -627,16 +627,16 @@ PATCH
         my @addBagInfo = @{ $refAddBagInfo };
         my ($mods, $marc21);
 
-        # create bag dir
-        SLUB::LZA::SIPBuilderBagIt::createDir($bagPath);
-        # move payloads to data dir (which is automatically renamed from IE Directory)
-        print_scalar_data("", "INFO: moving original data...", "", "white");
-        dirmove($ieDirectory, $dataPath) || die ("Error moving $ieDirectory to $dataPath");
         # get descriptive metadata from catalog
         if ($ppn) {
             $marc21 = get_marc21_from_catalogue($ppn);
             $mods = get_mods_from_marc21($marc21);
         }
+        # create bag dir
+        SLUB::LZA::SIPBuilderBagIt::createDir($bagPath);
+        # move payloads to data dir (which is automatically renamed from IE Directory)
+        print_scalar_data("", "INFO: moving original data...", "", "white");
+        dirmove($ieDirectory, $dataPath) || die ("Error moving $ieDirectory to $dataPath");
         # create meta dir
         SLUB::LZA::SIPBuilderBagIt::createDir($metaPath);
         # copy not payload files to meta
@@ -667,16 +667,16 @@ PATCH
         my @addBagInfo = @{ $refAddBagInfo };
         my ($mods, $marc21);
 
-        # create bag dir
-        SLUB::LZA::SIPBuilderBagIt::createDir($bagPath);
-        # move payloads to data dir (which is automatically renamed from IE Directory)
-        print_scalar_data("", "INFO: moving original data...", "", "white");
-        dirmove($ieDirectory, $dataPath) || die ("Error moving $ieDirectory to $dataPath");
         # get descriptive metadata from catalog
         if ($ppn) {
             $marc21 = get_marc21_from_catalogue($ppn);
             $mods = get_mods_from_marc21($marc21);
         }
+        # create bag dir
+        SLUB::LZA::SIPBuilderBagIt::createDir($bagPath);
+        # move payloads to data dir (which is automatically renamed from IE Directory)
+        print_scalar_data("", "INFO: moving original data...", "", "white");
+        dirmove($ieDirectory, $dataPath) || die ("Error moving $ieDirectory to $dataPath");
         # create meta dir
         SLUB::LZA::SIPBuilderBagIt::createDir($metaPath);
         # copy not payload files to meta
-- 
GitLab