#
# butil.py - get architecture type from anaconda
#
# Erik Troan <ewt@redhat.com>
#
# Copyright 2001, 2002 Red Hat, Inc.
#
# This software may be freely redistributed under the terms of the GNU
# library public license.
#
# You should have received a copy of the GNU Library Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import os

def getArch ():
    arch = os.uname ()[4]
    if (len (arch) == 4 and arch[0] == 'i' and
        arch[2:4] == "86"):
        arch = "i386"

    if arch == "sparc64":
        arch = "sparc"

    if arch == "ppc64":
	arch = "ppc"

    if arch == "s390x":
        arch = "s390"

    return arch

        
# return the ppc machine variety type
def getPPCMachine():
    machine = None
    # ppc machine hash
    ppcType = { 'Mac'      : 'PMac',
                'Book'     : 'PMac',
                'CHRP IBM' : 'pSeries',
                'iSeries'  : 'iSeries',
                'PReP'     : 'PReP',
                'CHRP'     : 'pSeries',
                'Amiga'    : 'APUS',
                'Gemini'   : 'Gemini',
                'Shiner'   : 'ANS',
                'BRIQ'     : 'BRIQ',
		'Teron'    : 'Teron',
		'AmigaOne' : 'Teron'
                }

    if getArch() != "ppc":
        return 0

    f = open('/proc/cpuinfo', 'r')
    lines = f.readlines()
    f.close()
    for line in lines:
        if line.find('machine') != -1:
            machine = line.split(':')[1]
            break

    if machine is None:
        return

    for type in ppcType.items():
        if machine.find(type[0]) != -1:
            return type[1]

    return 0

# return the pmac machine id
def getPPCMacID():
    machine = None
    
    if getArch() != "ppc":
        return 0
    if getPPCMachine() != "PMac":
        return 0

    f = open('/proc/cpuinfo', 'r')
    lines = f.readlines()
    f.close()
    for line in lines:
      if line.find('machine') != -1:
        machine = line.split(':')[1]
        machine = machine.strip()
        return machine

    return 0

# return the pmac generation
def getPPCMacGen():
    # XXX: should NuBus be here?
    pmacGen = ['OldWorld', 'NewWorld', 'NuBus']
    
    if getArch() != "ppc":
        return 0
    if getPPCMachine() != "PMac":
        return 0

    f = open('/proc/cpuinfo', 'r')
    lines = f.readlines()
    f.close()
    gen = None
    for line in lines:
      if line.find('pmac-generation') != -1:
        gen = line.split(':')[1]
        break

    for type in pmacGen:
      if gen.find(type) != -1:
          return type

    return 0

# return if pmac machine is it an iBook/PowerBook
def getPPCMacBook():
    if getArch() != "ppc":
        return 0
    if getPPCMachine() != "PMac":
        return 0

    f = open('/proc/cpuinfo', 'r')
    lines = f.readlines()
    f.close()

    for line in lines:
      if not string.find(string.lower(line), 'book') == -1:
        return 1
    return 0

# returns a product name to use for the boot loader string
def getProductName():
    # try e-smith-release first
    if os.access("/etc/e-smith-release", os.R_OK):
        f = open("/etc/e-smith-release", "r")
        lines = f.readlines()
        f.close()
        for buf in lines:
            relidx = buf.find(" release")
            if relidx != -1:
                return buf[:relidx]

    if os.access("/tmp/product/.buildstamp", os.R_OK):
        path = "/tmp/product/.buildstamp"
    elif os.access("/.buildstamp", os.R_OK):
        path = "/.buildstamp"
    else:
        path = None

    if path is not None:
        f = open(path, "r")
        lines = f.readlines()
        f.close()
        if len(lines) >= 2:
            return lines[1][:-1]

    # fall back
    return "Red Hat Linux"
        
            
