#!/bin/sh

# This script determines the previously installed locales
# and places the respective locale ids in the file 
# designated by $1
#
# Algorithm to use(per john perry):
#
#   Union of locids in locales_installed file (if it exists)
#      AND locids in pkgs with SUNW_LOC=single locid
#
#   If the above yields no locids, then use the INTERSECTION
#      of the locids from pkgs with SUNW_LOC=multiple locids
#


# installed_loc_file is the output file that will eventually be read
# by the solaris wizard.

installed_loc_file=$1

tmp_installed_loc_file=${1}.tmp
multi_installed_loc_file=${1}.multi

A_VAR_SADM=/a/var/sadm
PKGDIR=${A_VAR_SADM}/pkg
LOCALE_FILE=${A_VAR_SADM}/system/data/locales_installed

rm -f ${tmp_installed_loc_file} > /dev/null 2>&1
rm -f ${multi_installed_loc_file} > /dev/null 2>&1
rm -f ${installed_loc_file} > /dev/null 2>&1

#
# Use this flag in case we have multi locales in file which we delete
# during processing because of null intersection. Don't want to 
# recreate file a second time.
#
OK_TO_CREATE_MULTI_FILE="true"

#
# check to see if the locales_installed file has any locales
#
if [ -f ${LOCALE_FILE} ]
then
    LOCALES=`grep "^LOCALES=" ${LOCALE_FILE} 2>/dev/null | cut -d= -f2`
    locales_installed=`echo ${LOCALES}| cut -d= -f2 | sort -u | sed 's/,/ /g'`
    for locale in ${locales_installed}
    do
      echo $locale >> ${tmp_installed_loc_file}
    done
fi


if [ -d $PKGDIR ]
then
    cd $PKGDIR
    for pkg in *
    do
        pkglocs=`grep SUNW_LOC ${pkg}/pkginfo | cut -d= -f2 | sort -u | sed 's/,/ /g'`
        if [ "X$pkglocs" != "X" ]
        then
            set -- ${pkglocs} 

            #
            # Look for SUNW_LOC fields with a single locid
            #
            if [ $# = 1 ]
            then
                for locale in ${pkglocs}
                do
                    echo $locale >> ${tmp_installed_loc_file}
                done
            elif [ ! -f ${tmp_installed_loc_file} ]
            then
                #
                # Keep track of the intersection of locales from
                # SUNW_LOC=multiple locids in case we decide to use
                # them at the end. Store locids in tmp file. Once we
                # have a tmp_installed_loc_file, don't do this processing
                # as we have single locs.
                #
                if [ -f ${multi_installed_loc_file} ]
                then
                    multi_locs=`cat ${multi_installed_loc_file} | sort -u` 
                    rm -f ${multi_installed_loc_file} > /dev/null 2>&1
                    set -- ${multi_locs}
                    while [ $# -gt 0 ]
                    do
                        currentloc=$1
                        for pkgloc in ${pkglocs}
                        do
                            if [ ${pkgloc} = ${currentloc} ]
                            then
                                echo ${pkgloc} >> ${multi_installed_loc_file}
                            fi
                        done
                        shift ;
                    done
                else
                    if [ $OK_TO_CREATE_MULTI_FILE = "true" ]
                    then
                        for locale in ${pkglocs}
                        do
                            echo $locale >> ${multi_installed_loc_file}
                        done
                    fi
                    OK_TO_CREATE_MULTI_FILE="false"
                fi
            fi
        fi
    done

    # All done processing.
    # If there are single loc packages or if there are locs
    # in the locales_installed file (indicated by existence
    # of ${tmp_installed_loc_file}), use union of those.
    # Otherwise, if there are multi-locale packages, use 
    # intersection of locales in those packages, which
    # is now in ${multi_installed_loc_file}.
    #
    if [ -f ${tmp_installed_loc_file} ]
    then
        lang_ids=`cat ${tmp_installed_loc_file} | sort -u`
    elif [ -f ${multi_installed_loc_file} ]
    then
      #
      # use the multi-loc pkgs
      #
      lang_ids=`cat ${multi_installed_loc_file} | sort -u` 
    fi
  
    if [ "X$lang_ids" != "X" ]
    then
      for lang_id in ${lang_ids}
      do
              echo "${lang_id}" >> ${installed_loc_file}
      done
    fi

    # 
    # cleanup
    # 
    if [ -f ${tmp_installed_loc_file} ]
    then
        rm ${tmp_installed_loc_file}
    fi

    if [ -f ${multi_installed_loc_file} ]
    then
        rm ${multi_installed_loc_file}
    fi
fi

