#
# usage:        util.sh
#
# abstract:     This Bourne Shell script of utility functions for
#		the installer.
#
# note(s):      1. This routine must be called using a . (period)
#
# Copyright (c) 1998-2004 The MathWorks, Inc. All Rights Reserved.
# $Revision: 1.11.14.6 $  $Date: 2004/07/24 23:21:07 $
#----------------------------------------------------------------------------
#
#=======================================================================
#
# Functions:
#
#   clean_temp_areas ()
#   clear_screen ()
#   echon ()
#   get_oldname ()
#   premature_finish ()
#   query_to_continue ()
#
#=======================================================================
    clean_temp_areas () { # Removed any temporary files and directories.
		          #
                          # Always returns a 0 status.
                          #
                          # usage: clean_temp_areas
                          #
        rm -rf $temp_file $temp_file2 $temp_file3 $temp_file4 $temp_file5
        rm -rf $MATLAB/update/$$a $MATLAB/update/ftp $LSCREEN
	rm -rf $temp_boot
	return 0
    }
#=======================================================================
    clear_screen () { # Clear the screen if no arg and not in batch.
                      # Always output one line in case clear doesn't
                      # work.
                      #
                      # Always returns a 0 status.
                      #
                      # usage: clear_screen arg
                      #
#-----------------------------------------------------------------------
        echo ''
#-----------------------------------------------------------------------
        if [ $# -eq 0 ]; then
            if [ "$batch" != "1" ]; then
                clear
            fi
        fi
        return 0
    }
#=======================================================================
    echon () { # Echo's out a message without the newline so that the
               # user can be prompted.
               # 
               # Always returns a 0 status.
               # 
               # usage: echon mesg
               # 
        if [ "`echo -n`" != "" ]; then
            echo "$1\c"
        else
            echo -n "$1"
        fi
        return 0
    }
#=======================================================================
    get_oldname () { # This Bourne Shell script given oldname returns the
                     # unique name in old/ so that oldname can be placed
		     # there without overwriting any other file. The
		     # original oldname and old/ are in the directory
		     # specified by dir_oldname.  The format of a file
                     # in old/ is:
		     #         oldname.date.#
                     # date is taken from the file. If old/ does not
		     # exist it is created. We assume the file exists.
                     #
                     # note(s): 1. The variable 'oldname' holds the
		     #             filename.
                     #		3. The variable 'dir_oldname' holds the
		     #             directory where oldname and old/
		     #             exist.
                     #		4. date = ddmmmyy (mmm in lowercase)
		     #
                     # Always returns a 0 status.
                     # 
                     # usage: get_oldname dir_oldname file_oldname
                     # 
	dir_oldname=$1
	file_oldname=$2
#
        if [ ! -d $dir_oldname/old ]; then
	    mkdir $dir_oldname/old
	    chmod 755 $dir_oldname/old
        fi
#
        daymonth=`(cd $dir_oldname; ls -l $file_oldname | awk '{ print $(NF-2) $(NF-3) }' | tr '[:upper:]' '[:lower:]')`
	year=`date | awk '{ print $NF }'`
	date=$daymonth$year
#
	nfiles=`(cd $dir_oldname/old; ls $file_oldname.$date.* 2>/dev/null) | awk '
#----------------------------------------------------------------------------
    BEGIN { maxn = 0; digits = "0123456789" }
          { n = split($0,a,".")
	    if (n >= 3) {
	        m = a[n]
		for (i = 1; i <= length(m); i = i + 1)
		    if (index(digits,substr(m,i,1)) == 0) 
			break
	        if (i == length(m) + 1) 
		    if (m > maxn)
			 maxn = m 
	    }
	  }
    END { print maxn }'`
#----------------------------------------------------------------------------
        nfiles=`expr $nfiles + 1`
	echo $file_oldname.$date.$nfiles
#
	return 0
    }
#=======================================================================
    premature_finish () { # Premature finish
		          #
		          # Always returns a 0 status
		          #
			  # usage: premature_finish
		          #
#
	clean_temp_areas
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ''
    echo "    $CMDNAME stopped prematurely . . ."
    echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        exit 1
    }
#=======================================================================
    query_to_continue () { # Query the user to continue if not in batch
			   # mode and no arguments.
			   #
		           # Always returns a 0 status.
		           #
			   # usage: query_to_continue arg
		           #
        if [ "$batch" = "0" -a $# -eq 0 ]; then
	    echon 'Continue? ([y]/n) '
            read ans
            if [ `expr "//$ans" : '//[Nn].*'` -gt 0 ]; then
		premature_finish
            fi
        fi
	return 0
    }
#=======================================================================
