Index: LFS/lfs.xsl
===================================================================
--- LFS/lfs.xsl	(revision adbd09b06cef240a6e581bbaac21d2358a69325a)
+++ LFS/lfs.xsl	(revision 2758d9421e479d9a91bbae7a91cff4b4019fa76c)
@@ -84,4 +84,7 @@
   <xsl:param name='local'     select="'n'"/>
   <xsl:param name='log-level' select="'4'"/>
+
+  <!-- The scripts root is needed for printing disk usage -->
+  <xsl:param name='script-root' select="'jhalfs'"/>
 
 <!-- End parameters -->
@@ -170,5 +173,7 @@
       <xsl:text>&#xA;</xsl:text>
       <xsl:if test="sect2[@role='installation']">
-        <xsl:text>cd $PKGDIR&#xA;</xsl:text>
+        <xsl:call-template name="start-script">
+          <xsl:with-param name="order" select="$order"/>
+        </xsl:call-template>
       </xsl:if>
       <xsl:apply-templates select="sect2[not(@revision) or
@@ -193,4 +198,7 @@
       </xsl:if>
       <xsl:text>echo -e "\n\nTotalseconds: $SECONDS\n"&#xA;</xsl:text>
+      <xsl:if test="sect2[@role='installation']">
+        <xsl:call-template name="end-script"/>
+      </xsl:if>
       <xsl:text>exit&#xA;</xsl:text>
     </exsl:document>
@@ -226,5 +234,5 @@
           <xsl:text>mkdir -pv $PKG_DEST/{bin,boot,etc,lib,sbin}
 mkdir -pv $PKG_DEST/usr/{bin,include,lib/pkgconfig,sbin}
-mkdir -pv $PKG_DEST/usr/share/{doc,info,man}
+mkdir -pv $PKG_DEST/usr/share/{doc,info,bash-completion/completions}
 mkdir -pv $PKG_DEST/usr/share/man/man{1..8}
 case $(uname -m) in
@@ -296,5 +304,6 @@
 <!-- remove empty directories -->
           <xsl:text>for dir in $PKG_DEST/usr/share/man/man{1..8} \
-           $PKG_DEST/usr/share/{doc,info,man} \
+           $PKG_DEST/usr/share/bash-completion{/completions,} \
+           $PKG_DEST/usr/share/{doc,info,man,} \
            $PKG_DEST/usr/lib/pkgconfig \
            $PKG_DEST/usr/{lib,bin,sbin,include} \
@@ -356,7 +365,7 @@
 
   <xsl:template match="sect1" mode="pkgmngt">
-    <xsl:param name="dirname" select="chapter05"/>
+    <xsl:param name="dirname" select="'chapter05'"/>
     <!-- The build order -->
-    <xsl:param name="order" select="062"/>
+    <xsl:param name="order" select="'062'"/>
 <!-- The file names -->
     <xsl:variable name="pi-file" select="processing-instruction('dbhtml')"/>
@@ -372,7 +381,8 @@
 set +h
 set -e
-
-cd $PKGDIR
-</xsl:text>
+</xsl:text>
+        <xsl:call-template name="start-script">
+          <xsl:with-param name="order" select="concat($order,'-',position())"/>
+        </xsl:call-template>
         <xsl:apply-templates
            select=".//screen[not(@role) or
@@ -395,5 +405,7 @@
         <xsl:text>
 echo -e "\n\nTotalseconds: $SECONDS\n"
-exit
+</xsl:text>
+        <xsl:call-template name="end-script"/>
+        <xsl:text>exit
 </xsl:text>
       </exsl:document>
@@ -1121,3 +1133,92 @@
   </xsl:template>
 
+  <xsl:template name="basename">
+    <xsl:param name="path" select="''"/>
+    <xsl:choose>
+      <xsl:when test="contains($path,'/') and substring-after($path,'/')!=''">
+        <xsl:call-template name="basename">
+          <xsl:with-param name="path" select="substring-after($path,'/')"/>
+        </xsl:call-template>
+      </xsl:when>
+      <xsl:when test="contains($path,'/') and substring-after($path,'/')=''">
+        <xsl:value-of select="substring-before($path,'/')"/>
+      </xsl:when>
+      <xsl:otherwise>
+        <xsl:value-of select="$path"/>
+      </xsl:otherwise>
+    </xsl:choose>
+  </xsl:template>
+
+  <xsl:template name="start-script">
+    <xsl:param name="order" select="'073'"/>
+<!-- get the location of the system root -->
+    <xsl:text>
+if [ -h /tools ]; then
+  ROOT=$(dirname $(readlink /tools))/
+else
+  ROOT=/
+fi
+SRC_DIR=${ROOT}sources
+<!-- save the timer, so that unpacking, and du is not counted -->
+PREV_SEC=${SECONDS}
+<!-- Set variables, for use by the Makefile and package manager -->
+VERSION=</xsl:text><!-- needed for Makefile, and may be used in PackInstall-->
+    <xsl:copy-of select=".//sect1info/productnumber/text()"/>
+    <xsl:text>
+PKG_DEST=${SRC_DIR}/</xsl:text>
+    <xsl:copy-of select="$order"/>
+    <xsl:text>-</xsl:text>
+    <xsl:copy-of select=".//sect1info/productname/text()"/>
+    <xsl:text>
+    <!-- Get the tarball name from sect1info -->
+PACKAGE=</xsl:text>
+    <xsl:call-template name="basename">
+      <xsl:with-param name="path" select=".//sect1info/address/text()"/>
+    </xsl:call-template>
+    <xsl:text>
+SCRIPT_ROOT=</xsl:text>
+    <xsl:copy-of select="$script-root"/>
+    <xsl:text>
+</xsl:text>
+    <xsl:if test = "( ../@id != 'chapter-temporary-tools' or
+                      starts-with(@id,'ch-system') ) and $pkgmngt = 'y'">
+      <xsl:text>
+source ${ROOT}${SCRIPT_ROOT}/packInstall.sh
+export -f packInstall</xsl:text>
+      <xsl:if test="$wrap-install='y'">
+        <xsl:text>
+export -f wrapInstall
+</xsl:text>
+      </xsl:if>
+    </xsl:if>
+<!-- Get the build directory name and clean remnants of previous attempts -->
+    <xsl:text>
+cd $SRC_DIR
+PKGDIR=$(tar -tf $PACKAGE | head -n1 | sed 's@^./@@;s@/.*@@')
+export PKGDIR VERSION PKG_DEST
+
+if [ -d "$PKGDIR" ]; then rm -rf $PKGDIR; fi
+if [ -d "${PKGDIR%-*}-build" ]; then  rm -rf ${PKGDIR%-*}-build; fi
+
+echo "KB: $(du -skx --exclude=lost+found --exclude=/var/lib --exclude=$SCRIPT_ROOT $ROOT)"
+<!-- At last unpack and change directory -->
+tar -xf $PACKAGE
+cd $PKGDIR
+SECONDS=${PREV_SEC}
+
+# Start of LFS book script
+</xsl:text>
+  </xsl:template>
+
+  <xsl:template name="end-script">
+    <xsl:text>
+# End of LFS book script
+
+echo "KB: $(du -skx --exclude=lost+found --exclude=/var/lib --exclude=$SCRIPT_ROOT $ROOT)"
+cd $SRC_DIR
+rm -rf $PKGDIR
+if [ -d "${PKGDIR%-*}-build" ]; then  rm -rf ${PKGDIR%-*}-build; fi
+</xsl:text>
+  </xsl:template>
+
 </xsl:stylesheet>
