#!/usr/bin/python
#
# This python script will hopefully handle all of the unpacking and copying
# needed to make a Debian/m68k CD's "install" tree.
# It snarfs the "cd-map" file Michael included in the boot floppies, and also
# does a lot of crap I got tired of doing manually.
#
# You need the following Debian packages: python-base; tar; macutils; lha
#
# If you're burning a CD, use the mkhybrid in potato (HFS is broken in the
# slink version of mkhybrid; at least it is for me).  Please don't use Joliet
# extensions; some Mac kernels have apparently been known to choke on them
# (and you really shouldn't be supporting a Microsoft Standard anyway :-)
#
# To make the CD bootable on BVME6000 systems, feed this env. var. to slink_cd:
# MKISOFS_DISC1_OPTS="-b install/bvme6000/resc1440.bin
#                     -c install/bvme6000/boot.catalog" (all one line)
#
# You may also want the following:
# * A copy of the Linux/m68k FAQ
# * A copy of my m68k-specific README and m68k-tools directories
# * The m68k kernel sources from sunsite.auc.dk:/projects/680x0
#   (Sunsite-Denmark is rsync-capable at sunsite.auc.dk::ftp)
#   I recommend getting the 2.0.36 and 2.2.1-pre2 sources at least.
#   (Actually 2.0.36 is non-essential since it's on the CDs already;
#    you probably DO want the 2.2.1-pre2 tree however [under v2.1!]).
#
# MVME/BVME users will love you if you also include the "tools" directory
# from the FTP site, since they may want/need rawwrite for MS-DOS.
#
# The first two items are in a tar file at master.debian.org:~lawrencc, along
# with a silly rsync script that will accomplish the last item.
#
# Enjoy!  This is all completely free.
# - Chris Lawrence <lawrencc@debian.org>
#
# Slightly modified by Steve McIntyre <stevem@chiark.greenend.org.uk>
# to interface better with the slink_cd package, 24 Feb 1999.
#
# $1 is Debian-mirror location
# $2 is start directory location (where the scripts live)
# $3 is tmpdir location
# $4 is the binary arch
# $5 is debug level

import string, re, sys, os

splitter = re.compile(r'\s+')

if len(sys.argv) < 6:
    print 'Usage: install-disks <mirror dir> <slink_cd dir> <tmpdir> <arch> <debuglevel>'
    sys.exit(1)

mirror = sys.argv[1]
basedir = sys.argv[2]
tmpdir = sys.argv[3]
arch = sys.argv[4]
debug = sys.argv[5]

disksroot = os.path.join(mirror, "dists/slink/main/disks-m68k/current")
cdroot = os.path.join(tmpdir, "slink1")

if len(sys.argv) == 7:
    mapname = sys.argv[6]
else:
    mapname = os.path.join(disksroot, "source/cd-map")

# Ensure we have absolute paths
disksroot = os.path.normpath(os.path.join(os.getcwd(), disksroot))
cdroot = os.path.normpath(os.path.join(os.getcwd(), cdroot))
mapname = os.path.normpath(os.path.join(os.getcwd(), mapname))

print 'Disksroot:', disksroot
print 'CDroot:', cdroot
print 'Mapfile:', mapname

# Unpack install disks
installdir = os.path.join(cdroot, "install")

os.system("rm -rf '%s'" % installdir)
os.system("mkdir -p '%s'" % installdir)
os.chdir(installdir)

# Amiga installer
print
print 'Installing Amiga files'
os.system("lha xqf "+os.path.join(disksroot, "amiga/amigainstall.lha"))
os.rename("debian", "amiga")
os.rename("debian.info", "amiga.info")

# Atari installer
print
print 'Installing Atari files'
os.system("lha xqf "+os.path.join(disksroot, "atari/install.lzh"))
os.rename("debian", "atari")

# Mac installer
print
print 'Installing Mac files'
os.system("tar -C .. -zxf "+
          os.path.join(disksroot, "source/macinstall.tar.gz"))

mapfile = open(mapname)
while 1:
    line = mapfile.readline()
    if not line: break

    src, dest = splitter.split(string.strip(line), 1)
    while os.path.isabs(dest): dest = dest[1:]

    src = os.path.join(disksroot, src)
    dest = os.path.join(cdroot, dest)
    
    if os.path.exists(src):
        (pathpart, file) = os.path.split(dest)
        print 'Copying %s to %s' % (os.path.basename(src), pathpart)
        if not os.path.exists(pathpart):
            os.system("mkdir -p '%s'" % pathpart)
        os.system("cp '%s' '%s'" % (src, dest) )
    else:
        print "Warning:", src, "not found."

mapfile.close()

# Convert Mac hqx files for use by mkhybrid -probe
os.chdir(os.path.join(installdir, "mac"))
os.system("hexbin *.hqx")
