import argparse
import csv

def main():
  parser = argparse.ArgumentParser(description="Generates a Digikey BOM from Joost's KiCAD BOM")
  parser.add_argument("input", help="input BOM")
  parser.add_argument("output", help="output BOM")
  parser.add_argument("--m", help="quantity multiplier", default="1")

  args = parser.parse_args()
  multiplier = 1 if args.m is None else int(args.m)

  print("multiplier: {}".format(multiplier))

  digikey_idx = None
  count_idx = None
  components = []
  with open(args.input, 'r') as f:
    reader = csv.reader(f)
    parts = next(reader)
    for i, part in enumerate(parts):
      if 'Digikey' == part:
        digikey_idx = i
      elif 'Quantity' == part:
        count_idx = i

    if digikey_idx is None or count_idx is None:
      print("Unable to find headers")
      return

    for i, parts in enumerate(reader):
      if len(parts) < max(digikey_idx, count_idx):
        print("[WARN] line {} is missing fields".format(i + 1))
        continue
      part_num = parts[digikey_idx]
      count = parts[count_idx]
      if len(part_num) == 0:
        print("[WARN] line {} is missing part number".format(i + 1))
        print(parts)
        continue
      if count == 0:
        print("[WARN] line {} has zero quantity".format(i + 1))
      if part_num == 'NoPart':
        continue
      components.append((part_num, count))

  with open(args.output, 'w') as f:
    # header is necessary if you don't want to miss the first part
    f.write("Digikey,Quantity\n")
    for c in components:
      f.write("{},{}\n".format(c[0], multiplier*int(c[1])))

if __name__ == "__main__":
  main()
