PYTHON_LIBS=
   if test "$withval" != "no"; then
      if test "$withval" = "yes"; then
-       if test -f /usr/include/python2.2/Python.h; then
-          PYTHON_INCDIR=-I/usr/include/python2.2
-          PYTHON_LIBS="-L/usr/lib/python2.2/config -lpython2.2 -lutil -ldl"
-       elif test -f /usr/include/python2.3/Python.h; then
-          PYTHON_INCDIR=-I/usr/include/python2.3
-          PYTHON_LIBS="-L/usr/lib/python2.3/config -lpython2.3 -lutil -ldl"
-       elif test -f /usr/include/python2.4/Python.h; then
-          PYTHON_INCDIR=-I/usr/include/python2.4
-          PYTHON_LIBS="-L/usr/lib/python2.4/config -lpython2.4 -lutil -ldl"
-       elif test -f $prefix/include/Python.h; then
-          PYTHON_INCDIR=-I$prefix/include
-          PYTHON_LIBS="-L$prefix/lib/config -lpython -lutil -ldl"
-       else
+       for python_root in /usr /usr/local; do
+         if test -f $python_root/include/python2.2/Python.h; then
+            PYTHON_INCDIR=-I$python_root/include/python2.2
+            PYTHON_LIBS="-L$python_root/lib/python2.2/config -lpython2.2 -lutil"
+            break
+         elif test -f $python_root/include/python2.3/Python.h; then
+            PYTHON_INCDIR=-I$python_root/include/python2.3
+            PYTHON_LIBS="-L$python_root/lib/python2.3/config -lpython2.3 -lutil"
+            break
+         elif test -f $python_root/include/python2.4/Python.h; then
+            PYTHON_INCDIR=-I$python_root/include/python2.4
+            PYTHON_LIBS="-L$python_root/lib/python2.4/config -lpython2.4 -lutil"
+            break
+         fi
+       done
+       if test x$PYTHON_INCDIR = x; then
+         if test -f $prefix/include/Python.h; then
+            PYTHON_INCDIR=-I$prefix/include
+            PYTHON_LIBS="-L$prefix/lib/config -lpython -lutil"
+         else
           AC_MSG_RESULT(no)
           AC_MSG_ERROR(Unable to find Python.h in standard locations)
+         fi
        fi
      else
        if test -f $withval/Python.h; then
           PYTHON_INCDIR=-I$withval
-          PYTHON_LIBS="-L$withval/config -lpython -lutil -ldl"
+          PYTHON_LIBS="-L$withval/config -lpython -lutil"
        elif test -f $withval/include/Python.h; then
           PYTHON_INCDIR=-I$withval/include
-          PYTHON_LIBS="-L$withval/lib/config -lpython -lutil -ldl"
+          PYTHON_LIBS="-L$withval/lib/config -lpython -lutil"
        else
           AC_MSG_RESULT(no)
           AC_MSG_ERROR(Invalid Python directory $withval - unable to find Python.h under $withval)
 PSCMD="ps -e"
 WIN32=
 MACOSX=
+PYTHON_LIBS_EXTRAS="-ldl"
 hostname=`uname -n | cut -d '.' -f 1`
 case "$DISTNAME" in
 aix)
            platforms/freebsd/bacula-sd \
            platforms/freebsd/bacula-dir"
        largefile_support="yes"
+       PYTHON_LIBS_EXTRAS=
   ;;
 hpux)
        PSCMD="UNIX95=1 ps -e -o pid,comm"
 AC_SUBST(hostname)
 
 LIBS="$PTHREAD_LIB $LIBS"
+if test "x$PYTHON_LIBS" != x -a "x$PYTHON_LIBS_EXTRAS" != x; then
+  PYTHON_LIBS="$PYTHON_LIBS $PYTHON_LIBS_EXTRAS"
+fi
 
 AC_DEFINE_UNQUOTED(lld, "$lld")
 AC_DEFINE_UNQUOTED(llu, "$llu")
 
   PYTHON_LIBS=
   if test "$withval" != "no"; then
      if test "$withval" = "yes"; then
-       if test -f /usr/include/python2.2/Python.h; then
-          PYTHON_INCDIR=-I/usr/include/python2.2
-          PYTHON_LIBS="-L/usr/lib/python2.2/config -lpython2.2 -lutil -ldl"
-       elif test -f /usr/include/python2.3/Python.h; then
-          PYTHON_INCDIR=-I/usr/include/python2.3
-          PYTHON_LIBS="-L/usr/lib/python2.3/config -lpython2.3 -lutil -ldl"
-       elif test -f /usr/include/python2.4/Python.h; then
-          PYTHON_INCDIR=-I/usr/include/python2.4
-          PYTHON_LIBS="-L/usr/lib/python2.4/config -lpython2.4 -lutil -ldl"
-       elif test -f $prefix/include/Python.h; then
-          PYTHON_INCDIR=-I$prefix/include
-          PYTHON_LIBS="-L$prefix/lib/config -lpython -lutil -ldl"
-       else
+       for python_root in /usr /usr/local; do
+         if test -f $python_root/include/python2.2/Python.h; then
+            PYTHON_INCDIR=-I$python_root/include/python2.2
+            PYTHON_LIBS="-L$python_root/lib/python2.2/config -lpython2.2 -lutil"
+            break
+         elif test -f $python_root/include/python2.3/Python.h; then
+            PYTHON_INCDIR=-I$python_root/include/python2.3
+            PYTHON_LIBS="-L$python_root/lib/python2.3/config -lpython2.3 -lutil"
+            break
+         elif test -f $python_root/include/python2.4/Python.h; then
+            PYTHON_INCDIR=-I$python_root/include/python2.4
+            PYTHON_LIBS="-L$python_root/lib/python2.4/config -lpython2.4 -lutil"
+            break
+         fi
+       done
+       if test x$PYTHON_INCDIR = x; then
+         if test -f $prefix/include/Python.h; then
+            PYTHON_INCDIR=-I$prefix/include
+            PYTHON_LIBS="-L$prefix/lib/config -lpython -lutil"
+         else
           echo "$as_me:$LINENO: result: no" >&5
 echo "${ECHO_T}no" >&6
           { { echo "$as_me:$LINENO: error: Unable to find Python.h in standard locations" >&5
 echo "$as_me: error: Unable to find Python.h in standard locations" >&2;}
    { (exit 1); exit 1; }; }
+         fi
        fi
      else
        if test -f $withval/Python.h; then
           PYTHON_INCDIR=-I$withval
-          PYTHON_LIBS="-L$withval/config -lpython -lutil -ldl"
+          PYTHON_LIBS="-L$withval/config -lpython -lutil"
        elif test -f $withval/include/Python.h; then
           PYTHON_INCDIR=-I$withval/include
-          PYTHON_LIBS="-L$withval/lib/config -lpython -lutil -ldl"
+          PYTHON_LIBS="-L$withval/lib/config -lpython -lutil"
        else
           echo "$as_me:$LINENO: result: no" >&5
 echo "${ECHO_T}no" >&6
 PSCMD="ps -e"
 WIN32=
 MACOSX=
+PYTHON_LIBS_EXTRAS="-ldl"
 hostname=`uname -n | cut -d '.' -f 1`
 case "$DISTNAME" in
 aix)
            platforms/freebsd/bacula-sd \
            platforms/freebsd/bacula-dir"
        largefile_support="yes"
+       PYTHON_LIBS_EXTRAS=
   ;;
 hpux)
        PSCMD="UNIX95=1 ps -e -o pid,comm"
 
 
 LIBS="$PTHREAD_LIB $LIBS"
+if test "x$PYTHON_LIBS" != x -a "x$PYTHON_LIBS_EXTRAS" != x; then
+  PYTHON_LIBS="$PYTHON_LIBS $PYTHON_LIBS_EXTRAS"
+fi
 
 cat >>confdefs.h <<_ACEOF
 #define lld "$lld"
 
 General:
 
 Changes to 1.37.32:
+29Jul05
+- Apply user's patch to make mutiple modifiers for times
+  work correctly.
+- Make read_dev_volume_label() handle ANSI/IBM labels
+  correctly -- ie space over any label at the beginning
+  of the tape.
 28Jul05
+- Make ANSI/IBM writing of HDR1/2 labels ignore any
+  errors if at end of tape.
+- Apply Martin's patch to improve Python detection in
+  configure.in
 - Temporarily turn off disk seeking until I find the
   cause of the problem.
 27Jul05
 
    case T_UNQUOTED_STRING:
       bstrncpy(bsize, lc->str, sizeof(bsize));  /* save first part */
       /* if terminated by space, scan and get modifier */
-      if (lc->ch == ' ') {
+      while (lc->ch == ' ') {
          token = lex_get_token(lc, T_ALL);
          switch (token) {
+         case T_NUMBER:
          case T_IDENTIFIER:
          case T_UNQUOTED_STRING:
             bstrncat(bsize, lc->str, sizeof(bsize));
    case T_UNQUOTED_STRING:
       bstrncpy(period, lc->str, sizeof(period));  /* get first part */
       /* if terminated by space, scan and get modifier */
-      if (lc->ch == ' ') {
+      while (lc->ch == ' ') {
          token = lex_get_token(lc, T_ALL);
          switch (token) {
+         case T_NUMBER:
          case T_IDENTIFIER:
          case T_UNQUOTED_STRING:
             bstrncat(period, lc->str, sizeof(period));
 
                goto default_path;
             }
             
+#ifdef xxx_needed
             /* If we have a dvd that requires mount, 
              * we need to try to open the label, so the info can be reported
-             * if a wrong volume has been mounted. */
-/*            if (dev->is_dvd() && (dcr->VolCatInfo.VolCatParts > 0)) {
+             * if a wrong volume has been mounted.   
+             */
+            if (dev->is_dvd() && (dcr->VolCatInfo.VolCatParts > 0)) {
                break;
-            }*/
+            }  
+#endif
             
             Jmsg3(jcr, M_FATAL, 0, _("Open device %s Volume \"%s\" failed: ERR=%s\n"),
                 dev->print_name(), dcr->VolumeName, strerror_dev(dev));
          Dmsg1(100, "opened dev %s OK\n", dev->print_name());
       }
       
-      vol_label_status = read_dev_volume_label(dcr);
+      /* Read Volume Label */
       
       Dmsg0(200, "calling read-vol-label\n");
+      vol_label_status = read_dev_volume_label(dcr);
       switch (vol_label_status) {
       case VOL_OK:
          vol_ok = true;
 
       if (label_type == B_IBM_LABEL) {
          ascii_to_ebcdic(label, label, sizeof(label));
       }
+
+      /*
+       * This could come at the end of a tape, ignore
+       *  EOT errors.
+       */
       stat = write(dev->fd, label, sizeof(label));
       if (stat != sizeof(label)) {
          berrno be;
-         Jmsg1(jcr, M_FATAL, 0, _("Could not write ANSI HDR1 label. ERR=%s\n"),
-            be.strerror());
-         return false;
+         if (stat == -1) {
+            clrerror_dev(dev, -1);
+            if (dev->dev_errno == 0) {
+               dev->dev_errno = ENOSPC; /* out of space */
+            }
+            if (dev->dev_errno != ENOSPC) {
+               Jmsg1(jcr, M_FATAL, 0, _("Could not write ANSI HDR1 label. ERR=%s\n"),
+               be.strerror());
+               return false;
+            }
+         } else {
+            Jmsg(jcr, M_FATAL, 0, _("Could not write ANSI HDR1 label.\n"));
+            return false;
+         }
       }
 
-
       /* Now construct HDR2 label */
       memset(label, ' ', sizeof(label));
       ser_begin(label, sizeof(label));
       stat = write(dev->fd, label, sizeof(label));
       if (stat != sizeof(label)) {
          berrno be;
-         Jmsg1(jcr, M_FATAL, 0, _("Could not write ANSI HDR1 label. ERR=%s\n"),
-            be.strerror());
-         return false;
+         if (stat == -1) {
+            clrerror_dev(dev, -1);
+            if (dev->dev_errno == 0) {
+               dev->dev_errno = ENOSPC; /* out of space */
+            }
+            if (dev->dev_errno != ENOSPC) {
+               Jmsg1(jcr, M_FATAL, 0, _("Could not write ANSI HDR1 label. ERR=%s\n"),
+               be.strerror());
+               return false;
+            }
+            weof_dev(dev, 1);
+            return true;
+         } else {
+            Jmsg(jcr, M_FATAL, 0, _("Could not write ANSI HDR1 label.\n"));
+            return false;
+         }
       }
       if (weof_dev(dev, 1) < 0) {
          Jmsg(jcr, M_FATAL, 0, _("Error writing EOF to tape. ERR=%s"), dev->errmsg);
 
    DEV_BLOCK *block = dcr->block;
    int stat;
    bool want_ansi_label;
+   bool have_ansi_label = false;
 
    Dmsg3(100, "Enter read_volume_label device=%s vol=%s dev_Vol=%s\n",
       dev->print_name(), VolName, dev->VolHdr.VolumeName[0]?dev->VolHdr.VolumeName:
             Jmsg(jcr, M_FATAL, 0, "Too many tries: %s", jcr->errmsg);
          }
          Dmsg0(100, "return VOL_NAME_ERROR\n");
-         return VOL_NAME_ERROR;
+         stat = VOL_NAME_ERROR;
+         goto bail_out;
       }
       Dmsg0(30, "Leave read_volume_label() VOL_OK\n");
       return VOL_OK;       /* label already read */
       stat = read_ansi_ibm_label(dcr);            
       /* If we want a label and didn't find it, return error */
       if (want_ansi_label && stat != VOL_OK) {
-         empty_block(block);
-         rewind_dev(dev);
-         return stat;
+         goto bail_out;
       }
       if (stat == VOL_NAME_ERROR || stat == VOL_LABEL_ERROR) {
          Mmsg(jcr->errmsg, _("Wrong Volume mounted on device %s: Wanted %s have %s\n"),
          if (!dev->poll && jcr->label_errors++ > 100) {
             Jmsg(jcr, M_FATAL, 0, "Too many tries: %s", jcr->errmsg);
          }
-         empty_block(block);
-         rewind_dev(dev);
-         Dmsg1(100, "return %d\n", stat);
-         return stat;
+         goto bail_out;
       }
       if (stat != VOL_OK) {           /* Not an ANSI/IBM label, so re-read */
          rewind_dev(dev);
+      } else {
+         have_ansi_label = true;
       }
    }
   
       ok = true;
    }
    free_record(record);               /* finished reading Volume record */
-   empty_block(block);                /* done with block */
 
    if (!ok) {
       if (forge_on || jcr->ignore_label_errors) {
          dev->set_labeled();         /* set has Bacula label */
          Jmsg(jcr, M_ERROR, 0, "%s", jcr->errmsg);
+         empty_block(block);
          return VOL_OK;
       }
-      rewind_dev(dev);
-      Dmsg0(100, "return VOL_NO_LABEL\n");
-      return VOL_NO_LABEL;
+      stat = VOL_NO_LABEL;
+      goto bail_out;
    }
 
    /* At this point, we have read the first Bacula block, and
     * make sure we have the right Volume.
     */
 
-   /* If we are a streaming device, we only get one chance to read */
-   if (!dev_cap(dev, CAP_STREAM)) {
-      rewind_dev(dev);
-   }
 
    if (dev->VolHdr.VerNum != BaculaTapeVersion &&
        dev->VolHdr.VerNum != OldCompatibleBaculaTapeVersion1 &&
       Mmsg(jcr->errmsg, _("Volume on %s has wrong Bacula version. Wanted %d got %d\n"),
          dev->print_name(), BaculaTapeVersion, dev->VolHdr.VerNum);
       Dmsg1(30, "VOL_VERSION_ERROR: %s", jcr->errmsg);
-      return VOL_VERSION_ERROR;
+      stat = VOL_VERSION_ERROR;
+      goto bail_out;
    }
 
    /* We are looking for either an unused Bacula tape (PRE_LABEL) or
          Jmsg(jcr, M_FATAL, 0, "Too many tries: %s", jcr->errmsg);
       }
       Dmsg0(100, "return VOL_LABEL_ERROR\n");
-      return VOL_LABEL_ERROR;
+      stat = VOL_LABEL_ERROR;
+      goto bail_out;
    }
 
    dev->set_labeled();               /* set has Bacula label */
          Jmsg(jcr, M_FATAL, 0, "Too many tries: %s", jcr->errmsg);
       }
       Dmsg0(100, "return VOL_NAME_ERROR\n");
-      return VOL_NAME_ERROR;
+      stat = VOL_NAME_ERROR;
+      goto bail_out;
    }
    Dmsg1(30, "Copy vol_name=%s\n", dev->VolHdr.VolumeName);
 
       dump_volume_label(dev);
    }
    Dmsg0(30, "Leave read_volume_label() VOL_OK\n");
+   /* If we are a streaming device, we only get one chance to read */
+   if (!dev_cap(dev, CAP_STREAM)) {
+      rewind_dev(dev);
+      if (have_ansi_label) {
+         stat = read_ansi_ibm_label(dcr);            
+         /* If we want a label and didn't find it, return error */
+         if (stat != VOL_OK) {
+            goto bail_out;
+         }
+      }
+   }
+   empty_block(block);
    return VOL_OK;
+
+bail_out:
+   empty_block(block);
+   rewind_dev(dev);
+   Dmsg1(100, "return %d\n", stat);
+   return stat;
 }
 
 /*
 
    Copyright (C) 2000-2005 Kern Sibbald
 
    This program is free software; you can redistribute it and/or
-   modify it under the terms of the GNU General Public License as
-   published by the Free Software Foundation; either version 2 of
-   the License, or (at your option) any later version.
+   modify it under the terms of the GNU General Public License
+   version 2 as amended with additional clauses defined in the
+   file LICENSE in the main source directory.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-   General Public License for more details.
-
-   You should have received a copy of the GNU General Public
-   License along with this program; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
-   MA 02111-1307, USA.
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+   the file LICENSE for additional details.
 
  */
 
 
 /* */
 #undef  VERSION
 #define VERSION "1.37.32"
-#define BDATE   "28 July 2005"
-#define LSMDATE "28Jul05"
+#define BDATE   "29 July 2005"
+#define LSMDATE "29Jul05"
 
 /* Debug flags */
 #undef  DEBUG