<commit_before>#!/bin/bash
#
# Copyright (c) 2011, 2012, Joyent Inc., All rights reserved.
#

set -o errexit
set -o pipefail

function usage()
{
    echo "Usage: $0 <platform URI>"
    echo "(URI can be file:///, http://, anything curl supports or a filename)"
    exit 1
}

input=$1
if [[ -z ${input} ]]; then
    usage
fi

if echo "${input}" | grep "^[a-z]*://"; then
    # input is a url style pattern
    /bin/true
else
    if [[ -f ${input} ]]; then
       dir=$(cd $(dirname ${input}); pwd)
       file=$(basename ${input})
       input="file://${dir}/${file}"
    else
       echo "File: '${input}' not found."
       usage
    fi
fi

mounted="false"
usbmnt="/mnt/$(svcprop -p 'joyentfs/usb_mountpoint' svc:/system/filesystem/smartdc:default)"
usbcpy="$(svcprop -p 'joyentfs/usb_copy_path' svc:/system/filesystem/smartdc:default)"

. /lib/sdc/config.sh
load_sdc_config

if [[ -z $(mount | grep ^${usbmnt}) ]]; then
    echo "==> Mounting USB key"
    /usbkey/scripts/mount-usb.sh
    mounted="true"
fi

platform_type=smartos

# this should result in something like 20110318T170209Z
version=$(basename "${input}" .tgz | tr [:lower:] [:upper:] | sed -e "s/.*\-\(2.*Z\)$/\1/")
if [[ -n $(echo $(basename "${input}") | grep -i "HVM-${version}" 2>/dev/null) ]]; then
    version="HVM-${version}"
    platform_type=hvm
fi

if [[ ! -d ${usbmnt}/os/${version} ]]; then
    echo "==> Unpacking ${version} to ${usbcpy}/os"
    curl --progress -k ${input} -o ${usbcpy}/os/tmp.$$.tgz

    if [[ ! -f ${usbcpy}/os/tmp.$$.tgz ]]; then
       echo "File: '${input}' not found."
       usage
    fi
    
    mkdir -p ${usbmnt}/os/${version}
    (cd ${usbmnt}/os/${version} \
      && gzcat ${usbcpy}/os/tmp.$$.tgz | tar -xf - 2>/tmp/install_platform.log \
      && mv platform-* platform
    )
    rm -f ${usbcpy}/os/tmp.$$.tgz

    if [[ -f ${usbmnt}/os/${version}/platform/root.password ]]; then
         mv -f ${usbmnt}/os/${version}/platform/root.password \
             ${usbmnt}/private/root.password.${version}
    fi
fi

if [[ ! -d ${usbcpy}/os/${version} ]]; then
    echo "==> Copying ${version} to ${usbcpy}/os"
    mkdir -p ${usbcpy}/os
    (cd ${usbmnt}/os && rsync -a ${version}/ ${usbcpy}/os/${version})
fi

if [[ ${mounted} == "true" ]]; then
    echo "==> Unmounting USB Key"
    umount /mnt/usbkey
fi

echo "==> Adding to list of available platforms"

# Wait until MAPI is actually up. Attempts to guarantee that (watching the MAPI svc)
# before calling this script aren't reliable.<commit_msg>HEAD-924 Nitpick: (Customer raised) install-platform.sh should report unpacking platform to mounted USB not local usbkey copy - and good to note this can be slow<commit_after>#!/bin/bash
#
# Copyright (c) 2012, Joyent Inc., All rights reserved.
#

set -o errexit
set -o pipefail

function usage()
{
    echo "Usage: $0 <platform URI>"
    echo "(URI can be file:///, http://, anything curl supports or a filename)"
    exit 1
}

input=$1
if [[ -z ${input} ]]; then
    usage
fi

if echo "${input}" | grep "^[a-z]*://"; then
    # input is a url style pattern
    /bin/true
else
    if [[ -f ${input} ]]; then
       dir=$(cd $(dirname ${input}); pwd)
       file=$(basename ${input})
       input="file://${dir}/${file}"
    else
       echo "File: '${input}' not found."
       usage
    fi
fi

mounted="false"
usbmnt="/mnt/$(svcprop -p 'joyentfs/usb_mountpoint' svc:/system/filesystem/smartdc:default)"
usbcpy="$(svcprop -p 'joyentfs/usb_copy_path' svc:/system/filesystem/smartdc:default)"

. /lib/sdc/config.sh
load_sdc_config

if [[ -z $(mount | grep ^${usbmnt}) ]]; then
    echo "==> Mounting USB key"
    /usbkey/scripts/mount-usb.sh
    mounted="true"
fi

platform_type=smartos

# this should result in something like 20110318T170209Z
version=$(basename "${input}" .tgz | tr [:lower:] [:upper:] | sed -e "s/.*\-\(2.*Z\)$/\1/")
if [[ -n $(echo $(basename "${input}") | grep -i "HVM-${version}" 2>/dev/null) ]]; then
    version="HVM-${version}"
    platform_type=hvm
fi

if [[ ! -d ${usbmnt}/os/${version} ]]; then
    echo "==> Staging ${version}"
    curl --progress -k ${input} -o ${usbcpy}/os/tmp.$$.tgz

    if [[ ! -f ${usbcpy}/os/tmp.$$.tgz ]]; then
       echo "File: '${input}' not found."
       usage
    fi
    
    echo "==> Unpacking ${version} to ${usbmnt}/os"
    echo "==> This may take a while..."
    mkdir -p ${usbmnt}/os/${version}
    (cd ${usbmnt}/os/${version} \
      && gzcat ${usbcpy}/os/tmp.$$.tgz | tar -xf - 2>/tmp/install_platform.log \
      && mv platform-* platform
    )
    rm -f ${usbcpy}/os/tmp.$$.tgz

    if [[ -f ${usbmnt}/os/${version}/platform/root.password ]]; then
         mv -f ${usbmnt}/os/${version}/platform/root.password \
             ${usbmnt}/private/root.password.${version}
    fi
fi

if [[ ! -d ${usbcpy}/os/${version} ]]; then
    echo "==> Copying ${version} to ${usbcpy}/os"
    mkdir -p ${usbcpy}/os
    (cd ${usbmnt}/os && rsync -a ${version}/ ${usbcpy}/os/${version})
fi

if [[ ${mounted} == "true" ]]; then
    echo "==> Unmounting USB Key"
    umount /mnt/usbkey
fi

echo "==> Adding to list of available platforms"

# Wait until MAPI is actually up. Attempts to guarantee that (watching the MAPI svc)
# before calling this script aren't reliable.
{'fraction_non_alphanumeric': 0.19954040597472233, 'fraction_numerical': 0.010915358100344696, 'mean_word_length': 2.891952309985097, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '940280', 'n_tokens_mistral': 1938, 'n_tokens_neox': 1881, 'n_words': 512}