#! /bin/sh
#
# usage:        genpathdef.sh matlabroot
#		where matlabroot is the top level matlab directory.
#
# abstract:     This script generates a new pathdef.m file, preserving any
#		user added paths.
# 		This reads all the .phl files in toolbox/local/path and combines
#		those entries with any that already exist in pathdef.m.
#
# note(s):      
#
# Copyright (c) 1992-2004 The MathWorks, Inc. All Rights Reserved.
# $Revision: 1.1.6.4 $  $Date: 2004/03/22 23:28:20 $
#----------------------------------------------------------------------------
#

# This function takes a phl filename as input and checks if
# the entries in the file exist as paths on the file system.  
# If they do exist, they will be echo'ed to stdout in the format 
# that they should be listed in pathdef.m.
get_phl_entries () {
    # Use the echo command to handle phl files that might not
    # have a line ending character at the end of the file.
    # Use tr to remove any DOS line endings. Change the \r to \n
    # because the Mac uses \r as its line endings and we need \n.
    echo "" | cat $1 - | tr "\r" "\n" |
    (while read filepath
     do
	if [ -d "${ML_ROOT}/$filepath" -a "$filepath" != "" ]; then
		echo "matlabroot,'/"$filepath":', ..."
	fi
    done ) 
}

# Get matlab root directory from input.
    ML_ROOT=$1

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
    echo "          A2. Generate MATLAB path in $ML_ROOT/toolbox/local/pathdef.m"
    echo '          ---------------------------------------------------------------'
#
    if [ -d ${ML_ROOT}/toolbox/local/path ]; then
#
        echo 'Creating MATLAB path . . .'
        echo ''
#
        pathdeffile=${ML_ROOT}/toolbox/local/pathdef.m
        templatefile=${ML_ROOT}/toolbox/local/template/pathdef.m
        phldir=${ML_ROOT}/toolbox/local/path
	origpaths=/tmp/orig.$$
	mlpaths=/tmp/ml.$$
	slpaths=/tmp/sl.$$
	rtwpaths=/tmp/rtw.$$
	sfpaths=/tmp/sf.$$
	otherpaths=/tmp/other.$$
    
#
# Read in existing pathdef.m file
#
        if [ -f $pathdeffile ]; then
	    # Get paths, strips spaces and tabs from beginning and end of lines.
	    # Look for the begin and end markers in the file to figure out
	    # where the path entries begin and end.
	    cat $pathdeffile | awk '
	    BEGIN  {state = 0}
	    index($0,"%%% BEGIN ENTRIES %%%") != 0 { state = 1; next }
	    index($0,"%%% END ENTRIES %%%")   != 0 { exit }
	    state == 1 {
		printf "%s\n",$0 
		next } ' | \
	    sed -e "s/^ *//" | sed -e "s/^	*//" | sed -e "s/ *$//" | sed -e "s/	*$//" \
	    > $origpaths
	else
	    cat /dev/null > $origpaths
        fi

# Make sure temp files are empty
	rm -f $mlpaths $slpaths $rtwpaths $sfpaths $otherpaths
	cat /dev/null > $mlpaths
	cat /dev/null > $slpaths
	cat /dev/null > $rtwpaths
	cat /dev/null > $sfpaths
	cat /dev/null > $otherpaths

#
# Read in the .phl files
#
        for file in ${phldir}/*.phl
        do
	    case "$file" in
#----------------------------------------------------------------------------
		*/matlab.phl)
		    outfile=$mlpaths
 		    ;;
#----------------------------------------------------------------------------
		*/simulink.phl)
		    outfile=$slpaths
 	            ;;
#----------------------------------------------------------------------------
		*/stateflow.phl)
		    outfile=$sfpaths
 	            ;;
#----------------------------------------------------------------------------
		*/rtw.phl)
		    outfile=$rtwpaths
	            ;;
#----------------------------------------------------------------------------
	        *)
		    outfile=$otherpaths
	            ;;
#----------------------------------------------------------------------------
	    esac
	    get_phl_entries $file >> $outfile
        done
#
# Create new pathdef.m 
#
	# Print out top half of file
        cat $templatefile | awk '
	BEGIN { magic_str = "<PLEASE FILL IN ONE DIRECTORY PER LINE>"}
	index($0, magic_str) != 0 {exit}
	{print $0}
	' > $pathdeffile

	# Print out all the paths and remove any duplicates
	cat $origpaths $mlpaths $slpaths $rtwpaths $sfpaths $otherpaths | awk '
	{   path = $0
	    if (patharr[path] != 1) 
	    {
	    	printf "%s\n", path
	        patharr[path] = 1;
	    }
	} ' >> $pathdeffile

	# clean up all the temp files
	rm -f $origpaths $mlpaths $slpaths $rtwpaths $sfpaths $otherpaths 

	# Print out bottom half of file
        cat $templatefile | awk '
	BEGIN { state = 0; 
		magic_str = "<PLEASE FILL IN ONE DIRECTORY PER LINE>"}
	index($0, magic_str) != 0 {state = 1; next}
	state == 1 {print $0}
	' >> $pathdeffile

        chmod 644 $pathdeffile
    fi
#

