#!/bin/sh
#
# Copyright C 2002-2011 by Isilon Systems, LLC. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
# 
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# Inc.
# 
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems. 
#  
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc.  Legion is a trademark of Isilon Systems, Inc.  All other
# trademarks and registered trademarks are the property of their
# respective holders.

# PROVIDE: isi_first_update
# REQUIRE: isi_first_boot isi_hal_init

if [ -f /etc/ifs/isi_first_update_already_ran ]; then
    exit
fi

touch /etc/ifs/isi_first_update_already_ran
chmod 640 /etc/ifs/isi_first_update_already_ran

# Create a .ifsvar file in /ifs root filesystem, so no programs can
# accidently create a local .ifsvar directly and populate it.
touch /ifs/.ifsvar

# Recover any newer saved journal metadata from the old root
LKGMOUNT=/usr/update/root

if ! [ -d $LKGMOUNT ]; then
  /bin/mkdir -p $LKGMOUNT
fi

dataormissing() {
    if [ -f "$*" ]; then
        retvalue=`cat "$*"`
    else
        retvalue="No file"
    fi
}

bootdisk_type=`isi_bootdisk_type`
if [ ${bootdisk_type} != "gmirror" ]; then
  backup="/dev/imdd3a"
else
  backup="/dev/mirror/journal-backup"
fi
if /sbin/mount $backup $LKGMOUNT; then
  backupvalid=`/usr/bin/python -c "import isi.fs.journal as jlib; print jlib.FILES['backup_valid']"`
  backupserial=`/usr/bin/python -c "import isi.fs.journal as jlib; print jlib.FILES['backup_serial']"`
  if [ "X${backupvalid}X" = "XX" ]; then
      backupvalid=/etc/ifs/journal_backup_valid
  fi
  if [ "X${backupserial}X" = "XX" ]; then
      backupserial=/etc/ifs/journal_backup_serial
  fi
  # 
  # This is only important if there's a hardware fault requiring the
  # journal backup.  However, it's very dangerous to get this wrong.
  # As sanity checks to make sure we're doing this properly, we make
  # sure that:
  #   * the files have the same contents
  #   * the *valid file was written before the *serial file 
  #   * neither of the files on the current root are newer than the
  #     ones on the LKG root.
  #
  /usr/bin/cmp -s $LKGMOUNT/$backupserial $LKGMOUNT/$backupvalid
  # Another sanity check: *valid should have been written before
  # the journal backup, and *serial written after.
  if [ $? -eq 0 ] && \
     ! [ $LKGMOUNT/$backupserial -nt $LKGMOUNT/$backupvalid ] && \
     [ \( ! -f $backupserial \) -o \( $backupserial -ot $LKGMOUNT/$backupserial \) ] && \
     [ \( ! -f $backupvalid \) -o \( $backupvalid -ot $LKGMOUNT/$backupvalid \) ]; then
     dataormissing $backupvalid
     backupvaliddata=$retvalue
     dataormissing $backupserial
     backupserialdata=$retvalue
     # Somewhat redundant; we've already tested their existence
     dataormissing $LKGMOUNT/$backupvalid
     lkgbackupvaliddata=$retvalue
     dataormissing $LKGMOUNT/$backupserial
     lkgbackupserialdata=$retvalue
     /usr/bin/logger -i -p daemon.notice <<LOGGING
Recovering journal metadata from LKG
$backupvalid contents: $backupvaliddata
$backupserial contents: $backupserialdata
LKG $backupvalid contents: $lkgbackupvaliddata
LKG $backupserial contents: $lkgbackupserialdata
LOGGING
     /bin/cp -f $LKGMOUNT/$backupserial $backupserial
     /bin/cp -f $LKGMOUNT/$backupvalid $backupvalid
  fi
  /sbin/umount $backup
fi

if [ -f /etc/mcp/override/crontab.advquotas ]; then
    /bin/rm -f /etc/mcp/override/crontab.advquotas
fi

if [ -f /usr/sbin/isi_ssh_keys_cleanup ]; then
    /usr/sbin/isi_ssh_keys_cleanup
fi

# LCD is stopped and restarted to pick-up HW files.
/usr/bin/isi_lcd_d stop
/usr/bin/isi_lcd_d start

# Regenerate /etc/nsswitch.conf and /usr/local/etc/ldap.conf based on
# preserved LDAP settings.
/etc/mcp/scripts/generic_override.py /etc/mcp/sys/ldap.xml /etc/mcp/override/ldap.xml /usr/local/etc/ldap.conf
/etc/mcp/scripts/ldap.py /etc/mcp/sys/ldap.xml /etc/mcp/override/ldap.xml

# cleanup patch meta data
rm -f /+INSTALL
rm -f /+POST-INSTALL
rm -f /+REQUIRE
rm -f /+CONTENTS
rm -f /+COMMENT
rm -f /+DESC

if [ -f /etc/ifs/array.xml ]; then
    /usr/bin/python -c '
import isi.sys.version as ver
ns = ver.Node_Status()
ns.update_versions()
'
    # Flag to communicate to init_smartpools that this cluster was upgraded
    # to Habanero (as opposed to being newly configured at Habanero)
    /usr/bin/touch /etc/ifs/upgraded_to_habanero
    chmod 640 /etc/ifs/upgraded_to_habanero

    if [ -f /etc/mcp/scripts/templated_override.py ]; then
        /etc/mcp/scripts/templated_override.py /etc/mcp/templates/crontab /etc/mcp/override/crontab.advquotas /etc/local/crontab.local /etc/crontab
    fi
else
    /usr/bin/python -c '
import isi.sys.version as ver
ns = ver.Node_Status()
ns.write_current_version()
'
fi

# Bug 43322: Remove any existing /var/log/httpd/ui-ssl_request_log* files
/bin/rm -f /var/log/httpd/ui-ssl_request_log*

# bug 45924/30001: update any conf files (including sysctl and syslog confs)
if [ -f /usr/sbin/isi_mcp ]; then
    /usr/sbin/isi_mcp -u
fi

# Disable SyncIQ v1 service
isi_replicate_enabled=`expr "\`/usr/bin/isi services -a isi_replicate\`" : ".*enabled"`
if [ $isi_replicate_enabled -ne 0 ]; then
    /usr/bin/isi services -a isi_replicate disable
fi

# Create FTP User
pw usershow ftp
if [ $? -ne 0 ]; then
    /usr/sbin/pw user add -n ftp -u 14 -c "Anonymous Ftp" -d /ifs/home/ftp -g 5 -s /usr/sbin/nologin
    if [ $? -ne 0 ]; then
        /usr/bin/logger -i -p user.err "FTP user was not created successfully."
    fi
fi

# Delete API User & GROUP as it's being replace with a more meaningful
# InsightIQ user and group
API_USER=api
API_GROUP=api
API_UGID=15

if pw user show "${API_USER}" >/dev/null 2>&1; then

    echo "Deleting user \"${API_USER}\", (uid: ${API_UGID})."
    pw user del ${API_USER}
    if [ $? != 0 ]; then
        /usr/bin/logger -i -p user.err "Failed to delete API user & group \"${API_GROUP}\"."
    fi
fi

# Create Insightiq  User & GROUP
IIQ_USER=insightiq
IIQ_GROUP=insightiq
IIQ_UGID=15

if pw group show "${IIQ_GROUP}" >/dev/null 2>&1; then
    echo "Using existing group \"${IIQ_GROUP}\"."
else
    echo "Creating group \"${IIQ_GROUP}\", (gid: ${IIQ_UGID})."
    pw group add ${IIQ_GROUP} -g ${IIQ_UGID}
    if [ $? != 0 ]; then
        /usr/bin/logger -i -p user.err "Failed to add InsightIQ group \"${IIQ_GROUP}\"."
    fi
fi

if pw user show "${IIQ_USER}" >/dev/null 2>&1; then
    echo "Using existing user \"${IIQ_USER}\"."
else
    echo "Creating user \"${IIQ_USER}\", (uid: ${IIQ_UGID})."
    pw user add  -n ${IIQ_USER} -u ${IIQ_UGID} -g ${IIQ_GROUP} -h - -d /ifs/home/insightiq -s /sbin/nologin -c "InsightIQ User"
    if [ $? != 0 ]; then
        /usr/bin/logger -i -p user.err "Failed to add InsightIQ user \"${IIQ_USER}\"."
    fi
fi

# Create ganglia User & GROUP
GMOND_USER=ganglia
GMOND_GROUP=ganglia
GMOND_UGID=102

if pw group show "${GMOND_GROUP}" >/dev/null 2>&1; then
    echo "Using existing group \"${GMOND_GROUP}\"."
else
    echo "Creating group \"${GMOND_GROUP}\", (gid: ${GMOND_UGID})."
    pw group add ${GMOND_GROUP} -g ${GMOND_UGID}
    if [ $? != 0 ]; then
        /usr/bin/logger -i -p user.err "Failed to add ganglia group \"${GMOND_GROUP}\"."
    fi
fi

if pw user show "${GMOND_USER}" >/dev/null 2>&1; then
    echo "Using existing user \"${GMOND_USER}\"."
else
    echo "Creating user \"${GMOND_USER}\", (uid: ${GMOND_UGID})."
    pw user add  -n ${GMOND_USER} -u ${GMOND_UGID} -g ${GMOND_GROUP} -h - -d /nonexistent -s /sbin/nologin -c "Ganglia User"
    if [ $? != 0 ]; then
        /usr/bin/logger -i -p user.err "Failed to add ganglia user \"${GMOND_USER}\"."
    fi
fi

# Remove the old adcache used pre-65.
if [ -f /var/lib/likewise/db/lsass-adcache.db ]; then
    sqlite3 /var/lib/likewise/db/lsass-adcache.db .schema | grep "lwiusers6"
    if [ $? != 0 ]; then
        rm -f /var/lib/likewise/db/lsass-adcache.db
    fi
fi

# Delete Isilon For vCenter User & GROUP, we no longer need it for IFV
# and 7.0 is not going to support IFV when release
ISILONFORVCENTER_USER=vapi
ISILONFORVCENTER_GROUP=vapi
ISILONFORVCENTER_UGID=16

if pw user show "${ISILONFORVCENTER_USER}" >/dev/null 2>&1; then
    echo "Deleting user \"${ISILONFORVCENTER_USER}\""
    pw user del  -n ${ISILONFORVCENTER_USER} 
fi

if pw group show "${ISILONFORVCENTER_GROUP}" >/dev/null 2>&1; then
    echo "Deleting group \"${ISILONFORVCENTER_GROUP}\", (gid: ${ISILONFORVCENTER_UGID})."
    pw group del ${ISILONFORVCENTER_GROUP} -g ${ISILONFORVCENTER_UGID}
fi

# ConnectEMC requires these directories to function
mkdir -p /var/crash/connectemc/archive
mkdir -p /var/crash/connectemc/failed
mkdir -p /var/crash/connectemc/history
mkdir -p /var/crash/connectemc/output
mkdir -p /var/crash/connectemc/poll
mkdir -p /var/crash/connectemc/queue
mkdir -p /var/crash/connectemc/recycle
mkdir -p /var/crash/connectemc/tmp

# Setup the owner of the remote support shell
REMOTE_SUPPORT_USER=remotesupport
REMOTE_SUPPORT_UGID=12
REMOTE_SUPPORT_SHELL=/usr/bin/isi_remote_support_shell

if pw user show "${REMOTE_SUPPORT_USER}" >/dev/null 2>&1; then
    echo "Using existing user \"${REMOTE_SUPPORT_USER}\"."
else
    echo "Creating user \"${REMOTE_SUPPORT_USER}\", (uid: ${REMOTE_SUPPORT_UGID})."
    pw user add  -n ${REMOTE_SUPPORT_USER} -u ${REMOTE_SUPPORT_UGID} -g wheel -h - -d /usr/local/connectemc/remotesupport -s /usr/bin/isi_remote_support_shell -c "Remote Support User"
    if [ $? != 0 ]; then
        /usr/bin/logger -i -p user.err "Failed to add Remote Support user \"${REMOTE_SUPPORT_USER}\"."
    fi
fi

chown ${REMOTE_SUPPORT_UGID} ${REMOTE_SUPPORT_SHELL}
