#!/usr/bin/bash
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License").  You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at http://smartos.org/CDDL
#
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file.
#
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
# Copyright (c) 2013, Joyent, Inc. All rights reserved.
#

export PS4='[\D{%FT%TZ}] ${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'

set -o xtrace

# Use platform script instead, if it exists
[ -f /lib/svc/method/mdata-fetch ] && exec /lib/svc/method/mdata-fetch $@

#
# The rest is legacy for platforms that do not come with its own
#

. /lib/svc/share/smf_include.sh
smf_is_globalzone && exit ${SMF_EXIT_OK}

if [ ! -x /usr/sbin/mdata-get ]; then
  echo "Metadata mdata-get tool not found."
  exit ${SMF_EXIT_ERR_FATAL}
fi

timer=30

while [ $((timer--)) -gt 0 ]; do

  if [[ -e /.zonecontrol/metadata.sock ]] ||\
     [[ -e /var/run/smartdc/metadata.sock ]]; then
    /usr/sbin/mdata-get sdc:uuid
    result=$?
    if [[ ${result} -lt 10 ]]; then
      # exit 10 means problem talking to socket. We assume errors 10 or
      # higher (I've seen 141 from something in here getting SIGPIPE too)
      # is something we'll just retry on for now.  <10 we assume is a
      # problem retrying won't fix (eg. not found) or is success (0)
      break;
    fi
  fi

  # This is an alternate signal to metadata which we enable if the socket isn't
  # here yet in case something else in smf is messed up. (for debugging)
  [[ -e /tmp/.ready_for_metadata ]] || touch /tmp/.ready_for_metadata

  sleep 1
done

if [ ! -e /.zonecontrol/metadata.sock ] && \
   [ ! -e /var/run/smartdc/metadata.sock ]; then
  echo "No metadata socket file, skipping metadata loading."
  exit ${SMF_EXIT_OK}
fi

/usr/sbin/mdata-get sdc:uuid
result=$?
if [[ ${result} -gt 0 ]]; then
  # Assume this is SDC6
  legacy_platform=yes
fi

# Update sysinfo to ensure values that come from metadata are populated.
[ -z ${legacy_platform} ] && \
/usr/bin/sysinfo -fu

echo "Retrieving metadata user-data"
/usr/sbin/mdata-get user-data >/var/db/mdata-user-data.new
case $? in
  0)
    echo "Metadata user-data successfuly retrieved."
    mv /var/db/mdata-user-data{.new,}
    ;;
  1)
    echo "Metadata user-data not defined."
    rm -f /var/db/mdata-user-data{,.new}
    ;;
  *)
    echo "Metadata couldn't be retrieved."
    exit ${SMF_EXIT_ERR_FATAL}
    ;;
esac

echo "Retrieving metadata user-script..."
/usr/sbin/mdata-get user-script >/var/svc/mdata-user-script.new
case $? in
  0)
    echo "Metadata user-script successfuly retrieved."
    mv /var/svc/mdata-user-script{.new,}
    chmod +x /var/svc/mdata-user-script
    ;;
  1)
    echo "Metadata user-script not defined."
    rm -f /var/svc/mdata-user-script{,.new}
    ;;
  *)
    echo "Metadata couldn't be retrieved."
    exit ${SMF_EXIT_ERR_FATAL}
    ;;
esac

[ ${legacy_platform} ] || {

echo "Retrieving tmpfs value..."
tmpfs=$(/usr/sbin/mdata-get sdc:tmpfs)
if [[ $? == 0 && -n ${tmpfs} && -f /etc/vfstab ]]; then
  check="swap        -   /tmp                tmpfs";
  new="swap        -   /tmp                tmpfs    -   yes    size=${tmpfs}m";
  if ! /usr/bin/grep "^${new}" /etc/vfstab; then
    if ! /usr/bin/grep "^${check}" /etc/vfstab; then
      # no tmpfs line. add it.
      echo "${new}" >> /etc/vfstab
    else
      # existing tmpfs line, but wrong value. fix it.
      /usr/bin/sed -i "" -e "s|^swap.*/tmp.*tmpfs.*$|${new}|" /etc/vfstab
      echo $?
    fi
    # also fix current size, since /etc/vfstab didn't have our correct line
    /usr/sbin/mount -F tmpfs -o remount,size=${tmpfs}m /tmp
  fi
fi

# We use the special sdc:nics value here though this is not an interface for
# use elsewhere.  If this is changed please also update agent.js in the metadata
# agent.
#
# We run this every startup in case nics have changed since last boot. As
# network/physical has an optional_all dependency on this service, we'll have
# had our chance to write these files out before networking comes up. This
# might eventually be replaced by network/physical grabbing data directly.
echo "Retrieving nics data..."
while IFS="|" read -r iface ip netmask primary gateway; do

  # so it shows up in the logs
  echo "iface[${iface}] ip[${ip}] netmask[${netmask}]" \
    "primary[${primary}] gateway[${gateway}]"

  # if we don't have interface name, we don't know what files to write out.
  if [[ -z ${iface} ]]; then
    continue;
  fi

  if [[ ${ip} != "dhcp" && -n ${ip} && -n ${netmask} ]]; then
    echo "${ip} netmask ${netmask} up" > /etc/hostname.${iface}
  else
    rm -f /etc/hostname.${iface}
  fi

  # remove any existing dhcp.${iface} file, we'll create one if it belongs later
  rm -f /etc/dhcp.${iface}

  if [[ -n ${primary} && ${primary} == "true" ]]; then
    if [[ -n ${gateway} ]]; then
      echo "${gateway}" > /etc/defaultrouter
    fi
    if [[ ${ip} == "dhcp" ]]; then
      touch /etc/dhcp.${iface}
    fi
  fi

  # XXX we leave old hostname.netX files around and just replace when we have
  # one next.

done < <(/usr/sbin/mdata-get sdc:nics \
    | /usr/bin/json -d '|' -a interface ip netmask primary gateway)

}

# Unconditionally enable mdata:execute, so that the last provisioning step
# is always taken (regardless of whether user-script exists or not)
svcadm enable smartdc/mdata:execute

exit ${SMF_EXIT_OK}
