#!/usr/bin/env python2

import os, string, shutil, glob

# The directory into which the distribution is to be installed:
publishing_directory = '/mit/linux/rh-7.1/install-test'

# Make sure everything's readable
perm_command = 'find %s -type d ' \
               '-exec fs sa {} system:kludgey-linux-testers rl \;'
assert not os.system(perm_command % publishing_directory)

# Where to put files that need to be publically accessible
public_directory = '/afs/sipb.mit.edu/project/linux/rh-7.1/'
permissions = os.popen('fs la %s' % public_directory).read()
assert permissions.find('system:anyuser rl') != -1

# The stock redhat install that is to be used.
redhat_image = '/afs/athena.mit.edu/system/rhlinux/redhat-7.1'

# The initial rpm's that are installed during the bootstrap.
rpms = ['wget-1.6-2.i386.rpm', 'gmp-3.1.1-3.i386.rpm',
        'python-1.5.2-30.i386.rpm']

assert not os.system('attach -h -n -q linux')

if not os.path.exists('publish.py'):
    print 'This must be run from the directory that contains it.'
    sys.exit(1)

# Create the directory the install's in, if need be.
if not os.path.exists(publishing_directory):
    os.mkdir(publishing_directory)
install_directory = os.path.join(publishing_directory, 'install')
if not os.path.exists(install_directory): os.mkdir(install_directory)

# Record the path to the install directory in the documentation
open('publishingLocation.tex', 'w').write(
    """\def\publishedLocation{%s}
\def\imageLocation{%s}
""" % (publishing_directory, redhat_image))

# Get the path to the install to tell the scripts.
assert publishing_directory[:5] == '/mit/'
publish_path = publishing_directory[5:]

# Set up the bootstrap script with this information.
install_path = os.path.join(publishing_directory, 'install.sh')
install_sh = open(install_path, 'w')
install_sh.write("""#!/bin/sh

ATHENA_REDHAT_IMAGE=%s
PUBLISH_PATH=%s
RPMS="%s"

""" % (redhat_image, publish_path, string.join(rpms)))
install_sh.write(open('install.sh.in').read())

# Set up the script to create the install disk
floppy_path = os.path.join(publishing_directory,
                           'kludgey-install-floppy')
floppy_sh = open(floppy_path, 'w')
floppy_sh.write("""#!/bin/sh

ATHENA_REDHAT_IMAGE=%s
PUBLISH_PATH=%s

""" % (redhat_image, publish_path))
floppy_sh.write(open('make_disk.sh.in').read())
assert not os.system('chmod a+x %s' % floppy_path)

# Create the program that gets downloaded by the bootstrap script.
manifest = []
for line in open('MANIFEST').readlines():
    manifest.append(string.strip(line))

for file in manifest:
    shutil.copy(file, os.path.join(install_directory, file))

install_files = glob.glob(os.path.join(install_directory, '*'))
install_names = []
for file in install_files: install_names.append(os.path.basename(file))
install_names.sort()
manifest.sort()

assert manifest == install_names

tarball = os.path.join(public_directory, 'install.tar.gz')
assert not os.system('gtar zcf %s -C %s %s' % (
    tarball, publishing_directory, 'install'))
tarlink = os.path.join(publishing_directory, 'install.tar.gz')
print tarball
print tarlink
if os.path.exists(tarlink): os.remove(tarlink)
os.symlink(tarball, tarlink)

# Publish the documentation
if not os.path.exists('latex'): os.mkdir('latex')
os.chdir('latex')
for filename in ('install.tex', 'publishingLocation.tex'):
    if not os.path.exists(filename):
        os.symlink('../'+filename, filename)
assert not os.system('latex install')
assert not os.system('latex install')
doc_path = os.path.join(publishing_directory, 'install.ps')
assert not os.system('dvips -o %s install' % doc_path)
