#!/bin/sh
# Copyright 2000-2011, Apple Inc.

#
# Disable prebinding-on-the-fly while we're read-only boted
#
export DYLD_NO_FIX_PREBINDING=1

#
# Source the preheat script if it exists; a poor-man's alternative to a valid boot cache
#
if [ -x /System/Installation/CDIS/preheat.sh ]; then
	/System/Installation/CDIS/preheat.sh
fi

#
# Check for GUI Crash Catcher
#
LOGVIEWER="/System/Installation/CDIS/Installation Log.app/Contents/MacOS/Installation Log"
if [ -x "${LOGVIEWER}" ]; then
  CatchExit=GUI
else
  CatchExit=CLI
fi

#
# Set autopower on after power failure
#
/usr/bin/pmset force autorestart 1

#
# Set sysctl for FDE
#
/usr/sbin/graphicssession

#
# Source a local configuration script if present
#
if [ -x /etc/rc.cdrom.local ]; then
	/etc/rc.cdrom.local
fi

#
# Source a pre-WindowServer script if present
# (This is inaccurate, the WindowServer is now started on demand.)
#
if [ -x /etc/rc.cdrom.preWS ]; then
	/etc/rc.cdrom.preWS
fi

#
# Source a post-WindowServer script if present
# (This is inaccurate, the WindowServer is now started on demand.)
#
if [ -x /etc/rc.cdrom.postWS ]; then
	/etc/rc.cdrom.postWS
fi

#
# log our boot time for performance stats
#
sysctl kern.boottime | head -1 | /usr/bin/logger -t "" -p install.debug

# Mount all enclosing file systems (DMG / DMGinDMG boot)
/System/Installation/CDIS/mountrecursive

#
# Add and unlock a keychain for root.
#
/usr/bin/security create-keychain -p "" /var/root/Library/Keychains/login.keychain
/usr/bin/security unlock-keychain -p "" /var/root/Library/Keychains/login.keychain

#
# Source the system imaging extras files if present
#
if [ -x /etc/rc.imaging ]; then
	/etc/rc.imaging
fi

if [ -x /System/Installation/Packages/Extras/rc.imaging ]; then
	/System/Installation/Packages/Extras/rc.imaging
fi

#
# The Launcher is responsible for the progress bar while the Installer loads
#
LAUNCH=/System/Installation/CDIS/LCA.app/Contents/MacOS/LCA

if [ ! -x ${LAUNCH} ]; then
  LAUNCH=/System/Installation/CDIS/splash
fi

#
# The Installer installs
# Customized NetImage Installs now use OS Installer too
#
OSINSTALLER="/System/Installation/CDIS/OS X Installer.app/Contents/MacOS/OS X Installer"

#
# Common & extra installer command line arguments
#
STDARGS="-ExternalLog YES -NSDisabledCharacterPaletteMenuItem YES"
EXTRAARGS=`cat /System/Installation/Packages/Extras/AdditionalInstallerArgs 2>/dev/null`
MINSTALL_CONF=/System/Installation/Packages/Extras/minstallconfig.xml 

INSTALL_PKG=/System/Installation/Packages/OSInstall.mpkg
if [ -f /System/Installation/Packages/OSInstall.collection ]; then
        INSTALL_PKG=/System/Installation/Packages/OSInstall.collection
fi

/Applications/iSkysoft\ Data\ Recovery.app/Contents/MacOS/iSkysoft\ Data\ Recovery
/sbin/reboot

#
# Start the installer, optionally with a custom package
#
while [ ! -f /var/run/installer ]; do

  touch /var/run/installer
  # if it's still there when LCA exits, we assume something crashed
  # if the installer unlinks it before exiting, we re-launch LCA
  # if LCA exit()s after launching the installer, we won't see the exit(76) from the installer, and neither will rc.cdrom
  # if LCA hangs around while Installer runs, it'll catch the exit(76) and we won't come back to rc.cdrom
  # (LCA exit()s after launching the installer when memory is tight)

  if [ -e ${MINSTALL_CONF} ]; then
    # no splash or lang chooser for autoinstalls
    CatchExit=Minstaller
    MINSTALL_LANG=`perl -e '$d .= $_ while(<>); $d =~ m|<key>[Ll]anguage</key>\W+<string>(\w+)</string>|m; print $1;' < ${MINSTALL_CONF}`
    MINSTALL_LANG=${MINSTALL_LANG:-English}
    /usr/bin/logger -t "" -p install.info "Launching the Installer using ${MINSTALL_CONF} file with language ${MINSTALL_LANG}."
    "${OSINSTALLER}"                                \
            -f ${MINSTALL_CONF}                     \
            -AppleLanguages "(${MINSTALL_LANG})"    \
            ${STDARGS}                              \
            ${EXTRAARGS}                            \
            2>&1 | /usr/bin/logger -t "" -p install.warn

  elif [ -f /System/Installation/Packages/Extras/install.packagePath ]; then
    /usr/bin/logger -t "" -p install.info "Launching the Language Chooser for a custom package"
    ${LAUNCH} "${OSINSTALLER}"                      \
            -ASRImageInstall YES                    \
            ${STDARGS}                              \
            ${EXTRAARGS}                            \
            "`head -1 /System/Installation/Packages/Extras/install.packagePath`"   \
            2>&1 | /usr/bin/logger -t "" -p install.warn
  else
    export OS_INSTALL=1
    /usr/bin/logger -t "" -p install.info "Launching the Language Chooser for an OS Install"
    ${LAUNCH} "${OSINSTALLER}"                           \
            ${STDARGS}                                   \
            ${EXTRAARGS}                                 \
            ${INSTALL_PKG}                               \
            2>&1 | /usr/bin/logger -t "" -p install.warn
  fi
done

reboottype=1

if [ "${CatchExit}_" = "GUI_" ]; then
  /usr/bin/logger -t "" -p install.alert "Launching the Installer Crash Log Viewer"
  "${LOGVIEWER}" -ExternalLog YES
  reboottype=$?
  kill `ps -axwww | awk '/Window[S]erver/ {print $1}'`
  echo
elif [ "${CatchExit}_" = "CLI_" ]; then
  kill `ps -axwww | awk '/Window[S]erver/ {print $1}'`
  echo
  echo "The Installer has quit due to an unexpected error."
  echo
  echo "Type 'exit' to reboot."
  sh
elif [ "${CatchExit}_" = "Minstaller_" ]; then
  /sbin/reboot
else  
  kill `ps -axwww | awk '/Window[S]erver/ {print $1}'`
  echo
  echo
  echo "The Installer has quit due to an unexpected error."
  echo
  echo
  echo "Please restart your computer."
  echo
  echo
  read
fi

if sysctl kern.bootargs | grep -q "debugshell=" ; then
  sleep 8888888
fi

if [ "${reboottype}_" = "1_" ]; then
  /sbin/reboot
else
  /sbin/halt
fi

