#!/usr/bin/python
#
# mk-rescueimage.ppc - builds a rescue image from an existing tree
#                      which can be converted into a yaboot bootable
#                      iso and used as a sysadmin rescue image.
#
#
# Usage: mk-rescueimage.ppc <src-tree> <dest-dir> <productname>
#
# the rescue image will be created as <dest-dir>/ppc-rescueimage
#

import os
import sys
import string

def usage():
    print "usage: mk-rescueimage.ppc <toplevel> <dest-dir> <productname> <productpath>"

if len(sys.argv) < 5:
    usage()
    sys.exit(1)
    
if not os.access("%s/ppc" % (sys.argv[1],), os.F_OK):
    print "ERROR - Source directory %s does not contain a ppc directory!" % (sys.argv[2],)
    sys.exit(1)

srcdir  = sys.argv[1]
destdir = sys.argv[2]+"/ppc-rescueimage"
productname = sys.argv[3]
productpath = sys.argv[4]

# clean and create destination directory
os.system("rm -rf %s" % (destdir,))
os.system("mkdir %s" % (destdir,))

# cp boot_image
os.system("cp -a %s/boot_image %s/" % (srcdir, destdir))

# copy documentation
for pat in ["*eula*", "*EULA*", "README*", "RELEASE*", "GPL", "RPM-*"]:
    os.system("cp -p %s/%s       %s/ 2>/dev/null" % (srcdir, pat, destdir))

# cp yaboot files
os.system("cp -a %s/ppc     %s/" % (srcdir, destdir))
os.system("cp -a %s/etc     %s/" % (srcdir, destdir))

# cp stage image
os.system("mkdir -p %s/images" % (destdir,))
os.system("cp -a %s/images/stage2.img %s/images" % (srcdir, destdir))

for arch in ("ppc32", "ppc64"):
    # munge various yaboot configs to have a default of rescue mode
    cfgfile = open("%s/ppc/%s/yaboot.conf" % (destdir, arch), "r")
    cfglines = cfgfile.readlines()
    cfgfile.close()

    # backup old one
    os.system("cp %s/ppc/%s/yaboot.conf %s/ppc/%s/yaboot.conf.backup" % (destdir, arch, destdir, arch))

    cfgfile = open("%s/ppc/%s/yaboot.conf" % (destdir, arch), "w+")
    fndit = 0
    for l in cfglines:
        if string.find(l, "Welcome") != -1:
            cfgfile.write(l.replace("installer", "rescue"))
            continue
        if string.find(l, "default=") != -1:
            cfgfile.write("default=rescue\n")
            continue
        # see if we're into the stanza now
        elif string.find(l, "image=") != -1:
            fndit = 1
            # build up the rescue stanza based on whats in the
            # default stanza as we echo it out
            rescuestanza = l + "        label=rescue\n"
            # if we fall through then write it out
            cfgfile.write(l)
        else:
            # we're writing out the matching stanza - if we hit the append
            # line put rescue on the end of it, otherwise just echo line
            if fndit:
                if string.find(l, "read-only") != -1:
                    rescuestanza = rescuestanza + l + "        append=rescue\n"
                else:
                    if not string.find(l, "label=") != -1:
                        rescuestanza = rescuestanza + l
            cfgfile.write(l)
    
    if fndit:
        cfgfile.write('\n' + rescuestanza)

    cfgfile.close()

    if not fndit:
        print "Could not find default stanza, did not modify yaboot.conf"
        os.system("mv -f %s/ppc/%s/yaboot.conf.backup %s/ppc/%s/yaboot.conf" % (destdir, arch, destdir, arch))
    else:
        os.system("rm -f %s/ppc/%s/yaboot.conf.backup" % (destdir, arch))

# Modify top level yaboot to indicate rescue CD instead of install CD
#

cfgfile = open("%s/etc/yaboot.conf" % (destdir,), "r")
cfglines = cfgfile.readlines()
cfgfile.close()

cfgfile = open("%s/etc/yaboot.conf" % (destdir,), "w")

origlines = ''
rescuelines = ''

# do some inline replacements of text
for l in cfglines:
    if l.find("init-message") != -1:
        entry = cfglines.index(l)
        cfglines[entry] = cfglines[entry].replace("installer", "rescue")
        cfglines[entry] = cfglines[entry].replace("linux32", "rescue32")
        continue
    if l.find("default=") != -1:
        cfglines[cfglines.index(l)] = l.replace("linux", "rescue")
        continue
    if l.find("image=/ppc/ppc64") != -1:
        origlines = cfglines[cfglines.index(l):]
        break
        
# create a big string that is easier to do replacements on
for l in origlines:
     rescuelines += l

# Do replacements on the big string
rescuelines = rescuelines.replace("label=linux64", "label=rescue64")
rescuelines = rescuelines.replace("alias=linux", "alias=rescue")
rescuelines = rescuelines.replace("label=linux32", "label=rescue32")
rescuelines = rescuelines.replace("read-only", "read-only\n        append=rescue")

for l in cfglines:
    cfgfile.write(l)

cfgfile.write('\n' + rescuelines)