/**
  * Writes the package.xml file out with the newly created <release></release> tag
  *
  * ALWAYS use {@link debugPackageFile} to verify that output is correct before
  * overwriting your package.xml
  *
  * @param boolean $debuginterface (optional) null if no debugging, true if web interface, false if command-line
  *
  * @throws PEAR_PACKAGEFILEMANAGER_RUN_SETOPTIONS
  * @throws PEAR_PACKAGEFILEMANAGER_ADD_MAINTAINERS
  * @throws PEAR_PACKAGEFILEMANAGER_NONOTES
  * @throws PEAR_PACKAGEFILEMANAGER_NOLICENSE
  * @throws PEAR_PACKAGEFILEMANAGER_INVALID_PACKAGE
  * @throws PEAR_PACKAGEFILEMANAGER_CANTWRITE_PKGFILE
  * @throws PEAR_PACKAGEFILEMANAGER_CANTCOPY_PKGFILE
  * @throws PEAR_PACKAGEFILEMANAGER_CANTOPEN_TMPPKGFILE
  * @throws PEAR_PACKAGEFILEMANAGER_DEST_UNWRITABLE
  * @return true|PEAR_Error
  * @access public
  * @since  0.1
  */
 function writePackageFile($debuginterface = null)
 {
     if (!$this->_packageXml) {
         return $this->raiseError(PEAR_PACKAGEFILEMANAGER_RUN_SETOPTIONS);
     }
     if (!isset($this->_packageXml['maintainers']) || empty($this->_packageXml['maintainers'])) {
         return $this->raiseError(PEAR_PACKAGEFILEMANAGER_ADD_MAINTAINERS);
     }
     if (!isset($this->_options['notes']) || empty($this->_options['notes'])) {
         return $this->raiseError(PEAR_PACKAGEFILEMANAGER_NONOTES);
     }
     if (!isset($this->_options['license']) || empty($this->_options['license'])) {
         return $this->raiseError(PEAR_PACKAGEFILEMANAGER_NOLICENSE);
     }
     extract($this->_options);
     $date = date('Y-m-d');
     if (isset($package)) {
         $this->_packageXml['package'] = $package;
     }
     if (isset($summary)) {
         $this->_packageXml['summary'] = $summary;
     }
     if (isset($description)) {
         $this->_packageXml['description'] = $description;
     }
     $this->_packageXml['release_date'] = $date;
     $this->_packageXml['version'] = $version;
     $this->_packageXml['release_license'] = $license;
     $this->_packageXml['release_state'] = $state;
     $this->_packageXml['release_notes'] = $notes;
     $PEAR_Common = $this->_options['pearcommonclass'];
     $this->_pear = new $PEAR_Common();
     if (method_exists($this->_pear, 'setPackageFileManager')) {
         $this->_pear->setPackageFileManager($this);
     }
     $this->_packageXml['filelist'] = $this->_getFileList();
     $warnings = $this->getWarnings();
     if (count($warnings)) {
         $nl = isset($debuginterface) && $debuginterface ? '<br />' : "\n";
         foreach ($warnings as $errmsg) {
             echo 'WARNING: ' . $errmsg['message'] . $nl;
         }
     }
     if (PEAR::isError($this->_packageXml['filelist'])) {
         return $this->_packageXml['filelist'];
     }
     if (isset($this->_pear->pkginfo['provides'])) {
         $this->_packageXml['provides'] = $this->_pear->pkginfo['provides'];
     }
     if ($this->_options['simpleoutput']) {
         unset($this->_packageXml['provides']);
     }
     $this->_packageXml['release_deps'] = $this->_getDependencies();
     $this->_updateChangeLog();
     $common =& $this->_pear;
     $warnings = $errors = array();
     if (method_exists($common, 'setPackageFileManagerOptions')) {
         $common->setPackageFileManagerOptions($this->_options);
     }
     $packagexml = $common->xmlFromInfo($this->_packageXml);
     if (PEAR::isError($packagexml)) {
         $errs = $packagexml->getUserinfo();
         if (is_array($errs)) {
             foreach ($errs as $error) {
                 if ($error['level'] == 'error') {
                     $errors[] = $error['message'];
                 } else {
                     $warnings[] = $error['message'];
                 }
             }
         }
     } else {
         $common->validatePackageInfo($packagexml, $warnings, $errors, $this->_options['packagedirectory']);
     }
     if (count($errors)) {
         $ret = '';
         $nl = isset($debuginterface) && $debuginterface ? '<br />' : "\n";
         foreach ($errors as $errmsg) {
             $ret .= $errmsg . $nl;
         }
         return $this->raiseError(PEAR_PACKAGEFILEMANAGER_INVALID_PACKAGE, $nl, $ret);
     }
     if (count($warnings)) {
         $nl = isset($debuginterface) && $debuginterface ? '<br />' : "\n";
         foreach ($warnings as $errmsg) {
             echo $errmsg . $nl;
         }
     }
     if (!strpos($packagexml, '<!DOCTYPE')) {
         // hack to fix pear
         $packagexml = str_replace('<package version="1.0">', '<!DOCTYPE package SYSTEM "' . $this->_options['doctype'] . "\">\n<package version=\"1.0\">", $packagexml);
     }
     if (isset($debuginterface)) {
         if ($debuginterface) {
             echo '<pre>' . htmlentities($packagexml) . '</pre>';
         } else {
             echo $packagexml;
         }
         return true;
     }
     $outputdir = $this->_options['outputdirectory'] ? $this->_options['outputdirectory'] : $this->_options['packagedirectory'];
     if (file_exists($outputdir . $this->_options['packagefile']) && is_writable($outputdir . $this->_options['packagefile']) || @touch($outputdir . $this->_options['packagefile'])) {
         if ($fp = @fopen($outputdir . $this->_options['packagefile'] . '.tmp', "w")) {
             $written = @fwrite($fp, $packagexml);
             @fclose($fp);
             if ($written === false) {
                 return $this->raiseError(PEAR_PACKAGEFILEMANAGER_CANTWRITE_PKGFILE);
             }
             if (!@copy($outputdir . $this->_options['packagefile'] . '.tmp', $outputdir . $this->_options['packagefile'])) {
                 return $this->raiseError(PEAR_PACKAGEFILEMANAGER_CANTCOPY_PKGFILE);
             } else {
                 @unlink($outputdir . $this->_options['packagefile'] . '.tmp');
                 return true;
             }
         } else {
             return $this->raiseError(PEAR_PACKAGEFILEMANAGER_CANTOPEN_TMPPKGFILE, $outputdir . $this->_options['packagefile'] . '.tmp');
         }
     } else {
         return $this->raiseError(PEAR_PACKAGEFILEMANAGER_DEST_UNWRITABLE, $outputdir);
     }
 }