Setup NetBoot Service on Mac OS X 10.6.x Client - Setup

By: Richard Glaser - Revised: 2011-07-21 rcg

Index

Script

Here is the script to setup NetBoot Service on Mac OS X 10.6.x client...

#!/bin/bash
#
# config_netboot_server.sh
#
# Revised: July 5, 2011
# Revised by: Richard Glaser
#
# Copyright (c) 2011 University of Utah Student Computing Labs.
#
# All Rights Reserved.
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appears in all copies and
# that both that copyright notice and this permission notice appear
# in supporting documentation, and that the name of The University
# of Utah not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission. This software is supplied as is without expressed or
# implied warranties of any kind.

# Global Variables
########################################################################

NETBOOT_IMAGES_DIR_PATH="/Library/NetBoot/NetBootSP0" # Enter path to NetBoot images directory

# The variables used below are used to copy NetBoot images to client you want to setup NetBoot service.
# If you don't want to programatically copy NetBoot images to client, comment out the "download_netboot_image" sub-routine at the end of the script

NETBOOT_SERVER_MOUNT_PATH="" # Enter path to mount point for sharepoint with NetBoot images on client.
NETBOOT_IMAGES_SERVER_HOSTNAME="" # Enter server hostname for sharepoint with NetBoot images.
NETBOOT_IMAGES_SERVER_SHAREPOINT="" # Enter sharepoint name that has the NetBoot images stored.
NETBOOT_IMAGES_SERVER_USERNAME="" # Enter the username with access to sharepoint with NetBoot images.
NETBOOT_IMAGES_SERVER_PASSWORD="" # Enter the password to access to sharepoint with NetBoot images.
NETBOOT_IMAGE_NAME="" # Enter the NetBoot image name you want to download to client.

# Current date
CURRENT_DATE=`date +%Y.%m.%d`
PATH_TO_LOG_DIR=`mkdir -p /var/log/config_netboot_server`
PATH_TO_LOG_FILE="/var/log/config_netboot_server/${CURRENT_DATE}-config_netboot_server.log"

NETWORKSETUP_PATH=`/usr/sbin/networksetup`
TFTP_PATH="/System/Library/LaunchDaemons/tftp.plist"
BOOTPD_PATH="/etc/bootpd.plist"

#////////////////////////////////////////////////////////////////////////////////////////
#////////////////////////////////////////////////////////////////////////////////////////

run_as_root()
{

# Make sure only root can run our script
    if [ "$(id -u)" != "0" ]; then
    echo "This script must be run as root" 1>&2
    exit 1
fi

}

#////////////////////////////////////////////////////////////////////////////////////////
#////////////////////////////////////////////////////////////////////////////////////////

create_netboot_dirs_and_symlinks()
{

# Create Directories & Symlinks
########################################################################

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "#######################################################################################################" | tee -a "${PATH_TO_LOG_FILE}"
echo " Creating NetBoot Directories & Symbolic Links $(date)" | tee -a "${PATH_TO_LOG_FILE}"
echo "#######################################################################################################" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo " Time Started: $(date +"%r")" | tee -a "${PATH_TO_LOG_FILE}"
echo "=======================================================================================================" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

# Create NetBoot, "NetBootClients0" & "NetBootSP0" Directories
echo "# Create NetBoot, "NetBootClients0" & "NetBootSP0" Directories"
echo "mkdir /Library/NetBoot" | tee -a "${PATH_TO_LOG_FILE}"
mkdir /Library/NetBoot | tee -a "${PATH_TO_LOG_FILE}"

echo ""
echo "mkdir /Library/NetBoot/NetBootClients0" | tee -a "${PATH_TO_LOG_FILE}"
mkdir /Library/NetBoot/NetBootClients0 | tee -a "${PATH_TO_LOG_FILE}"

echo ""
echo "mkdir /Library/NetBoot/NetBootSP0" | tee -a "${PATH_TO_LOG_FILE}"
mkdir /Library/NetBoot/NetBootSP0 | tee -a "${PATH_TO_LOG_FILE}"

# Recursively, change owner to "root" & group "admin"
echo ""
echo "# Recursively, change owner to "root" & group "admin""
echo "chown -R root:admin /Library/NetBoot" | tee -a "${PATH_TO_LOG_FILE}"
chown -R root:admin /Library/NetBoot | tee -a "${PATH_TO_LOG_FILE}"

# Recursively, change permissions for owner & group to read, write & execute
# and others to read & execute NetBoot directory & sub-directories.
echo ""
echo "# Recursively, change permissions for owner & group to read, write & execute"
echo "# and others to read & execute NetBoot directory & sub-diretories."
echo "chmod -R 775 /Library/NetBoot" | tee -a "${PATH_TO_LOG_FILE}"
chmod -R 775 /Library/NetBoot | tee -a "${PATH_TO_LOG_FILE}"

# Create symlinks ".clients" & ".sharepoint"
#
# To make sure the symlinks are correctly do the following list command...
#
# ls -al
# total 16
# drwxrwxr-x 6 root admin 204 May 2 14:29 .
# drwxrwxr-t+ 60 root admin 2040 May 2 13:01 ..
# lrwxr-xr-x 1 root admin 15 May 2 14:29 .clients -> NetBootClients0
# lrwxr-xr-x 1 root admin 10 May 2 14:29 .sharepoint -> NetBootSP0
# drwxrwxr-x 3 root admin 102 May 2 14:28 NetBootClients0
# drwxrwxr-x 5 root admin 170 May 2 14:28 NetBootSP0

# Change directory to "/Library/NetBoot"
echo ""
echo "# Change directory to "/Library/NetBoot""
echo "cd /Library/NetBoot" | tee -a "${PATH_TO_LOG_FILE}"
cd /Library/NetBoot | tee -a "${PATH_TO_LOG_FILE}"

# Create symlink ".clients" from "NetBootClients0" in directory "/Library/NetBoot"
# Using the "ln" utility with options "-s", "-h" & "-f"
#
# The "-s" option creates a symbolic Links
# The "-h" option will not follow the target file/directory if it is a symbolic links
# The "-f" will unlink the target file if the symbolic link exists.

echo ""
echo "# Create symlink ".clients" from "NetBootClients0" in directory "/Library/NetBoot""
echo "ln -shf NetBootClients0 /Library/NetBoot/.clients" | tee -a "${PATH_TO_LOG_FILE}"
ln -shf NetBootClients0 /Library/NetBoot/.clients | tee -a "${PATH_TO_LOG_FILE}"

# Create symlink ".sharepoint" from "NetBootSP0" in directory "/Library/NetBoot"
echo ""
echo "# Create symlink ".clients" from "NetBootClients0" in directory "/Library/NetBoot""
echo "ln -shf NetBootSP0 /Library/NetBoot/.sharepoint" | tee -a "${PATH_TO_LOG_FILE}"
ln -shf NetBootSP0 /Library/NetBoot/.sharepoint | tee -a "${PATH_TO_LOG_FILE}"

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "=======================================================================================================" | tee -a "${PATH_TO_LOG_FILE}"
echo " Time Completed : $(date +"%r")" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

}

#////////////////////////////////////////////////////////////////////////////////////////
#////////////////////////////////////////////////////////////////////////////////////////

config_nfs_exports()
{

# Configure NFS (Network File System) Exports
########################################################################

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "#######################################################################################################" | tee -a "${PATH_TO_LOG_FILE}"
echo " Configure NFS (Network File System) Exports $(date)" | tee -a "${PATH_TO_LOG_FILE}"
echo "#######################################################################################################" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo " Time Started: $(date +"%r")" | tee -a "${PATH_TO_LOG_FILE}"
echo "=======================================================================================================" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

# Create NFS Export for "NetBootSP0"
echo "# Create NFS Export for "NetBootSP0""
echo "echo "/Library/NetBoot/NetBootSP0 -ro -maproot=root" > /etc/exports" | tee -a "${PATH_TO_LOG_FILE}"
echo "/Library/NetBoot/NetBootSP0 -ro -maproot=root" > /etc/exports | tee -a "${PATH_TO_LOG_FILE}"

# Create NFS Export for "NetBootClients0"
echo ""
echo "# Create NFS Export for "NetBootClients0""
echo "echo "/Library/NetBoot/NetBootClients0 -ro -maproot=root" >> /etc/exports" | tee -a "${PATH_TO_LOG_FILE}"
echo "/Library/NetBoot/NetBootClients0 -ro -maproot=root" >> /etc/exports | tee -a "${PATH_TO_LOG_FILE}"

# -ro option
#
# The directory is shared read only; the client machine will not be able to write it. This is the default.

# -maproot=root option
#
# The -maproot=root option allows the root user on the remote system to write
# data on the exported file system as root. If the -maproot=root flag is not
# specified, then even if a user has root access on the remote system, they
# will not be able to modify files on the exported file system.

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "=======================================================================================================" | tee -a "${PATH_TO_LOG_FILE}"
echo " Time Completed : $(date +"%r")" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

}

#////////////////////////////////////////////////////////////////////////////////////////
#////////////////////////////////////////////////////////////////////////////////////////

config_tftp()
{

# Configure TFTP (Trival File Transfer Protocol) Service
########################################################################

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "#######################################################################################################" | tee -a "${PATH_TO_LOG_FILE}"
echo " Configure TFTP (Trival File Transfer Protocol) Service $(date)" | tee -a "${PATH_TO_LOG_FILE}"
echo "#######################################################################################################" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo " Time Started: $(date +"%r")" | tee -a "${PATH_TO_LOG_FILE}"
echo "=======================================================================================================" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

# tftpd - Info
#
# tftpd is a server which supports the DARPA Trivial File Transfer
# Protocol. This server should not be started manually; instead, it
# should be run using launchd using the plist
# /System/Library/LaunchDaemons/tftp.plist. It may be started
# using the launchctl load command.
#
# The use of tftp(1) does not require an account or password on the
# remote system. Due to the lack of authentication information,
# tftpd will allow only publicly readable files to be accessed.
# Filenames beginning in ``../'' or containing ``/../'' are not
# allowed. Files may be written to only if they already exist and
# are publicly writable.

# Create NetBoot directory in "/private/tftpboot" directory
echo "# Create NetBoot directory in "/private/tftpboot" directory" | tee -a "${PATH_TO_LOG_FILE}"
echo "mkdir /private/tftpboot/NetBoot" | tee -a "${PATH_TO_LOG_FILE}"
mkdir /private/tftpboot/NetBoot | tee -a "${PATH_TO_LOG_FILE}"

# Create symlink "NetBootSP0" from source "/Library/NetBoot/NetBootSP0"
#
# To make sure the symlinks are correctly do the following list command...
#
# ls -al
# total 8
# drwxr-xr-x 3 root wheel 102 May 2 10:08 .
# drwxr-xr-x 3 root wheel 102 May 2 09:24 ..
# lrwxr-xr-x 1 root wheel 27 May 2 10:08 NetBootSP0 -> /Library/NetBoot/NetBootSP0

# Created symlink "NetBootSP0" from "/Library/NetBoot/NetBootSP0" in directory "/private/tftpboot"
# Using the "ln" utility with options "-s", "-h" & "-f"
#
# The "-s" option creates a symbolic Links
# The "-h" option will not follow the target file/directory if it is a symbolic links
# The "-f" will unlink the target file if the symbolic link exists.

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "# Created symlink "NetBootSP0" from "/Library/NetBoot/NetBootSP0" in directory "/private/tftpboot"" | tee -a "${PATH_TO_LOG_FILE}"
echo "ln -shf /Library/NetBoot/NetBootSP0 /private/tftpboot/NetBoot/NetBootSP0" | tee -a "${PATH_TO_LOG_FILE}"
ln -shf /Library/NetBoot/NetBootSP0 /private/tftpboot/NetBoot/NetBootSP0 | tee -a "${PATH_TO_LOG_FILE}"

# /System/Library/LaunchDaemons/tftp.plist setup
########################################################################

# Example - /System/Library/LaunchDaemons/tftp.plist file
#
# <?xml version="1.0" encoding="UTF-8"?>
# <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
# <plist version="1.0">
# <dict>
#     <key>Disabled</key>
#     <true/>
#     <key>Label</key>
#     <string>com.apple.tftpd</string>
#     <key>ProgramArguments</key>
#     <array>
#         <string>/usr/libexec/tftpd</string>
#         <string>-i</string>
#         <string>/private/tftpboot</string>
#     </array>
#     <key>inetdCompatibility</key>
#     <dict>
#         <key>Wait</key>
#         <true/>
#     </dict>
#     <key>InitGroups</key>
#     <true/>
#     <key>Sockets</key>
#     <dict>
#         <key>Listeners</key>
#         <dict>
#             <key>SockServiceName</key>
#             <string>tftp</string>
#             <key>SockType</key>
#             <string>dgram</string>
#         </dict>
#     </dict>
# </dict>
# </plist>

echo ""
echo "# Find string "-s" and replace with string "-i"
"
# Find string "-s" and replace with string "-i"
# The "-s" option enables tftpd to chroot to the /tftpboot directory restricting access to outside files/directories
# The "-i" option enables insecure mode which allows access to files outside the /private/tftpboot directory
# This is necessary for NetBoot to allow access to the images located in directory /Library/NetBoot/NetBootSP0

FIND="<string>-s</string>"
REPLACE="<string>-i</string>"

echo "sed -i '.bak' "s%$FIND%$REPLACE%g" "${TFTP_PATH}"" | tee -a "${PATH_TO_LOG_FILE}"
sed -i '.bak' "s%$FIND%$REPLACE%g" "${TFTP_PATH}" | tee -a "${PATH_TO_LOG_FILE}"

# launchctl interfaces with launchd to load, unload daemons/agents and generally control launchd

# Use launchctl to unload the tftp service
echo ""
echo "# launchctl interfaces with launchd to load, unload daemons/agents and generally control launchd"
echo "# Use launchctl to unload the tftp service"
echo "# "-w" option overrides the "Disabled" key and sets it to false"

echo "launchctl unload -w "${TFTP_PATH}"" | tee -a "${PATH_TO_LOG_FILE}"
launchctl unload -w "${TFTP_PATH}" | tee -a "${PATH_TO_LOG_FILE}"

# Use launchctl to load the tftp service
echo ""
echo "# launchctl interfaces with launchd to load, unload daemons/agents and generally control launchd"
echo "# Use launchctl to load the tftp service"

echo "launchctl load -w "${TFTP_PATH}"" | tee -a "${PATH_TO_LOG_FILE}"
launchctl load -w "${TFTP_PATH}" | tee -a "${PATH_TO_LOG_FILE}"

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "=======================================================================================================" | tee -a "${PATH_TO_LOG_FILE}"
echo " Time Completed : $(date +"%r")" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

}

#////////////////////////////////////////////////////////////////////////////////////////
#////////////////////////////////////////////////////////////////////////////////////////

download_netboot_image()
{

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "#######################################################################################################" | tee -a "${PATH_TO_LOG_FILE}"
echo " Download NetBoot Image $(date)" | tee -a "${PATH_TO_LOG_FILE}"
echo "#######################################################################################################" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo " Time Started: $(date +"%r")" | tee -a "${PATH_TO_LOG_FILE}"
echo "=======================================================================================================" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

# Make directory for sharepoint mount path
echo "# Make directory for sharepoint mount path"
echo "mkdir "$NETBOOT_SERVER_MOUNT_PATH"" | tee -a "${PATH_TO_LOG_FILE}"
mkdir "$NETBOOT_SERVER_MOUNT_PATH" | tee -a "${PATH_TO_LOG_FILE}"

# Mount AFP sharepoint
echo ""
echo "# Mount AFP sharepoint"
echo "mount_afp "afp://$NETBOOT_IMAGES_SERVER_USERNAME:$NETBOOT_IMAGES_SERVER_PASSWORD@$NETBOOT_IMAGES_SERVER_HOSTNAME$NETBOOT_IMAGES_SERVER_SHAREPOINT" "$NETBOOT_SERVER_MOUNT_PATH"" | tee -a "${PATH_TO_LOG_FILE}"
mount_afp "afp://$NETBOOT_IMAGES_SERVER_USERNAME:$NETBOOT_IMAGES_SERVER_PASSWORD@$NETBOOT_IMAGES_SERVER_HOSTNAME$NETBOOT_IMAGES_SERVER_SHAREPOINT" "$NETBOOT_SERVER_MOUNT_PATH" | tee -a "${PATH_TO_LOG_FILE}"

# Copy the NetBoot image to the local NetBoot image directory /Library/NetBoot/NetBootSP0
echo ""
echo "# Copy the server NetBoot image to the local NetBoot image directory /Library/NetBoot/NetBootSP0"
echo "/usr/local/bin/rsync -acv --progress --delete --ignore-existing "$NETBOOT_SERVER_MOUNT_PATH/$NETBOOT_IMAGE_NAME" "$NETBOOT_IMAGES_DIR_PATH/"" | tee -a "${PATH_TO_LOG_FILE}"
/usr/local/bin/rsync -acv --progress --delete --ignore-existing "$NETBOOT_SERVER_MOUNT_PATH/$NETBOOT_IMAGE_NAME" "$NETBOOT_IMAGES_DIR_PATH/" | tee -a "${PATH_TO_LOG_FILE}"

# Unmount file system
echo ""
echo "# Unmount file system"
echo "umount "$NETBOOT_SERVER_MOUNT_PATH"" | tee -a "${PATH_TO_LOG_FILE}"
umount "$NETBOOT_SERVER_MOUNT_PATH" | tee -a "${PATH_TO_LOG_FILE}"

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "=======================================================================================================" | tee -a "${PATH_TO_LOG_FILE}"
echo " Time Completed : $(date +"%r")" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

}

#////////////////////////////////////////////////////////////////////////////////////////
#////////////////////////////////////////////////////////////////////////////////////////

config_bootpd_template()
{

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "#######################################################################################################" | tee -a "${PATH_TO_LOG_FILE}"
echo " Setup bootpd.plist Template $(date)" | tee -a "${PATH_TO_LOG_FILE}"
echo "#######################################################################################################" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo " Time Started: $(date +"%r")" | tee -a "${PATH_TO_LOG_FILE}"
echo "=======================================================================================================" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

# /etc/bootpd.plist setup
########################################################################
#

# Use cat and a here file to create /etc/bootpd.plist with template data.
echo "# Use cat and a here file to create /etc/bootpd.plist with template data."
echo ""

(
cat <<'EOF'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>NetBoot</key>
    <dict/>
    <key>Subnets</key>
    <array>
        <dict>
            <key>allocate</key>
            <false/>
            <key>dhcp_domain_name</key>
            <string>DHCP_DOMAIN_NAME</string>
            <key>dhcp_domain_name_server</key>
            <array>
                <string>DHCP_DOMAIN_NAME_SERVER_0</string>
                <string>DHCP_DOMAIN_NAME_SERVER_1</string>
            </array>
            <key>dhcp_domain_search</key>
            <array>
                <string>DHCP_DOMAIN_SEARCH</string>
            </array>
            <key>dhcp_router</key>
            <string>DHCP_ROUTER</string>
            <key>lease_max</key>
            <integer>3600</integer>
            <key>name</key>
            <string>NAME</string>
            <key>net_address</key>
            <string>NET_ADDRESS</string>
            <key>net_mask</key>
            <string>NET_MASK</string>
            <key>net_range</key>
            <array>
                <string>NET_RANGE_START</string>
                <string>NET_RANGE_END</string>
            </array>
            <key>selected_port_name</key>
            <string>en0</string>
            <key>uuid</key>
            <string>UUID</string>
        </dict>
    </array>
    <key>allow</key>
    <array/>
    <key>bootp_enabled</key>
    <false/>
    <key>deny</key>
    <array/>
    <key>detect_other_dhcp_server</key>
    <false/>
    <key>dhcp_enabled</key>
    <false/>
    <key>netboot_enabled</key>
    <array>
        <string>en0</string>
    </array>
    <key>old_netboot_enabled</key>
    <false/>
    <key>relay_enabled</key>
    <false/>
    <key>relay_ip_list</key>
    <array/>
    <key>startTime</key>
    <string>START_TIME</string>
</dict>
</plist>
EOF
) > "${BOOTPD_PATH}"

cat "${BOOTPD_PATH}" | tee -a

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "=======================================================================================================" | tee -a "${PATH_TO_LOG_FILE}"
echo " Time Completed : $(date +"%r")" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

}

#////////////////////////////////////////////////////////////////////////////////////////
#////////////////////////////////////////////////////////////////////////////////////////

modify_bootpd_template_items()
{

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "#######################################################################################################" | tee -a "${PATH_TO_LOG_FILE}"
echo " Modifying bootpd.plist Template Items $(date)" | tee -a "${PATH_TO_LOG_FILE}"
echo "#######################################################################################################" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo " Time Started: $(date +"%r")" | tee -a "${PATH_TO_LOG_FILE}"
echo "=======================================================================================================" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

#####

echo "# Get Ethernet Interface" | tee -a "${PATH_TO_LOG_FILE}"
# Get Ethernet Interface

TEMP_NETWORK_INTERFACE=$(/usr/sbin/networksetup -listallnetworkservices | grep "Ethernet 1")

if [[ "$TEMP_NETWORK_INTERFACE" == "" ]]; then

    DEFAULT_NETWORK_INTERFACE="Ethernet"

else

    DEFAULT_NETWORK_INTERFACE="Ethernet 1"

fi

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "Default Network Interface is: " $DEFAULT_NETWORK_INTERFACE | tee -a "${PATH_TO_LOG_FILE}"

#####

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "#####" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

echo "# DHCP Domain Name" | tee -a "${PATH_TO_LOG_FILE}"
# DHCP Domain Name

DHCP_DOMAIN_NAME=`hostname | awk -F "." '{print $2"."$3"."$4}'`
echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "Domain Name is: "$DHCP_DOMAIN_NAME | tee -a "${PATH_TO_LOG_FILE}"

FIND="<string>DHCP_DOMAIN_NAME</string>"
REPLACE="<string>$DHCP_DOMAIN_NAME</string>"

echo "sed -i '.bak' "s%$FIND%$REPLACE%g" $BOOTPD_PATH" | tee -a "${PATH_TO_LOG_FILE}"
sed -i '.bak' "s%$FIND%$REPLACE%g" $BOOTPD_PATH | tee -a "${PATH_TO_LOG_FILE}"

#####

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "#####" | tee -a "${PATH_TO_LOG_FILE}"

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "# DHCP Name Server(s)" | tee -a "${PATH_TO_LOG_FILE}"
# DHCP Name Server(s)
DHCP_DOMAIN_NAME_SERVER=(`networksetup -getdnsservers "${DEFAULT_NETWORK_INTERFACE}" | tr '\n' ' '`)

echo "DHCP_DOMAIN_NAME_SERVER[0] : " ${DHCP_DOMAIN_NAME_SERVER[0]}
echo "DHCP_DOMAIN_NAME_SERVER[1] : " ${DHCP_DOMAIN_NAME_SERVER[1]}

INDEX=0
LOOP_NUM=1

for i in "${DHCP_DOMAIN_NAME_SERVER[@]}"; do

    FIND="<string>DHCP_DOMAIN_NAME_SERVER_$INDEX</string>"
    REPLACE="<string>${DHCP_DOMAIN_NAME_SERVER[$INDEX]}</string>"

    echo "" | tee -a "${PATH_TO_LOG_FILE}"
    echo "Name Server - Loop #$LOOP_NUM"| tee -a "${PATH_TO_LOG_FILE}"
    echo "=======================================================================================================" | tee -a "${PATH_TO_LOG_FILE}"
    echo "FIND is: " $FIND | tee -a "${PATH_TO_LOG_FILE}"
    echo "Replace is: " $REPLACE | tee -a "${PATH_TO_LOG_FILE}"
    echo "" | tee -a "${PATH_TO_LOG_FILE}"

    echo "sed -i '.bak' "s%$FIND%$REPLACE%g" $BOOTPD_PATH" | tee -a "${PATH_TO_LOG_FILE}"
    sed -i '.bak' "s%$FIND%$REPLACE%g" $BOOTPD_PATH | tee -a "${PATH_TO_LOG_FILE}"

    INDEX=$(( $INDEX + 1 ))
    LOOP_NUM=$(( $LOOP_NUM + 1 ))

done

#####

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "#####" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

# DHCP Search Domains
echo "# DHCP Search Domains" | tee -a "${PATH_TO_LOG_FILE}"

TEMP_DHCP_DOMAIN_SEARCH=`networksetup -getsearchdomains "${DEFAULT_NETWORK_INTERFACE}"`

if [ "${TEMP_DHCP_DOMAIN_SEARCH}" == "There aren't any Search Domains set on ${DEFAULT_NETWORK_INTERFACE}." ] ; then

    DHCP_DOMAIN_SEARCH=""

fi

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "DHCP_DOMAIN_SEARCH is: " $DHCP_DOMAIN_SEARCH | tee -a "${PATH_TO_LOG_FILE}"

if [ "${DHCP_DOMAIN_SEARCH}" == "" ] ; then

    # No domain search found - remove template string
    echo "" | tee -a "${PATH_TO_LOG_FILE}" | tee -a "${PATH_TO_LOG_FILE}"
    echo "# No domain search found - remove template string" | tee -a "${PATH_TO_LOG_FILE}"

    FIND="<string>DHCP_DOMAIN_SEARCH</string>"
    REPLACE="<string></string>"

    echo "sed -i '.bak' "s%$FIND%$REPLACE%g" $BOOTPD_PATH" | tee -a "${PATH_TO_LOG_FILE}"
    sed -i '.bak' "s%$FIND%$REPLACE%g" $BOOTPD_PATH | tee -a "${PATH_TO_LOG_FILE}"

else

    # Domain search found - update template string
    echo "" | tee -a "${PATH_TO_LOG_FILE}" | tee -a "${PATH_TO_LOG_FILE}"
    echo "# Domain search found - update template string" | tee -a "${PATH_TO_LOG_FILE}"

    FIND="<string>DHCP_DOMAIN_SEARCH</string>"
    REPLACE="<string>$DHCP_DOMAIN_SEARCH</string>"

    echo "sed -i '.bak' "s%$FIND%$REPLACE%g" $BOOTPD_PATH" | tee -a "${PATH_TO_LOG_FILE}"
    sed -i '.bak' "s%$FIND%$REPLACE%g" $BOOTPD_PATH | tee -a "${PATH_TO_LOG_FILE}"

fi

#####

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "#####" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

# DHCP Router
echo "# DHCP Router" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

DHCP_ROUTER=`networksetup -getinfo "${DEFAULT_NETWORK_INTERFACE}" | grep '^Router:' | sed 's/Router:\ //g'`

echo "DHCP Router is: " $DHCP_ROUTER | tee -a "${PATH_TO_LOG_FILE}"

FIND="<string>DHCP_ROUTER</string>"
REPLACE="<string>$DHCP_ROUTER</string>"

echo "sed -i '.bak' "s%$FIND%$REPLACE%g" $BOOTPD_PATH" | tee -a "${PATH_TO_LOG_FILE}"
sed -i '.bak' "s%$FIND%$REPLACE%g" $BOOTPD_PATH | tee -a "${PATH_TO_LOG_FILE}"

#####

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "#####" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

# Network address
echo "# Network address" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

TEMP_NET_ADDRESS=`networksetup -getinfo "${DEFAULT_NETWORK_INTERFACE}" | grep "^IP address" | sed 's/IP address: //g' | awk -F "." '{print $1"."$2"."$3}'`
NET_ADDRESS="${TEMP_NET_ADDRESS}.0"

echo "Network Address is: " $NET_ADDRESS | tee -a "${PATH_TO_LOG_FILE}"

FIND="<string>NET_ADDRESS</string>"
REPLACE="<string>$NET_ADDRESS</string>"

echo "sed -i '.bak' "s%$FIND%$REPLACE%g" $BOOTPD_PATH" | tee -a "${PATH_TO_LOG_FILE}"
sed -i '.bak' "s%$FIND%$REPLACE%g" $BOOTPD_PATH | tee -a "${PATH_TO_LOG_FILE}"

#####

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "#####" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

# Network Mask
echo "# Network Mask" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

NET_MASK=`networksetup -getinfo "${DEFAULT_NETWORK_INTERFACE}" | grep '^Subnet mask:' | sed 's/Subnet mask: //g'`

echo "Network Mask is: " $NET_MASK | tee -a "${PATH_TO_LOG_FILE}"

FIND="<string>NET_MASK</string>"
REPLACE="<string>$NET_MASK</string>"

echo "sed -i '.bak' "s%$FIND%$REPLACE%g" $BOOTPD_PATH" | tee -a "${PATH_TO_LOG_FILE}"
sed -i '.bak' "s%$FIND%$REPLACE%g" $BOOTPD_PATH | tee -a "${PATH_TO_LOG_FILE}"

#####

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "#####" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

# Network Name
echo "# Network Name" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

NUMBER_OF_BITS_IN_NETWORK_MASK=`mask2cidr $NET_MASK`

NAME="$NET_ADDRESS/$NUMBER_OF_BITS_IN_NETWORK_MASK $DEFAULT_NETWORK_INTERFACE"

echo "Network Names is: " $NAME | tee -a "${PATH_TO_LOG_FILE}"

FIND="<string>NAME</string>"
REPLACE="<string>$NAME</string>"

echo "sed -i '.bak' "s%$FIND%$REPLACE%g" $BOOTPD_PATH" | tee -a "${PATH_TO_LOG_FILE}"
sed -i '.bak' "s%$FIND%$REPLACE%g" $BOOTPD_PATH | tee -a "${PATH_TO_LOG_FILE}"

#####

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "#####" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

# Network Range
echo "# Network Range" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

MID_RANGE_OCTET=`echo "${NET_MASK}" | awk -F "." '{print $4}'`

SUBNET_IP_RANGE=`echo $[256-$MID_RANGE_OCTET]`

NUMBER_OF_SUBNETS=$(echo "256 / $SUBNET_IP_RANGE" | bc -l | awk -F "." '{print $1}')

IP_ADDRESS=`networksetup -getinfo "${DEFAULT_NETWORK_INTERFACE}" | grep "^IP address" | sed 's/IP address: //g'`

IP_ADDRESS_LAST_OCTET=`echo $IP_ADDRESS | awk -F "." '{print $4}'`

SUBNET=`networksetup -getinfo "${DEFAULT_NETWORK_INTERFACE}" | grep "^IP address" | sed 's/IP address: //g' | awk -F "." '{print $1"."$2"."$3}'`

NET_RANGE_START_OCTET=0

for (( i=1; i<=$NUMBER_OF_SUBNETS; i++ )); do

    NET_RANGE_END_OCTET=`echo $(($SUBNET_IP_RANGE * $i - 1))`

    if (("$NET_RANGE_END_OCTET" >= "$IP_ADDRESS_LAST_OCTET")); then

        echo "" | tee -a "${PATH_TO_LOG_FILE}"
        echo "Subnet Starting IP is: $SUBNET.$NET_RANGE_START_OCTET" | tee -a "${PATH_TO_LOG_FILE}"
        NET_RANGE_START="$SUBNET.$NET_RANGE_START_OCTET"

        FIND="<string>NET_RANGE_START</string>"
        REPLACE="<string>$NET_RANGE_START</string>"

        echo "sed -i '.bak' "s%$FIND%$REPLACE%g" $BOOTPD_PATH" | tee -a "${PATH_TO_LOG_FILE}"
        sed -i '.bak' "s%$FIND%$REPLACE%g" $BOOTPD_PATH | tee -a "${PATH_TO_LOG_FILE}"
        echo "" | tee -a "${PATH_TO_LOG_FILE}"

        echo "Subnet Ending IP is: $SUBNET.$NET_RANGE_END_OCTET" | tee -a "${PATH_TO_LOG_FILE}"
        NET_RANGE_END="$SUBNET.$NET_RANGE_END_OCTET"

        FIND="<string>NET_RANGE_END</string>"
        REPLACE="<string>$NET_RANGE_END</string>"

        echo "sed -i '.bak' "s%$FIND%$REPLACE%g" $BOOTPD_PATH" | tee -a "${PATH_TO_LOG_FILE}"
        sed -i '.bak' "s%$FIND%$REPLACE%g" $BOOTPD_PATH | tee -a "${PATH_TO_LOG_FILE}"

    fi

    NET_RANGE_END_OCTET=`echo $(($SUBNET_IP_RANGE * $i))`

done

#####

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "#####" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

# Network Interface UUID
echo "# Network Interface UUID" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

UUID=`uuidgen`

echo "Network Interface UUID: " $UUID | tee -a "${PATH_TO_LOG_FILE}"

FIND="<string>UUID</string>"
REPLACE="<string>$UUID</string>"

echo "sed -i '.bak' "s%$FIND%$REPLACE%g" $BOOTPD_PATH" | tee -a "${PATH_TO_LOG_FILE}"
sed -i '.bak' "s%$FIND%$REPLACE%g" $BOOTPD_PATH | tee -a "${PATH_TO_LOG_FILE}"

#####

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "#####" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

# bootps.plist Modification Time
echo "# Start Time" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

START_TIME=`date '+%F %H:%M:%S %z'`

echo "Configuration Start Time: " $START_TIME | tee -a "${PATH_TO_LOG_FILE}"

FIND="<string>START_TIME</string>"
REPLACE="<string>$START_TIME</string>"

echo "sed -i '.bak' "s%$FIND%$REPLACE%g" $BOOTPD_PATH" | tee -a "${PATH_TO_LOG_FILE}"
sed -i '.bak' "s%$FIND%$REPLACE%g" $BOOTPD_PATH | tee -a "${PATH_TO_LOG_FILE}"

#####

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "#####" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

# launchctl interfaces with launchd to load, unload daemons/agents and generally control launchd

# Use launchctl to unload the bootps service
echo "# Use launchctl to unload the bootpd service" | tee -a "${PATH_TO_LOG_FILE}"
launchctl unload -w /System/Library/LaunchDaemons/bootps.plist | tee -a "${PATH_TO_LOG_FILE}"
echo "launchctl unload -w /System/Library/LaunchDaemons/bootps.plist" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

# Use launchctl to load the bootpd service
echo "# Use launchctl to load the bootpd service" | tee -a "${PATH_TO_LOG_FILE}"
launchctl load -w /System/Library/LaunchDaemons/bootps.plist | tee -a "${PATH_TO_LOG_FILE}"
echo "launchctl load -w /System/Library/LaunchDaemons/bootps.plist" | tee -a "${PATH_TO_LOG_FILE}"

# /System/Library/LaunchDaemons/bootps.plist
########################################################################

# Example - /System/Library/LaunchDaemons/bootps.plist file
#
# <?xml version="1.0" encoding="UTF-8"?>
# <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
# <plist version="1.0">
# <dict>
#     <key>Disabled</key>
#     <true/>
#     <key>Label</key>
#     <string>com.apple.bootpd</string>
#     <key>ProgramArguments</key>
#     <array>
#         <string>/usr/libexec/bootpd</string>
#         <string>-v</string>
#     </array>
#     <key>Sockets</key>
#     <dict>
#         <key>Listeners</key>
#         <dict>
#             <key>SockFamily</key>
#             <string>IPv4</string>
#             <key>SockServiceName</key>
#             <string>bootps</string>
#             <key>SockType</key>
#             <string>dgram</string>
#         </dict>
#     </dict>
#     <key>inetdCompatibility</key>
#     <dict>
#         <key>Wait</key>
#         <true/>
#     </dict>
# </dict>
# </plist>

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "=======================================================================================================" | tee -a "${PATH_TO_LOG_FILE}"
echo " Time Completed : $(date +"%r")" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

}

#////////////////////////////////////////////////////////////////////////////////////////
#////////////////////////////////////////////////////////////////////////////////////////

mask2cidr()
{
    nbits=0
    IFS=.
    for dec in $1 ; do
         case $dec in
              255) let nbits+=8;;
              254) let nbits+=7;;
              252) let nbits+=6;;
              248) let nbits+=5;;
              240) let nbits+=4;;
              224) let nbits+=3;;
              192) let nbits+=2;;
              128) let nbits+=1;;
              0);;
              *) echo "Error: $dec is not recognised"; exit 1
         esac
    done
    echo "$nbits"
}

#////////////////////////////////////////////////////////////////////////////////////////
#////////////////////////////////////////////////////////////////////////////////////////

config_afp()
{

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "#######################################################################################################" | tee -a "${PATH_TO_LOG_FILE}"
echo " Setting Up AFP Sharepoint & Enabling File Sharing $(date)" | tee -a "${PATH_TO_LOG_FILE}"
echo "#######################################################################################################" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo " Time Started: $(date +"%r")" | tee -a "${PATH_TO_LOG_FILE}"
echo "=======================================================================================================" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

# To get the values you can turn on File Sharing using GUI System Preferences -> Sharing
# Then use Directory Service command line utility [dscl] to read the sharepoints values needed
#
# dscl . -read SharePoints/NetBootClient0
# dsAttrTypeNative:afp_guestaccess: 1
# dsAttrTypeNative:afp_name: NetBootClient0
# dsAttrTypeNative:afp_shared: 1
# dsAttrTypeNative:directory_path: /Library/NetBoot/NetBootClient0
# dsAttrTypeNative:ftp_name: NetBootClient0
# dsAttrTypeNative:sharepoint_group_id: DD8A7526-901D-4D31-8B90-8EC8B71A1B43
# dsAttrTypeNative:smb_createmask: 644
# dsAttrTypeNative:smb_directorymask: 755
# dsAttrTypeNative:smb_guestaccess: 1
# dsAttrTypeNative:smb_name: NetBootClient0
# dsAttrTypeNative:smb_shared: 1
# AppleMetaNodeLocation: /Local/Default
# RecordName: NetBootClient0
# RecordType: dsRecTypeStandard:SharePoints

# Create items needed for NetBoot sharepoint /Libary/NetBoot/NetBootClients0
#
# Gets stored in directory:
#
# /private/var/db/dslocal/nodes/Default/config/SharePoints/NetBootClients0.plist

echo "# Create items needed for NetBoot sharepoing /Libary/NetBoot/NetBootClients0" | tee -a "${PATH_TO_LOG_FILE}"
echo "#" | tee -a "${PATH_TO_LOG_FILE}"
echo "# Gets stored in directory:" | tee -a "${PATH_TO_LOG_FILE}"
echo "#" | tee -a "${PATH_TO_LOG_FILE}"
echo "# /private/var/db/dslocal/nodes/Default/config/SharePoints/NetBootClients0.plist" | tee -a "${PATH_TO_LOG_FILE}"
echo""

echo "dscl . -create SharePoints/NetBootClients0" | tee -a "${PATH_TO_LOG_FILE}"
dscl . -create SharePoints/NetBootClients0 | tee -a "${PATH_TO_LOG_FILE}"

echo "dscl . -create SharePoints/NetBootClients0 afp_guestaccess 1" | tee -a "${PATH_TO_LOG_FILE}"
dscl . -create SharePoints/NetBootClients0 afp_guestaccess 1 | tee -a "${PATH_TO_LOG_FILE}"

echo "dscl . -create SharePoints/NetBootClients0 afp_name NetBootClients0" | tee -a "${PATH_TO_LOG_FILE}"
dscl . -create SharePoints/NetBootClients0 afp_name NetBootClients0 | tee -a "${PATH_TO_LOG_FILE}"

echo "dscl . -create SharePoints/NetBootClients0 afp_shared 1" | tee -a "${PATH_TO_LOG_FILE}"
dscl . -create SharePoints/NetBootClients0 afp_shared 1 | tee -a "${PATH_TO_LOG_FILE}"

echo "dscl . -create SharePoints/NetBootClients0 directory_path /Library/NetBoot/NetBootClients0" | tee -a "${PATH_TO_LOG_FILE}"
dscl . -create SharePoints/NetBootClients0 directory_path /Library/NetBoot/NetBootClients0 | tee -a "${PATH_TO_LOG_FILE}"

echo "dscl . -create SharePoints/NetBootClients0 ftp_name NetBootClients0" | tee -a "${PATH_TO_LOG_FILE}"
dscl . -create SharePoints/NetBootClients0 ftp_name NetBootClients0 | tee -a "${PATH_TO_LOG_FILE}"

# Not sure how to generate the sharepoint group id and works without specifying it.
#dscl . -create SharePoints/NetBootClients0 sharepoint_group_id F7D1C81A-53EF-48E7-BF47-D2ABEF4F400B | tee -a "${PATH_TO_LOG_FILE}"

echo "dscl . -create SharePoints/NetBootClients0 smb_createmask 644" | tee -a "${PATH_TO_LOG_FILE}"
dscl . -create SharePoints/NetBootClients0 smb_createmask 644 | tee -a "${PATH_TO_LOG_FILE}"

echo "dscl . -create SharePoints/NetBootClients0 smb_directorymask 755" | tee -a "${PATH_TO_LOG_FILE}"
dscl . -create SharePoints/NetBootClients0 smb_directorymask 755 | tee -a "${PATH_TO_LOG_FILE}"

echo "dscl . -create SharePoints/NetBootClients0 smb_guestaccess 0" | tee -a "${PATH_TO_LOG_FILE}"
dscl . -create SharePoints/NetBootClients0 smb_guestaccess 0 | tee -a "${PATH_TO_LOG_FILE}"

echo "dscl . -create SharePoints/NetBootClients0 smb_name NetBootClients0" | tee -a "${PATH_TO_LOG_FILE}"
dscl . -create SharePoints/NetBootClients0 smb_name NetBootClients0 | tee -a "${PATH_TO_LOG_FILE}"

echo "dscl . -create SharePoints/NetBootClients0 smb_shared 0" | tee -a "${PATH_TO_LOG_FILE}"
dscl . -create SharePoints/NetBootClients0 smb_shared 0 | tee -a "${PATH_TO_LOG_FILE}"

# Restarting DirectoryService after modifying database.
echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "# Restarting DirectoryService after modifying database." | tee -a "${PATH_TO_LOG_FILE}"
echo "killall DirectoryService" | tee -a "${PATH_TO_LOG_FILE}"
killall DirectoryService | tee -a "${PATH_TO_LOG_FILE}"

# Make sure AFP is running by starting the daemon
echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "# Make sure AFP is running by starting the daemon" | tee -a "${PATH_TO_LOG_FILE}"
echo "/usr/sbin/AppleFileServer" | tee -a "${PATH_TO_LOG_FILE}"
/usr/sbin/AppleFileServer | tee -a "${PATH_TO_LOG_FILE}"

# Unload the AppleFileServer launch daemons
echo ""
echo "# Unload the associated launch daemons"
echo "launchctl unload -w /System/Library/LaunchDaemons/com.apple.AppleFileServer.plist" | tee -a "${PATH_TO_LOG_FILE}"
launchctl unload -w /System/Library/LaunchDaemons/com.apple.AppleFileServer.plist | tee -a "${PATH_TO_LOG_FILE}"

echo ""
echo "# Load the associated launch daemons"

echo "" > /tmp/load_applefileserver_error.txt | tee -a "${PATH_TO_LOG_FILE}"
LOAD_APPLEFILESERVER_ERROR=`cat /tmp/load_applefileserver_error.txt | grep "Address already in use" | sed "s%bind(): %%g"`

INDEX=1

while [[ "${LOAD_APPLEFILESERVER_ERROR}" != "Address already in use" && "${INDEX}" -lt 10 ]]
do

    echo "launchctl load -w /System/Library/LaunchDaemons/com.apple.AppleFileServer.plist 2>/tmp/config_netboot_load_applefileserver_error.txt" | tee -a "${PATH_TO_LOG_FILE}"
    launchctl load -w /System/Library/LaunchDaemons/com.apple.AppleFileServer.plist 2>/tmp/load_applefileserver_error.txt | tee -a "${PATH_TO_LOG_FILE}"
    LOAD_APPLEFILESERVER_ERROR=`cat /tmp/load_applefileserver_error.txt | grep "Address already in use" | sed "s%bind(): %%g"`

    echo "Loading com.apple.AppleFileServer item error is: ${LOAD_APPLEFILESERVER_ERROR}"
    echo "Index is: $INDEX"
    echo ""

    let INDEX=INDEX+1

    sleep 30

done

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "=======================================================================================================" | tee -a "${PATH_TO_LOG_FILE}"
echo " Time Completed : $(date +"%r")" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

}

#////////////////////////////////////////////////////////////////////////////////////////
#////////////////////////////////////////////////////////////////////////////////////////
# MAIN

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "#######################################################################################################" | tee -a "${PATH_TO_LOG_FILE}"
echo "=======================================================================================================" | tee -a "${PATH_TO_LOG_FILE}"
echo " Script Started: $0" | tee -a "${PATH_TO_LOG_FILE}"
echo " Date: $(date)" | tee -a "${PATH_TO_LOG_FILE}"
echo "=======================================================================================================" | tee -a "${PATH_TO_LOG_FILE}"
echo "#######################################################################################################" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

run_as_root
create_netboot_dirs_and_symlinks
config_nfs_exports
config_tftp
download_netboot_image
config_bootpd_template
modify_bootpd_template_items
config_afp

echo "" | tee -a "${PATH_TO_LOG_FILE}"
echo "#######################################################################################################" | tee -a "${PATH_TO_LOG_FILE}"
echo "=======================================================================================================" | tee -a "${PATH_TO_LOG_FILE}"
echo " Script Completed: $0" | tee -a "${PATH_TO_LOG_FILE}"
echo " Date: $(date)" | tee -a "${PATH_TO_LOG_FILE}"
echo "=======================================================================================================" | tee -a "${PATH_TO_LOG_FILE}"
echo "#######################################################################################################" | tee -a "${PATH_TO_LOG_FILE}"
echo "" | tee -a "${PATH_TO_LOG_FILE}"

# $? reads the exit status of the last command executed.
# After a function returns, $? gives the exit status of the last command exeaed in the function.

exit $?