##############################################################################
# Copyright (c) 2013-2016, Lawrence Livermore National Security, LLC.
# Produced at the Lawrence Livermore National Laboratory.
#
# This file is part of Spack.
# Created by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
# LLNL-CODE-647188
#
# For details, see https://github.com/llnl/spack
# Please also see the LICENSE file for our notice and the LGPL.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License (as
# published by the Free Software Foundation) version 2.1, February 1999.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
# conditions of the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
import os
from subprocess import check_call

import llnl.util.tty as tty
from llnl.util.filesystem import join_path, mkdirp

import spack
from spack.util.executable import which

_SPACK_UPSTREAM = 'https://github.com/llnl/spack'

description = "Create a new installation of spack in another prefix"

def setup_parser(subparser):
    subparser.add_argument('prefix', help="names of prefix where we should install spack")


def get_origin_info():
    git_dir = join_path(spack.prefix, '.git')
    git = which('git', required=True)
    try:
        branch = git('symbolic-ref', '--short', 'HEAD', output=str)
    except ProcessError:
        branch = 'develop'
        tty.warn('No branch found; using default branch: %s' % branch)
    try:
        origin_url = git(
            '--git-dir=%s' % git_dir,
            'config', '--get', 'remote.origin.url',
            output=str)
    except ProcessError:
        origin_url = _SPACK_UPSTREAM
        tty.warn('No git repository found; '
                 'using default upstream URL: %s' % origin_url)
    return (origin_url.strip(), branch.strip())


def bootstrap(parser, args):
    origin_url, branch = get_origin_info()
    prefix = args.prefix

    tty.msg("Fetching spack from origin: %s" % origin_url)

    if os.path.isfile(prefix):
        tty.die("There is already a file at %s" % prefix)

    mkdirp(prefix)

    if os.path.exists(join_path(prefix, '.git')):
        tty.die("There already seems to be a git repository in %s" % prefix)

    files_in_the_way = os.listdir(prefix)
    if files_in_the_way:
        tty.die("There are already files there!  Delete these files before boostrapping spack.",
                *files_in_the_way)

    tty.msg("Installing:",
            "%s/bin/spack" % prefix,
            "%s/lib/spack/..." % prefix)

    os.chdir(prefix)
    git = which('git', required=True)
    git('init', '--shared', '-q')
    git('remote', 'add', 'origin', origin_url)
    git('fetch', 'origin', '%s:refs/remotes/origin/%s' % (branch, branch),
                           '-n', '-q')
    git('reset', '--hard', 'origin/%s' % branch, '-q')

    tty.msg("Successfully created a new spack in %s" % prefix,
            "Run %s/bin/spack to use this installation." % prefix)

@@ -1544,16 +1544,172 @@
 arser):%0A
+    subparser.add_argument(%0A        '-r', '--remote', action='store', dest='remote',%0A        help=%22name of the remote to bootstrap from%22, default='origin')%0A
     subp
@@ -1813,16 +1813,22 @@
 in_info(
+remote
 ):%0A    g
@@ -2106,16 +2106,195 @@
 branch)%0A
+    if remote == 'origin' and %5C%0A       branch not in ('master', 'develop'):%0A        branch = 'develop'%0A        tty.warn('Unknown branch found; using default branch: %25s' %25 branch)%0A
     try:
@@ -2401,19 +2401,24 @@
 ote.
-origin.url'
+%25s.url' %25 remote
 ,%0A  
@@ -2730,16 +2730,27 @@
 in_info(
+args.remote
 )%0A    pr
@@ -2806,21 +2806,33 @@
 rom 
-origin
+'%25s'
 : %25s%22 %25
+ (args.remote,
  ori
@@ -2839,16 +2839,17 @@
 gin_url)
+)
 %0A%0A    if

allow using alternate remotes