#!/bin/sh

##
# Copyright 2002 Apple Computer, Inc.
#
# Clean up and reset files and devices.
##


    ConsoleMessage "Resetting files and devices"

    ##
    # Attempt to recover the passwd file, if needed.  This procedure is
    # primarily historical and makes sense only when the passwd file is edited
    # using the vipw command.  
    ##
    if [ -s /etc/ptmp ]; then
	if [ -s /etc/passwd ]; then
	    echo -n "Passwd file conflict with ptmp: "
	    ls -l /etc/passwd /etc/ptmp
	    echo "Moving ptmp to ptmp.save"
	    mv -f /etc/ptmp /etc/ptmp.save
	else
	    echo "Passwd file recovered from ptmp"
	    mv /etc/ptmp /etc/passwd
	fi
    elif [ -r /etc/ptmp ]; then
	echo "Removing passwd lock file"
	rm -f /etc/ptmp
    fi

    ConsoleMessage "Cleaning up"
    echo -n "    "

    ##
    # If the shutdown command was used to shut the system down, the file
    # /etc/nologin may have been created to prevent users from logging in.  
    # Remove it so that logins are enabled when the system comes up.
    ##
    echo -n " /etc/nologin"
    rm -f /etc/nologin

    # Clean out /private/tmp.
    if [ -d /private/tmp ]; then
        # blow away any _tmp_ in case it exists as well
        if [ -e /private/_tmp_ ]; then
            chflags -R nouchg /private/_tmp_ && rm -rf /private/_tmp_
        fi
    echo -n " /private/tmp"
    mv /private/tmp /private/_tmp_
    (chflags -R nouchg /private/_tmp_ && rm -rf /private/_tmp_) &
    fi
    mkdir -m 1777 /private/tmp

    # Clear /private/Network (created by automount).
    if [ -d /private/Network ]; then
        # blow away any _Network_ in case it exists as well
        if [ -e /private/_Network_ ]; then
            chflags -R nouchg /private/_Network_ && rm -rf /private/_Network_
        fi

        echo -n " /private/Network"
        mv /private/Network /private/_Network_
        {
            find /private/_Network_ -x -exec sh -c "chflags -R nouchg {} && rm -f -- {}" \; \
                >/dev/null 2>&1;
            find -d /private/_Network_ -x -type d -exec rmdir -- {} \; \
                >/dev/null 2>&1;
            rmdir /private/_Network_ >/dev/null 2>&1;
        } &
    fi


    # Move /var/run out of the way
    if [ -d /var/run ]; then
        # blow away any _run_ in case it exists as well
        if [ -e /var/_run_ ]; then
            chflags -R nouchg /var/_run_ && rm -rf /var/_run_
        fi
        mv /var/run /var/_run_
    fi

    # Make new /var/run
    mkdir -m 775 /var/run
    chown root:daemon /var/run
    mkdir -m 775 /var/run/StartupItems
    chown root:daemon /var/run/StartupItems
    mkdir -m 755 /var/run/proxy
    chown www:www /var/run/proxy
    mkdir -m 775 /var/run/davlocks
    chown www:www /var/run/davlocks

    # Move sudo back to /var/run, touch the contents of /var/run/sudo/* back to the epoch
    if [ -d /var/_run_/sudo ]; then
        mv /var/_run_/sudo /var/run/sudo
        touch -t 198501010000 /var/run/sudo/*
    fi

    # Clear utmp (who is logged on).
    touch /var/run/utmp

    # purge the _run_ directory if it exists
    if [ -d /var/_run_ ]; then
        (chflags -R nouchg /var/_run_ && rm -rf /var/_run_) &
    fi

    # Clear /var/spool/lock
    purgedir /var/spool/lock


    # if "/Desktop Folder" exists and has contents, make sure there is a
    #   "/Desktop (Mac OS 9)" symlink to it
    # if "/Desktop Folder" does not exist, exists but has no contents, or exists
    #   and has only a single file, ".DS_Store" then there should be no
    #   "/Desktop (Mac OS 9)" symlink
    # if there is some other file or directory with the name "/Desktop (Mac OS 9)"
    #   then just exit
    needlink=0
    if [ -d "/Desktop Folder" ]; then
        needlink=$(ls -a1 "/Desktop Folder" | wc -l)
        if [ "${needlink}" -eq 3 ]; then
            if [ -f "/Desktop Folder/.DS_Store" ]; then
                needlink=0
            fi
        fi
    fi

    if [ "${needlink}" -lt 3 ]; then
        if [ -h "/Desktop (Mac OS 9)" ]; then
            rm -f "/Desktop (Mac OS 9)"
        fi
    else
        if ! [ -e "/Desktop (Mac OS 9)" ]; then
            ln -s "/Desktop Folder" "/Desktop (Mac OS 9)"
        fi
    fi


    echo ""
