#!/usr/bin/python3
#

import os, sys
import csv
import subprocess as sp

HEADING = ["0", "45", "90", "135", "180", "225", "270", "315", "stat"];
#HEADING = ["0", "45", "90", "135", "180", "225", "270", "315"]

route_HDR = []
route_info = []

with open("routes.txt") as fp:
  fpcsv = csv.reader(fp, delimiter=",", quotechar='"')
  for row in fpcsv:
    if len(route_HDR)==0:
      route_HDR = row
      continue
    route_info.append(row);

for route_row in route_info:
  route_id = route_row[0]
  for heading in HEADING:
    print(route_id, heading)

    bub_width = (len(heading)*4) + 2 + 2
    bub_height = 9
    bw2 = int(bub_width/2)
    bub_offset = 0
    left_overflow = bw2 - 6;
    if (left_overflow < 0):
      left_overflow = 0
      bub_offset = 6 - 2 - 2

    tot_width = 32 + 2*left_overflow
    tot_height = 37+2

    base_icon = "bus_gw_" + heading + ".png"
    cmd = ["convert", "-page", str(tot_width) + "x" + str(tot_height) + "+" + str(left_overflow) + "+2", base_icon]
    cmd.append("-page")
    cmd.append("+" + str(bub_offset) + "+0")
    cmd.append("extra/bub_left.png")

    cur_offset = bub_offset + 2
    for idx in range(len(route_id)):
      cmd.append("-page")
      cmd.append("+" + str(cur_offset) + "+0")
      cmd.append("extra/bub_middle.png")
      cur_offset+=1

      cmd.append("-page")
      cmd.append("+" + str(cur_offset) + "+0")
      cmd.append("extra/bub_middle.png")
      cur_offset+=1

      cmd.append("-page")
      cmd.append("+" + str(cur_offset) + "+0")
      cmd.append("extra/bub_middle.png")
      cur_offset+=1

      cmd.append("-page")
      cmd.append("+" + str(cur_offset) + "+0")
      cmd.append("extra/bub_middle.png")
      cur_offset+=1

    cur_offset -=1

    cmd.append("-page")
    cmd.append("+" + str(cur_offset) + "+0")
    cmd.append("extra/bub_right.png")

    cur_offset = bub_offset + 2
    for idx in range(len(route_id)):
      p = cur_offset-1
      cmd.append("-page")
      cmd.append("+" + str(p) + "+2")
      cmd.append("extra/" + route_id[idx] + ".png")
      cur_offset += 4

    cmd.append("-background")
    cmd.append("none")
    cmd.append("-flatten")
    cmd.append("bus_route_icon/bus_gw_r" + route_id + "_" + heading + ".png")

    print(" ".join(cmd));
    sp.call(cmd)


