#!/bin/sh

# /mac/bin/mac32
#
# !!! THIS IS A SPECIAL VERSION OF /mac/bin/mac32 that is aware of FMbackup !!!
# !!! @(#)mac32 for FMbackup version 1.0.4                                  !!!
#
# Shell script which launches 32-bit Macintosh environment.  This script
# is normally invoked as the initial command to the user's shell when
# logging in to the A/UX Finder (32-bit) session type.
#
# A user can override this global script completely for login by placing
# a file called ".mac32" in his home directory.  That file could be created
# by copying this file and making desired modifications, such as removing
# the -u flag from the CommandShell command.
#

SMLOGFILE=/tmp/mac32sm.$$	# startmac log file
CSLOGFILE=/tmp/mac32cs.$$	# CommandShell log file
rm -f $SMLOGFILE $CSLOGFILE

# TBTRANSLATEUXONLY=1
# export TBTRANSLATEUXONLY
 
# TBFMIGNORECASE=1
# export TBFMIGNORECASE

TBTRANSLATESLASHES=1
export TBTRANSLATESLASHES

#
# handle FMbackup stuff if needed
#
#
# first, locate the user's "System Folder"
#
if [ -d "$HOME/System Folder" ]; then
    SYSTEMFOLDER="$HOME/System Folder"
else
    SYSTEMFOLDER="/mac/sys/System Folder"
fi

#
# and locate the user's saved desktop file
#
DESKFILE="$HOME/.mac/`/bin/hostname`/.Desktop.Z"

#
# now see if the FMbackup extension is to be loaded
#
if [ -r "$SYSTEMFOLDER/Extensions/FMbackup" ]; then
    export SYSTEMFOLDER
    export DESKFILE
    if [ -f "$DESKFILE" ]; then
	# Restore the user's saved desktop files
	zcat "$DESKFILE" | (cd "$SYSTEMFOLDER"; cpio -icuBm) >/dev/null 2>&1
    else
	# make directories if needed
	mkdir "$HOME/.mac"			> /dev/null 2>&1
	mkdir "$HOME/.mac/`/bin/hostname`"	> /dev/null 2>&1
    fi
else
#
# force the removal of the saved desktop file... this is needed if FMbackup
# was used, then not, then used again. The 2nd time would read the _old_
# copy when we want it to save the new one
#
    rm -f "$DESKFILE"
fi

#
# Launch the Macintosh environment.
#
/mac/bin/startmac > $SMLOGFILE 2>&1 &
STARTMAC_PID=$!

#
# Launch CommandShell to provide support for handling console messages.
# If you want CommandShell to come up in foreground with windows open
# for all users, change the -u flag to -q.
#
/mac/bin/CommandShell -u > $CSLOGFILE 2>&1 &
CMDSHELL_PID=$!

#
# Wait for startmac to exit, then make sure CommandShell is dead.
#
wait $STARTMAC_PID
kill $CMDSHELL_PID > /dev/null 2>&1

#
# If there is output in either log file, display the console screen and print
# the contents of the log files.  Redirect the output to /dev/tty so that it
# appears on the screen, rather than being hidden in /tmp/Login_output, since
# Login redirects standard output and error.
#
if [ -s $SMLOGFILE -o -s $CSLOGFILE ]; then
    screenrestore

    if [ -s $SMLOGFILE ]; then
	echo "Error output from startmac:"
	echo
	newform -p4 $SMLOGFILE
	echo
	echo
    fi > /dev/tty

    if [ -s $CSLOGFILE ]; then
	echo "Error output from CommandShell:"
	echo
	newform -p4 $CSLOGFILE
	echo
	echo
    fi > /dev/tty

    echo "Press the Return key to exit..." > /dev/tty
    read input < /dev/tty
    echo "Exiting..." > /dev/tty
else
    grep -s console $HOME/.aux_prefs > /dev/null 2>&1
    if [ $? -eq 0 ]; then
	screenrestore
    fi
fi

rm -f $SMLOGFILE $CSLOGFILE
