#!/usr/bin/env python3

# Copyright (c) 2009, 2010, 2011, 2013, 2017, 2020 Nicira, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import getopt
import sys

def dot2pic(src, dst):
    scale = 1.0
    while True:
        line = src.readline()
        if not line:
            break

        words = line.split()
        command = words[0]
        if command == 'graph':
            scale = float(words[1])
        elif command == 'node':
            name = words[1]
            x = float(words[2])
            y = float(words[3])
            width = float(words[4])
            height = float(words[5])
            label, style, shape, color, fillcolor = words[6:11]
            x *= scale
            y *= scale
            width *= scale
            height *= scale
            dst.write("linethick = %f;\n" % (0.5 if style == 'bold' else 1.0))
            dst.write('box at %f,%f wid %f height %f "%s"\n'
                      % (x, y, width, height, name))
            if style == 'bold':
                inset = 2.0 / 72.0
                width -= inset * 2
                height -= inset * 2
                dst.write("box at %f,%f wid %f height %f\n"
                          % (x, y, width, height))
        elif command == 'edge':
            tail = words[1]
            head = words[2]
            n = int(words[3])

            # Extract x,y coordinates.
            words = words[4:]
            xy = []
            for i in range(n):
                x = float(words[0]) * scale
                y = float(words[1]) * scale
                words = words[2:]
                xy.append((x, y))

            # Extract style and color from end of words.
            style, color = words[-2:]
            words = words[:-2]

            # If there's anything left, that's the label.
            if words:
                xl = float(words[-2]) * scale
                yl = float(words[-1]) * scale
                label = ' '.join(words[:-2])
                if label.startswith('"') and label.endswith('"'):
                    label = label[1:-1]
            else:
                label = None

            dst.write("linethick = %f;\n"
                      % (0.5 if style == 'dotted' else 1.0))
            dst.write("spline -> from %f,%f" % xy[0])
            for x, y in xy:
                dst.write(" to %f,%f" % (x, y))
            dst.write('\n')

            if label:
                dst.write('"%s" at %f,%f\n' % (label, xl, yl))
        elif command == 'stop':
            break
        else:
            sys.stderr.write("%s\n" % command)
            assert False


options, args = getopt.gnu_getopt(sys.argv[1:], 'f:', [])

font_scale = 0
for key, value in options:
    if key == '-f':
        font_scale = int(value)
    else:
        raise False

if font_scale:
    print(".ps %+d" % -font_scale)

print(".PS")
print("linethick = 1;")
if args:
    for arg in args:
        dot2pic(open(arg), sys.stdout)
else:
    dot2pic(sys.stdin, sys.stdout)
if font_scale:
    print(".ps %+d" % font_scale)
print(".PE")

