#
#   makeWinPackage - Create windows packages
#
#   Copyright (c) Embedthis Software LLC, 2003-2009. All Rights Reserved.
#
################################################################################

setupWin() {
    echo -e "\n  #\n  # Create the Windows installation packages ...\n  #"
}

################################################################################

createInnoSetupFile() {

    local count f dir flags name base

    echo "  #    Creating binary file list ..."

    cp package/WIN/install.iss $ROOT_DIR
    patchFile $ROOT_DIR/install.iss

    cd "${BLD_PREFIX}"
    count=0
    find * -print | while read f
    do
        if [ ! -d $f ]
        then
            name=`basename $f`
            dir=`dirname $f`
            #
            # Get the dirname portion and change all "/" to "\"
            #
            flags=""
            if [ "$name" = "README.TXT" -a $count = 0 ]
            then
                #
                #   Now that we display the doc on installation, don't want to also display the readme.
                #
                #       flags=isreadme
                #
                count=1
            fi
            echo -n "Source: \"${BLD_PREFIX}/${f}\"; DestDir: \"{app}/${dir}\"; "
            echo "DestName: \"${name}\"; Flags: \"$flags\"; Components: bin"
        fi
    done >>"${ROOT_DIR}/install.iss"
    cd $HOME

    #
    #   Add the web directory to the spec file if it is not a subdir of the BLD_PREFIX
    #
	if [ "${BLD_WEB_PREFIX}" != "" ] ; then
		echo ${BLD_WEB_PREFIX} | grep ${BLD_PREFIX} >/dev/null 2>&1
		if [ $? != 0 ]
		then
			cd "${BLD_WEB_PREFIX}"
			echo "  #    Creating web file list ..."
			find * -print | while read f
			do
				if [ ! -d "$f" ]
				then
					name=`basename "$f"`
					dir=`dirname "$f"`
					flags=""
					prefix="${ORIG_BLD_WEB_PREFIX}"
					echo -n "Source: \"${BLD_WEB_PREFIX}/${f}\"; DestDir: \"${prefix}/${dir}\"; "
					echo "DestName: \"${name}\"; Flags: \"$flags\"; Components: bin"
				fi
			done >>"${ROOT_DIR}/install.iss"
			cd "$HOME"
		fi
	fi

    #
    #   Add dev 
    #
    if [ -d "${BLD_INC_PREFIX}" ] ; then
        cd "${BLD_INC_PREFIX}"
        echo "  #    Creating development headers file list ..."

        find * -print | while read f
        do
            if [ ! -d "$f" ]
            then
                name=`basename "$f"`
                dir=`dirname "$f"`
                flags=""
                echo -n "Source: \"${BLD_INC_PREFIX}/${f}\"; DestDir: \"{app}/include/${dir}\"; "
                echo "DestName: \"${name}\"; Flags: \"$flags\"; Components: dev"
            fi
        done >> "${ROOT_DIR}/install.iss"
        cd "$HOME"
    fi

    #
    #   Add documentation to the spec file
    #
    base=${BLD_DOC_PREFIX##*/} 
    if [ -d "${BLD_DOC_PREFIX}" ] ; then
        cd "${BLD_DOC_PREFIX}"
        echo "  #    Creating documentation file list ..."

        find * -print | while read f
        do
            if [ ! -d "$f" ]
            then
                name=`basename "$f"`
                dir=`dirname "$f"`
                flags=""
                echo -n "Source: \"${BLD_DOC_PREFIX}/${f}\"; DestDir: \"{app}/${base}/${dir}\"; "
                echo "DestName: \"${name}\"; Flags: \"$flags\"; Components: dev"
            fi
        done >> "${ROOT_DIR}/install.iss"
        cd "$HOME"
    fi

##  #
##  #   Add samples to the spec file 
##  #
##  base=${BLD_SAM_PREFIX##*/} 
##  if [ -d "${BLD_SAM_PREFIX}" ] ; then
##      cd "${BLD_SAM_PREFIX}"
##      echo "  #    Creating samples file list ..."
##      find * -print | while read f
##      do
##          if [ ! -d "$f" ]
##          then
##              name=`basename "$f"`
##              dir=`dirname "$f"`
##              flags=""
##              echo -n "Source: \"${BLD_SAM_PREFIX}/${f}\"; DestDir: \"{app}/${base}/${dir}\"; "
##              echo "DestName: \"${name}\"; Flags: \"$flags\"; Components: src"
##          fi
##      done >> "${ROOT_DIR}/install.iss"
##      cd "$HOME"
##  fi

    #
    #   Add src (includes samples)
    #
    cd "${BLD_SRC_PREFIX}"
    echo "  #    Creating source file list"
 
    find * -print | while read f
    do
        if [ ! -d "$f" ]
        then
            name=`basename "$f"`
            dir=`dirname "$f"`
            flags=""
            echo -n "Source: \"${BLD_SRC_PREFIX}/${f}\"; DestDir: \"{app}/src/${dir}\"; "
            echo "DestName: \"${name}\"; Flags: \"$flags\"; Components: src"
        fi
    done >> "${ROOT_DIR}/install.iss"
    cd "$HOME"


    #
    #   Fixup paths in the spec file
    #
    cp "${ROOT_DIR}/install.iss" "${ROOT_DIR}/install.new"
    cat "${ROOT_DIR}/install.new" | sed -e "/LicenseFile/s^.*^LicenseFile=${BLD_PREFIX}/LICENSE.TXT^g" | \
        sed -e 's/={sd}.:/={sd}/' > "${ROOT_DIR}/install.iss" 
    rm -f cp "${ROOT_DIR}/install.new"

    unix2dos -D ${ROOT_DIR}/install.iss 2>/dev/null
}

################################################################################
#
# Run Inno Setup to create the image file
#

makeWinPackage() {

    local drive

    echo -e "\n  #\n  # Creating windows installer package ...\n  #"

    cd "${ROOT_DIR}"

    name=`createPackageName ${BLD_PRODUCT}`.exe

    drive=`cygpath -m / | sed -e 's/:.*//'`
    echo "  ${drive}:/Program Files/Inno Setup 5/iscc" install.iss
    "${drive}:/Program Files/Inno Setup 5/iscc" install.iss >../ino.log 2>&1
    if [ $? != 0 ]
    then
        echo "Inno Setup error. Log:"
        cat ../ino.log
        exit 255
    fi
    rm -f ../ino.log
    cp Output/setup.exe $name
    cd "$HOME"
}

################################################################################
#
#   Main
#

setupWin
createInnoSetupFile
makeWinPackage
