# Holds common install function used e.g. by install.sh and omd_install.sh

cmp() {
    if [ "$(uname)" = "Darwin" ]; then
        SED=gsed
    else
        SED=sed
    fi
    cat $1 | $SED 's#\(var\)\s*\(\S*\)\s*=\s*#\1 \2=#;s#^\s*##;s#\s*$##;s#\t+# #g' | awk '
    BEGIN { OK=1; braces=0 }
    {
        # Remove /* */ one line comments
        sub(/\/\*[^@]*\*\//,"");
        # Remove // comments (line beginning)
        sub(/^\/\/.*/,"");
        
        # Count braces
        anz1 = gsub(/\{/,"{");
        anz2 = gsub(/}/,"}");
        
        if (OK == 1) {
            braces += anz1;
            braces -= anz2;
        }
    }
    /\/\*/ {
        c = gsub(/\/\*[^@]*$/,"");
        if(c > 0) {
            OK=0;
        }
    }
    /\*\/$/ {
        c = gsub(/^[^@]*\*\//,"");
        if(c > 0) {
            OK=1;
        }
    }
    {
        line = $0;
        #anz = gsub(/function/," function");
        #ch = substr(line,length(line));
        if (OK == 1) {
            if (length(line) > 0) {
                #if (ch == "}") {
                #   if (braces == 0) {
                #       if (length(line) > 0) {
                #           print line
                #       }
                #       line = ""
                #   }
                #}
                #line = line $0;
                
                print line;
            }
        }
    }
    ' >> $OUT
}

cmp_js() {
    pushd $1 >/dev/null
    OUT=NagVisCompressed.js
    >$OUT
    cmp ExtStacktrace.js
    cmp ExtLeaflet.js
    cmp nagvis.js
    cmp edit.js
    cmp popupWindow.js
    cmp ExtBase.js
    cmp frontendMessage.js
    cmp frontendEventlog.js
    cmp ajax.js
    cmp ajaxActions.js
    cmp dynfavicon.js
    cmp Element.js
    cmp ElementContext.js
    cmp ElementHover.js
    cmp ElementIcon.js
    cmp ElementLine.js
    cmp ElementGadget.js
    cmp ElementLabel.js
    cmp ElementShape.js
    cmp ElementBox.js
    cmp ElementTile.js
    cmp View.js
    cmp ViewMap.js
    cmp ViewWorldmap.js
    cmp ViewOverview.js
    cmp ViewUrl.js
    cmp NagVisObject.js
    cmp NagVisStatefulObject.js
    cmp NagVisStatelessObject.js
    cmp NagVisHost.js
    cmp NagVisService.js
    cmp NagVisHostgroup.js
    cmp NagVisServicegroup.js
    cmp NagVisDynGroup.js
    cmp NagVisAggr.js
    cmp NagVisMap.js
    cmp NagVisShape.js
    cmp NagVisLine.js
    cmp NagVisTextbox.js
    cmp NagVisContainer.js
    cmp NagVisRotation.js
    cmp frontend.js
    cmp ExtWzJsGraphics.js
    cmp ExtJSColor.js
    popd >/dev/null
}
