#!/usr/bin/env python

import os, sys, shutil, subprocess

SAGE_ROOT = os.environ['SAGE_ROOT']

os.chdir(os.path.join(SAGE_ROOT, "devel"))

def usage():
    print "\n\n"
    print "sage -clone <new_branch> [-r rev]"
    sys.exit(1)

if len(sys.argv) == 1:
    usage()

if sys.argv[1] == '-r':
    if len(sys.argv) < 4:
        usage()
    sys.argv = [sys.argv[0], sys.argv[3], sys.argv[1], sys.argv[2]]

branch = 'sage-%s'%sys.argv[1]

if os.path.isdir(branch):
    print "Sage library branch %s already exists so I can't clone to it."%branch
    sys.exit(1)

if not os.path.isdir('sage'):
    print "No Sage branch currently selected.  Use 'sage -b [branch_name]'."
    sys.exit(1)

print "Now cloning the current Sage library branch..."
cmd = 'hg clone %s sage %s '%(' '.join(sys.argv[2:]), branch)
print cmd
if os.system(cmd):
    print "Error cloning"
    sys.exit(1)

def cpdir(src, dest):
    """
    Copy autogenerated .c, .cpp and .h files
    """
    # Text possibly appearing in the first line of autogenerated files:
    first_lines = ['Generated by Cython', # .c, .cpp files
                   '#ifndef __PYX_HAVE__sage']  # .h files
    if not os.path.isdir(dest):
        return
    for F in os.listdir(src):
        if os.path.isdir(src + '/' + F):
            cpdir(src + '/' + F, dest + '/' + F)
        else:
            ext = os.path.splitext(F)[-1]
            if ext in ['.h', '.c', '.cpp']:
                if any(line in open(src + '/' + F).readline()
                       for line in first_lines):
                    os.link(src + '/' + F, dest + '/' +F)
                    os.utime(dest + '/' +F, None)

print "Copying over all Cython auto-generated .c, .cpp and .h files..."
cpdir(os.path.abspath('sage/sage'), os.path.abspath(branch + '/sage'))

if os.path.isfile('sage/.cython_version'):
    print "Copying over hidden Cython version file..."
    os.link('sage/.cython_version', branch+'/.cython_version')

def copy_dtree(src_dir, dest_dir):
    src_root = os.path.abspath(src_dir)
    dest_root = os.path.abspath(dest_dir)

    for root, dirs, files in os.walk(src_root):
        nroot = dest_root + root[len(src_root):]
        for d in dirs:
            os.makedirs(nroot+'/'+d)
        for f in files:
            os.link(root+'/'+f,nroot+'/'+f)
            os.utime(nroot+'/'+f, None)

print "Copying over build directory..."
copy_dtree('sage/build', branch + '/build')

print "Copying over all auto-generated reference manual .rst files..."
sys.path.append(os.environ['SAGE_DOC'])
from common.builder import LANGUAGES
for lang in LANGUAGES:
    ref = os.path.join('sage', 'doc', lang, 'reference')
    try:
        for f in os.listdir(ref):
            if os.path.isdir(os.path.join(ref, f)):
                for directory in ['sage', 'sagenb']:
                    fr = os.path.join(ref, f, directory)
                    if os.path.exists(fr):
                        to = os.path.join(branch, 'doc', lang, 'reference', f, directory)
                        cmd = 'cp -pR %s %s' % (fr, to)
                        subprocess.call([cmd], shell=True)
    except:
        pass

print "Copying over documentation output..."
# Use shutil.copytree instead of copy_dtree to make sure that symlinks
# are copied correctly. See #14245.
shutil.copytree('sage/doc/output', branch + '/doc/output', symlinks=True)

print "Building " + branch + "..."
cmd = 'sage -b %s'%sys.argv[1]
print cmd
if os.system(cmd):
    print "Error building Sage"
    sys.exit(1)

HGRC = open(os.path.join(SAGE_ROOT, "devel", "sage", ".hg", "hgrc"),
            mode='a')
HGRC.write("""
[diff]
git = true
""")
HGRC.close()

print """
*** WARNING ***
If *before* cloning, you changed any Sage library files without
rebuilding the reference manual, then after cloning, you will
need to touch those files again if you want the changes to be
incorporated into the reference manual.

*** WARNING ***
If you are cloning a previous revision or have uncommitted
changes to cython files, do

    sage -ba

Otherwise Sage might build using the wrong .c files!"""
