#! /bin/sh

if [ "$1" = "" ] ; then
  echo ""
  echo 'Usage: pi-makelist cdimage.iso > cdimage.list'
  echo ""
  echo "Will print on stdout the ordered list of files in the ISO 9660 image."
  echo ""
  echo 'Copyright (C) 1999 J.A. Bezemer           <costar@panic.et.tudelft.nl>' 
  echo "Released under GNU GPL     NO WARRANTY AT ALL     USE AT YOUR OWN RISK" 
  echo ""

  exit
fi

# In my testing situation, isoinfo was in . but . wasn't in the PATH.
PATH="$PATH":.
export PATH

# We need a tempfile. tempfile(1) is in debianutils >= 1.6
# You can also insert some fixed name here, but then you can run
# only one pi-makelist at a time.
TEMPF=`tempfile --prefix pimkl --mode 644`


# The rest is one long pipe. The best way to find out why it works is
# to comment out all but the first command, and then remove one hash
# each run.

isoinfo -i "$1" -R -l -f			\
  | grep -B 1 '^-'				\
  | grep -v '^--$'				\
  | (
      while read ThisFile; do
	read ThisInfoLine

	echo "$ThisInfoLine" ["$ThisFile"]
      done
    )						\
  | tr ']' '['					\
  | cut -d '[' -f 2,4				\
  | sort					\
  | cut -d '[' -f 2				\
  | grep -v					\
	    -e '/Packages'			\
	    -e '/Sources'			\
	    -e 'TRANS\.TBL$'			\
	    -e '^/md5sum\.txt$'			\
	    -e '/Release-Notes$'		\
	    -e '^/README\.1ST$'			\
	    -e '^/README\.multicd$'		\
	    -e '^/\.disk/'			\
	    -e '^/boot/'			\
	    -e '^/tools/.*/'			\
	    -e '^/tools/README\.tools$'		\
	    -e '\.info$'			\
	    -e '\.m68k$'			\
	    -e '^/m68k-faq/'			\
	    -e '^/m68k-tools/'			\
	    -e '^/extras/'			\
	    -e '^/dedication\.txt$'		\
	    -e '^/README\.html$'		\
	    -e '^/README\.txt$'			\
	    -e '^/pics/'			\
	    -e '^/doc/package-developer/hello'	\
	    -e '^/milo/'			\
	    -e '^/apb/'				\
	    -e '^/linux$'			\
	    -e '^/bin/'				\
	    -e '^/etc/'				\
	    -e '^/lib/'				\
	    -e '^/release_notes$'		\
	    -e '^/sbin/'			\
	    -e '^/tmp/'				\
	    -e '^/usr/'				\
  > "$TEMPF"

# Temporarily block the pipe, since we need to inspect it's contents

# Some magic to determine the architecture and codename
# (Doesn't work for source CDs, but isn't needed there, either)
L=`						\
      grep '/dists/.*/binary-'			\
    < "$TEMPF"					\
    | grep -v '/binary-all/'			\
    | head -1					\
    | sed -e 's|/dists/\([^/]*\)/.*/binary-\([^/]*\)/.*|\1:\2|'	\
  `

ARCH=`echo "$L" | cut -d ':' -f 2`
CODENAME=`echo "$L" | cut -d ':' -f 1`
#echo Arch: "$ARCH" Codename: "$CODENAME"

# Now the rest of the pipe

    sed -e 's|^/upgrade/|/dists/'"$CODENAME"'/main/upgrade-'"$ARCH"'/|'	\
  < "$TEMPF"					\
  | (
      if [ x"$ARCH" = xm68k ] ; then
	sed -e 's|^/install/|/dists/'"$CODENAME"'/main/disks-'"$ARCH"'/current/|'
      else
	grep -v '^/install'
      fi
    )						\
  | (
      # See if it's really non-US
      # If not, then also delete non-US/{Contents,Release} etc.
      if grep -q '^/dists/.*/non-US/.*\.d[es][bc]$' "$TEMPF" ; then
	cat
      else
	grep -v '^/dists/.*/non-US/'
      fi
    )						\
  | grep -v '/disks-.*/index\.html$'


# Remove tempfile
rm -f "$TEMPF"


# Packages[.cd][.gz] are usually not the same as those on FTP. Same with
# Sources[.gz].
# And the /tools are only zipped on FTP.
# /xxx.info is only on the m68k CDs and not on FTP.
# *.m68k is not on FTP; m68k-faq and -tools are not on the Debian FTPs.