#!/usr/bin/python
"""
Build the OZSWget bootstrap package.
Creates a raw SVR4 stream package at /tmp/ozsw-get.pkg suitable for:
  pkgadd -d http://operz.coralcmd.net/pub/osol/ozone/get
"""
import os, subprocess, shutil, time

PKGNAME    = "OZSWget"
VERSION    = "1.0"
MIRROR     = "http://operz.coralcmd.net/pub/osol/ozone/pkg"
PKGUTIL_SRC = "/export/home/ai/src/ozone/ozone/mgar/pkgutil/pkgutil"
BUILD      = "/tmp/ozsw-get-build"
ROOT       = BUILD + "/root"
SPOOL      = BUILD + "/spool"
OUT        = "/tmp/ozsw-get.pkg"

# --- clean slate ---
if os.path.exists(BUILD):
    shutil.rmtree(BUILD)
for d in (ROOT+"/opt/ozsw/bin", ROOT+"/opt/ozsw/etc",
          BUILD+"/install", SPOOL):
    os.makedirs(d)

# --- pkgutil binary ---
shutil.copy(PKGUTIL_SRC, ROOT+"/opt/ozsw/bin/pkgutil")
os.chmod(ROOT+"/opt/ozsw/bin/pkgutil", 0755)

# --- pkgutil.conf ---
open(ROOT+"/opt/ozsw/etc/pkgutil.conf", "w").write(
    "mirror=%s\n" % MIRROR)

# --- postinstall script ---
postinstall = r"""#!/bin/sh
# Create pkgutil working directory and admin file
mkdir -p /var/opt/ozsw/pkgutil

cat > /var/opt/ozsw/pkgutil/admin << 'ENDADMIN'
mail=
instance=overwrite
partial=nocheck
runlevel=nocheck
idepend=nocheck
rdepend=nocheck
space=nocheck
setuid=nocheck
conflict=nocheck
action=nocheck
networktimeout=60
networkretries=3
authentication=quit
keystore=/var/sadm/security
proxy=
basedir=default
ENDADMIN

echo ""
echo "========================================================"
echo " OZSW pkgutil bootstrap installed successfully."
echo ""
echo " Add /opt/ozsw/bin to your PATH, then:"
echo "   sudo pkgutil -U       # fetch package catalog"
echo "   sudo pkgutil -a       # list available packages"
echo "   sudo pkgutil -i NAME  # install a package"
echo "========================================================"
"""
pi_path = BUILD + "/install/postinstall"
open(pi_path, "w").write(postinstall)
os.chmod(pi_path, 0755)

# --- pkginfo ---
pstamp = time.strftime("%Y%m%d%H%M%S")
open(BUILD+"/pkginfo", "w").write("""\
PKG=%s
NAME=get - OZSW bootstrap installer
ARCH=i386
VERSION=%s
BASEDIR=/
CATEGORY=application
VENDOR=http://operz.coralcmd.net/ packaged for OZSW
EMAIL=operz@coralcmd.net
PSTAMP=%s
CLASSES=none
OPENCSW_CATALOGNAME=get
DESC=Bootstraps OZSW: installs pkgutil and configures the package mirror
""" % (PKGNAME, VERSION, pstamp))

# --- prototype ---
proto = """\
i pkginfo=pkginfo
i postinstall=install/postinstall
d none opt/ozsw 0755 root bin
d none opt/ozsw/bin 0755 root bin
d none opt/ozsw/etc 0755 root bin
f none opt/ozsw/bin/pkgutil 0755 root bin
f none opt/ozsw/etc/pkgutil.conf 0644 root bin
"""
open(BUILD+"/prototype", "w").write(proto)

# --- pkgmk: build package into spool directory ---
ret = subprocess.call([
    "pkgmk", "-o",
    "-r", ROOT,
    "-d", SPOOL,
    "-f", BUILD+"/prototype",
    "-p", pstamp,
])
if ret != 0:
    raise SystemExit("pkgmk failed with code %d" % ret)

# --- pkgtrans: convert to stream format (uncompressed) ---
if os.path.exists(OUT):
    os.unlink(OUT)
ret = subprocess.call([
    "pkgtrans", "-s", SPOOL, OUT, PKGNAME
])
if ret != 0:
    raise SystemExit("pkgtrans failed with code %d" % ret)

print "Built: %s (%d bytes)" % (OUT, os.path.getsize(OUT))
