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

cmp() {
    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
    pwd
    OUT=NagVisCompressed.js
    >$OUT
    cmp ExtStacktrace.js
    cmp nagvis.js
    cmp edit.js
    cmp popupWindow.js
    cmp ExtBase.js
    cmp frontendMessage.js
    cmp frontendEventlog.js
    cmp frontendHover.js
    cmp hover.js
    cmp frontendContext.js
    cmp ajax.js
    cmp ajaxActions.js
    cmp dynfavicon.js
    cmp frontend.js
    cmp lines.js
    cmp NagVisObject.js
    cmp NagVisStatefulObject.js
    cmp NagVisStatelessObject.js
    cmp NagVisHost.js
    cmp NagVisService.js
    cmp NagVisHostgroup.js
    cmp NagVisServicegroup.js
    cmp NagVisMap.js
    cmp NagVisShape.js
    cmp NagVisLine.js
    cmp NagVisTextbox.js
    cmp NagVisContainer.js
    cmp NagVisRotation.js
    cmp ExtWzJsGraphics.js
    cmp ExtGenericResize.js
    cmp ExtJSColor.js
    popd >/dev/null
}
