#! /bin/zsh
##
# Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
#
# @APPLE_LICENSE_HEADER_START@
# 
# The contents of this file constitute Original Code as defined in and
# are subject to the Apple Public Source License Version 1.1 (the
# "License").  You may not use this file except in compliance with the
# License.  Please obtain a copy of the License at
# http://www.apple.com/publicsource and read it before using this file.
# 
# This Original Code and all software distributed under the License are
# distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
# EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
# INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
# License for the specific language governing rights and limitations
# under the License.
# 
# @APPLE_LICENSE_HEADER_END@
##
# convertBundle <bundlePath>
#
# Converts the given bundle from the old app packaging to the new app packaging.

setopt EXTENDED_GLOB
# From the zsh documentation
# Treat the `#', `~' and `^' characters as part of patterns for filename generation, etc. 
# (An initial unquoted `~' always produces named directory expansion.) 

setopt NULL_GLOB
setopt GLOB_DOTS
# From the zsh documentation
# If a pattern for filename generation has no matches, 
# delete the pattern from the argument list instead of reporting an error. 
# Overrides NOMATCH. 

SetFilePath=${NEXT_ROOT}/System/Developer/Tools/SetFile
PlistConverterPath=${NEXT_ROOT}/Developer/Makefiles/pb_makefiles/CFInfoPlistConverter

if [ "$#" -lt "1" ] ; then
    echo "Usage: $0 <bundlePath>"
    exit 1
fi

# Split the bundle path up into the directory, the basename, and the extension
bundlePath="$1"
bundleDir="${bundlePath:h}"
bundleName="${bundlePath:t:r}"
bundleType="${bundlePath:e}"

if [ "${bundleDir}" = "${bundlePath}" ] ; then
    bundleDir=`pwd`
fi

# Make sure it exists
if [ \! -e "${bundleDir}/${bundleName}.${bundleType}" ] ; then
    echo "${bundleDir}/${bundleName}.${bundleType} does not seem to exist.  Exiting"
    exit 1
fi

echo "Converting bundle named ${bundleName} of type ${bundleType} \n    in directory ${bundleDir}."

# Back up the original bundle
#if [ \! -e "${bundleDir}/${bundleName}.${bundleType}.orig" ] ; then
#    echo -n "Backing up bundle..."
#    cd ${bundleDir}
#    mkdir _backupCBTemp
#    gnutar -cf - "${bundleName}.${bundleType}" | ( cd _backupCBTemp ; gnutar -xf - )
#    mv "_backupCBTemp/${bundleName}.${bundleType}" "${bundleName}.${bundleType}.orig"
#    rmdir _backupCBTemp
#    echo "done."
#fi

# Create Contents always
cd ${bundleDir}/${bundleName}.${bundleType}
mkdir "Contents"
if [ \! -e "${bundleDir}/${bundleName}.${bundleType}/Contents" ] ; then
    echo "Could not create Contents folder.  Exiting"
    exit 1
fi

# Handle Info.plist
cd ${bundleDir}/${bundleName}.${bundleType}
if [ -d "Resources" ] ; then
    echo -n "Doing Info.plist..."

    # first move them out of Resources
    for i in `ls Resources/Info*.plist` ; do
	mv ${i} "Contents"
    done
    
    # now change the platform specifier to modern version for any plists
    cd "Contents"
    if [ ! -e "Info-macos.plist" ] ; then
	if [ -e "Info-macosx.plist" ] ; then
	    mv "Info-macosx.plist" "Info-macos.plist"
	elif [ -e "Info-macintosh.plist" ] ; then
	    mv "Info-macintosh.plist" "Info-macos.plist"
	elif [ -e "Info-nextstep.plist" ] ; then
	    mv "Info-nextstep.plist" "Info-macos.plist"
	fi
    fi
    # Make sure we have no old versions hanging around.
    rm -f "Info-macosx.plist" "Info-macintosh.plist" "Info-nextstep.plist"
    
    if [ ! -e "Info-windows.plist" ] ; then
	if [ -e "Info-winnt.plist" ] ; then
	    mv "Info-winnt.plist" "Info-windows.plist"
	elif [ -e "Info-win32.plist" ] ; then
	    mv "Info-win32.plist" "Info-windows.plist"
	fi
    fi
    # Make sure we have no old versions hanging around.
    rm -f "Info-winnt.plist" "Info-win32.plist"

    if [ ! -e "Info-macosclassic.plist" ] ; then
	if [ -e "Info-macos8.plist" ] ; then
	    mv "Info-macos8.plist" "Info-macosclassic.plist"
	fi
    fi
    # Make sure we have no old versions hanging around.
    rm -f "Info-macos8.plist"

    # finally, run them through the plist converter    
    if [ "${PlistConverterPath}" -a -x "${PlistConverterPath}" ] ; then
	for i in `ls Info*.plist` ; do
	    ${PlistConverterPath} "${bundleDir}/${bundleName}.${bundleType}" "`pwd`/${i}" "${bundleType}"
	done
    fi
    
    echo "done."
fi

# Handle Resources directory
cd ${bundleDir}/${bundleName}.${bundleType}
if [ -d "Resources" ] ; then
    echo -n "Doing Resources..."
    
    mv "Resources" "Contents"
    cd "Contents/Resources"
    
    # We want to search the contents of all the lproj directories and the top-level directory where the global stuff is.  The ls of "." will include the lproj folders, but we do not really care...  lproj folders will not have names that match the folowing patterns (or if they do it is probably a sign of the apocalypse).
    for curLProj in `ls -d *.lproj` "." ; do
	echo converting files in Resources/$curLProj
	cd "${bundleDir}/${bundleName}.${bundleType}/Contents/Resources/${curLProj}"
	# -macintosh -> -macos
#        files=`ls -d *-macintosh.*`
#	for curResource in ${(f)files} ; do
#	    if [ "${curResource}" != "." ] ; then
#		mv ${curResource} ${curResource:s/-macintosh/-macos/}
#	    fi
#	done
#        files=`ls -d *-macintosh`
#	for curResource in ${(f)files} ; do
#	    if [ "${curResource}" != "." ] ; then
#		mv ${curResource} ${curResource:s/-macintosh/-macos/}
#	    fi
#	done
	# -macosx -> -macos
        files=`ls -d *-macosx.*`
	for curResource in ${(f)files} ; do
	    if [ "${curResource}" != "." ] ; then
		mv ${curResource} ${curResource:s/-macosx/-macos/}
	    fi
	done
        files=`ls -d *-macosx`
	for curResource in ${(f)files} ; do
	    if [ "${curResource}" != "." ] ; then
		mv ${curResource} ${curResource:s/-macosx/-macos/}
	    fi
	done
	# -winnt -> -windows
        files=`ls -d *-winnt.*`
	for curResource in ${(f)files} ; do
	    if [ "${curResource}" != "." ] ; then
		mv ${curResource} ${curResource:s/-winnt/-windows/}
	    fi
	done
        files=`ls -d *-winnt`
	for curResource in ${(f)files} ; do
	    if [ "${curResource}" != "." ] ; then
		mv ${curResource} ${curResource:s/-winnt/-windows/}
	    fi
	done
	# -win32 -> -windows
        files=`ls -d *-win32.*`
	for curResource in ${(f)files} ; do
	    if [ "${curResource}" != "." ] ; then
		mv ${curResource} ${curResource:s/-win32/-windows/}
	    fi
	done
        files=`ls -d *-win32`
	for curResource in ${(f)files} ; do
	    if [ "${curResource}" != "." ] ; then
		mv ${curResource} ${curResource:s/-win32/-windows/}
	    fi
	done
	# -macos8 -> -macosclassic
        files=`ls -d *-macos8.*`
	for curResource in ${(f)files} ; do
	    if [ "${curResource}" != "." ] ; then
		mv ${curResource} ${curResource:s/-macos8/-macosclassic/}
	    fi
	done
        files=`ls -d *-macos8`
	for curResource in ${(f)files} ; do
	    if [ "${curResource}" != "." ] ; then
		mv ${curResource} ${curResource:s/-macos8/-macosclassic/}
	    fi
	done
    done
    
    echo "done."
fi


# Handle Executables
# MF:!!! Only handles executables with same name as bundle.
cd ${bundleDir}/${bundleName}.${bundleType}
if [ -x "${bundleName}" ] ; then
    echo -n "Doing Executables..."

    mkdir -p "Contents/MacOS"
    mv "${bundleName}" "Contents/MacOS"
    # Move the resources for Carbon applications.
    if [ -d "${bundleName}..." ]; then
	mv "${bundleName}..." "Contents/MacOS"
    fi
    if [ -e ".%${bundleName}" ]; then
	mv ".%${bundleName}" "Contents/MacOS"
    fi
    if [ -e "._${bundleName}" ]; then
	mv "._${bundleName}" "Contents/MacOS"
    fi

    echo "done."
fi


### Don't run SetFile to turn on the Bundle bit, because that causes 
### a ._* file (containing Finder info) to be created, and that file
### looks like a resource fork on UFS.  To the greatest extent possible,
### we want to avoid the use of resource forks in our system.  Instead,
### the Finder will detect the presence of well-known suffixes
### (e.g., ".app") to know that a directory is really a wrapped package
### which users shouldn't navigate into.

# Set the bundle bit
#cd ${bundleDir}
#if [ "${SetFilePath}" -a -x "${SetFilePath}" ] ; then
#    echo -n "Doing Bundle bit..."
#    ${SetFilePath} -a B ${bundleName}.${bundleType}
#    echo "done."
#fi
