#! /bin/sh
# @(#)cvtolwmmenu 1.6 90/06/19
#
# Convert an old .olwmmenu file to the standard .openwin-menu style.
# Reads a filename given on the command line, or standard input if no
# arguments are given.  Writes to standard output.
# Typical usage: cvtmenu .olwmmenu >.openwin-menu

awk '
BEGIN { menudepth = 0; }

# Menu stuff.
# For each line with MENU on it, push the tag onto the stack.  Pull
# them off when we encounter the matching END line.

# If we have a MENU line with a quoted tag, save push the quoted string
# onto the stack.
/"[^"]*"[ \t]*MENU/ {
    l = length($0);
    seen = 0;
    for (i=1; i<=l; ++i) {
	if (substr($0,i,1)=="\"") {
	    ++seen;
	    if (seen == 1)
		first = i;
	    if (seen == 2) {
		second = i;
		break;
	    }
	}
    }
    menustack[menudepth] = substr($0,first,second-first+1);
    menupin[menudepth] = 0;
    ++menudepth;
}

# If we have an unquoted MENU line, push the tag onto the stack.  The tag
# is $2 if there is a DEFAULT tag first on the line, otherwise $1.
/^[^"]*MENU/ {
    if ( NF == 3 )
	menustack[menudepth] = $2
    else
	menustack[menudepth] = $1;
    menupin[menudepth] = 0;
    ++menudepth;
}

# Handle a DEFAULT tag.  Move it after the label.
/^[ \t]*DEFAULT/ {
    for (i=1; i<=length($0); ++i) {
	if (substr($0,i,7)=="DEFAULT") {
	    rem = length($0)-i-6;
	    temp = sprintf("%s%s",substr($0,1,i-1),substr($0,i+7,rem));
	    break;
	}
    }

    for (i=1; i<=length(temp); ++i) {
	char = substr(temp,i,1);
	if (char != " " && char != "\t")
	    break;
    }

    if (char == "\"") {
	++i;
	while (substr(temp,i,1) != "\"") {
	    ++i;
	}
	++i;
    } else {
	while (substr(temp,i,1) != " " && substr(temp,i,1) != "\t") {
	    ++i;
	}
    }

    # strip any quotes from the command
    start = 0;
    end = 0;
    for (j=i; j<=length(temp); ++j) {
	if ( substr(temp,j,1) == "\"") {
	    if (start == 0)
		start = j+1;
	    else
		end = j-1;
	}
    }
    if (start == 0)
	cmd = substr(temp,i)
    else
	cmd = substr(temp,start,end-start+1)

    printf("%s DEFAULT %s\n",	\
	substr(temp,1,i-1),	\
	cmd);
    next;
}

# Pop the menu tag and insert it onto the line after the leading whitespace.
/^[ \t]*END/ {
    --menudepth;
    start = 0;
    for (i=1; i<=length($0); ++i) {
	char = substr($0,i,1);
	if (char != " " && char != "\t") {
	    start = i;
	    break;
	}
    }
    
    printf("%s%s END", substr($0,1,start-1), menustack[menudepth]);
    if (menupin[menudepth])
	printf(" PIN\n");
    else
	printf("\n");
    next;
}

# If we encounter "xvplaces", change it to SAVE_WORKSPACE.
/"xvplaces"/ {
    for (i=1; i<length($0); ++i) {
	if (substr($0,i,10) == "\"xvplaces\"") {
	    printf("%sSAVE_WORKSPACE%s\n",	\
		substr($0,1,i-1),		\
		substr($0,i+10,length($0)-i-9));
	}
    }
    next;
}

# If we PIN outside of a menu context, ignore it.  Otherwise,
# mark the current menu as pinnable.
/PIN/ {
    if (menudepth != 0) {
	menupin[menudepth-1] = 1;
    }
    next;
}

# If we encounter TITLE, move it to the end of the line.
/^[ \t]*TITLE/ {
    for (i=1; i<=length($0); ++i) {
	if (substr($0,i,5)=="TITLE")
	    break;
    }
    printf("%s%s TITLE\n",	\
	substr($0,1,i-1),	\
	substr($0,i+5,length($0)-i-4));
    next;
}

# A quoted string near the end of the line indicates a quoted command.
# Remove the quotes from the command.
/".*"[ \t]*$/ {
    seen = 0;
    for (i=length($0); i>=1; --i) {
	if (substr($0,i,1) == "\"") {
	    ++seen;
	    if (seen == 1)
		second = i;
	    if (seen == 2) {
		first = i;
		break;
	    }
	}
    }
    printf("%s%s%s\n",		\
	substr($0,1,first-1),	\
	substr($0,first+1,second-first-1),	\
	substr($0,second+1,length($0)-second));
    next;
}

{ print $0; }
' "$@"
