Subversion Repositories DevTools

Rev

Rev 1530 | Blame | Compare with Previous | Last modification | View Log | RSS feed

####################################################################################################
# This is a class action script for the replace class 
# For each file in this class it will replace any replace tags in that file as it copies it to dest.
# It searches the src file for <REPLACETAG-XXX> and for each one it
#    Looks for Environment var XXX and if it exists it is used to replace the tag in the file
#    otherwise it prints a warning if the Var does not exist.
# If no tags are found in the file it is simply copied to dest
# These environment vars are setup by pkginfo or request script
# Just need to make sure that these vars match the XXX portion of the REPLACETAG
####################################################################################################

getList()
{
    sed -n '
    :start
    /<REPLACETAG/{
        :leading
        /^<REPLACETAG/!{
            # while replace tag not at beginning of line remove leading chars
            s/^<\{0,1\}[^<]*//
            b leading
        }
        # we now should have replace tag at start of line
        # save line
        h
        # print 1st REPLACETAG, ie remove all after tag and print line
        s/^<\(REPLACETAG[^>]*\)>.*$/\1/p
        #restore orignial line & remove printed replace & jump to start to see if any more on same line
        x
        s/^<REPLACETAG[^>]*>\(.*\)$/\1/
        b start
    }' $1 | sort | uniq | awk 'BEGIN { ORS=" " } { print $0 }'
}

Warn=

while read src dest
do  
    if [ "$src" != "/dev/null" ]; then
        # Get list of REPLACETAG- in current file
        LIST=`getList $src`
        Msg=
        sedArgs=
        
        for tag in $LIST
        do
            # Remove REPLACETAG- from front of tag to get the enviroment var name
            var=`echo $tag | sed 's|^REPLACETAG-||'`
            # now eval the name and expand the var to get contents into repl
            repl=`eval echo \\$$var`
    
            if [ -n "$repl" ]; then
                Msg="$Msg    [$tag=$repl]\n"
                sedArgs="$sedArgs -e 's|<$tag>|$repl|g'"
            else
                Warn="$Warn===>WARNING: Install Environment Var [$var] does not exist for replacement in [$dest]\n"
            fi
        done
    
        installf -c replace $PKGINST $dest f
        if [ -n "$sedArgs" ]; then
            printf "$dest <Replacements as follows>\n$Msg"
            eval sed $sedArgs $src > $dest
        else
            printf "$dest <No Replacements Found>\n$Msg"
            cp $src $dest
        fi
    fi
done

if [ "$1" = "ENDOFCLASS" ]; then
    installf -f -c replace $PKGINST
    echo "Replacements Completed"
    if [ -n "$Warn" ]; then
        printf "$Warn"
    fi
fi