#
# Description: Permit to use PLACEHOLDERS in prog.dldir (see: BTS #535556)
# Author: Daniel Dehennin <daniel.dehennin@baby-gnu.org> 
# Bug-Debian: http://bugs.debian.org/535556
#
Index: vcheck/vcheck
===================================================================
--- vcheck.orig/vcheck	2011-02-11 03:45:26.473135240 +0100
+++ vcheck/vcheck	2011-02-11 03:56:29.134570959 +0100
@@ -120,6 +120,7 @@
 require HTTP::Request;
 use Fcntl ':flock';
 use File::Basename;
+use File::Path qw{mkpath};
 
 #-----8<--------------------------------------------------------------------
 
@@ -702,13 +703,6 @@
       },
       dldir => {
         type => 'string',
-        validate => sub {
-          if (m#^/#) {
-            return (-d && -w && -x) ? 0 : 'Directory not accessible/writable'
-          } else {
-            return 'Not an absolute path'
-          }
-        },
         help => "absolute path of a directory where to put d/l'ed files"
       },
       dlexec => {
@@ -819,13 +813,10 @@
       },
       dldir => {
         type => 'string',
-        validate => sub {
-          if (m#^/#) {
-            return (-d && -w && -x) ? 0 : 'Directory not accessible/writable'
-          } else {
-            return 0  # can't check presence lacking global $dldir value
-          }
-        },
+        help => "d/l directory (absolute, or relative to global \$dldir)"
+      },
+      dldirlast => {
+        type => 'string',
         help => "d/l directory (absolute, or relative to global \$dldir)"
       },
       dlexec => {
@@ -1284,6 +1275,9 @@
       }
     }
   }
+  if (exists $data{$name}{'dldir'}) {
+      $data{$name}{'dldirlast'} = &substitute_vers($data{$name}{'dldir'}, \@vers);
+  }
 }
 
 &save_data();
@@ -1293,10 +1287,13 @@
   while (@dls) {
     @_ = @{shift @dls};  # (name, ver, fname, URL, D/L URL, vers_ref [, retry])
     eprint "Trying to retrieve §B@_[0, 1]§N...\n--> URL: <$_[4]>\n";
-    my $of = (exists $data{$_[0]}{dldir} && $data{$_[0]}{dldir} =~ m#^/# ?
-      "$data{$_[0]}{dldir}/" :
-      ($config{dldir} ? "$config{dldir}/" . (exists $data{$_[0]}{dldir} ?
-      "$data{$_[0]}{dldir}/" : '') : ''));
+    my $of = (exists $data{$_[0]}{dldirlast} && $data{$_[0]}{dldirlast} =~ m#^/# ?
+      "$data{$_[0]}{dldirlast}/" :
+      ($config{dldir} ? "$config{dldir}/" . (exists $data{$_[0]}{dldirlast} ?
+      "$data{$_[0]}{dldirlast}/" : '') : ''));
+    if (! -d $of) {
+        mkpath($of, {mode => 0750});
+    }
     if (length $of && !(-d $of && -w $of && -x $of)) {
       eprint "--> §YSkipping§N, since §Rspecified `dldir'  doesn't exist§N.\n";
       next
@@ -1862,6 +1859,22 @@
 will be considered relative to either B<config.dldir>, if specified, or the
 directory the script is executing in.
 
+The dldir string is subject to expansion of the following placeholders (see
+L<"PLACEHOLDERS"> for their meaning): C<__NEWVER__>.
+
+=item B<dldirlast> (string)
+
+This option specifies the download directory of the last downloaded
+version of a program. It does not contains L<"PLACEHOLDERS"> unlike
+C<prog.dldir>. If the directory is absolute (i.e., relative to the root
+directory, as indicated by a leading slash), it will be treated as an
+absolute path, otherwise it will be considered relative to either
+B<config.dldir>, if specified, or the directory the script is
+executing in.
+
+This option is only used to store L<"PLACEHOLDER"> free dldir, it is
+overwriten at each new download.
+
 =item B<dlexec> (string, may be zero-length)
 
 Specifies a command to be executed after any successful download of the
