#!/usr/bin/env python3

import os
import re
import sys
import time
import math
import logging
import argparse
import subprocess
import numpy as np


from epics import caget, caput, camonitor, camonitor_clear, PV


############################################
#            README - HOW TO RUN           #
############################################
# Run on a dev server (e.g. lcls-dev3) as shown below:


############################################
#             Global variables             #
############################################

# Path to the first list of PVs
pvs1 = "" 

# Path to the second list of PVs
pvs2 = ""

############################################
#           Classes follow below           #
############################################

#************ ARG PARSER Class ************#
class ARG_PARSER:
    """
    
    This class will parse out the arguments.

    """

    def __init__(self):
        # List argument options
        print("Initializing ARG_PARSER\n")
        parser = argparse.ArgumentParser(description='Process test configuration.')
        parser.add_argument('--pvs1', dest='pvs1', default='', help='path to first PVs file (default: cpu-b084-sp18)')
        parser.add_argument('--pvs2', dest='pvs2', default='', help='path to second PVs file (default: sioc-b084-ts05)')
        
        # Parse out arguments
        args = parser.parse_args()
       
        # Set the cpu name
        global pvs1
        if args.pvs1:
            pvs1 = args.pvs1

        # Set the TPG prefix
        global pvs2
        if args.pvs2:
            pvs2 = args.pvs2


#************ Compare PVs Class ************#
class COMP_PVS:
    """
    
    This class will compare the two PV lists.

    """

    def __init__(self):
        print("Initializing COMP_PVS\n")
    
    def compare(self):
        # Use paths to the PV lists
        global pvs1, pv2
        # Open first file to read
        f1 = open(pvs1, 'r')
        lines1 = f1.readlines()
        f1.close()
        # Open second file to read
        f2 = open(pvs2, 'r')
        lines2 = f2.readlines()
        f2.close()
        # Open new file to write out comparison result
        f3 = open("comp.out", 'w')
        # Main loop starts here
        for i in range(len(lines1)):
          pv1  = lines1[i].strip()
          pv2  = lines2[i].strip()
          get1 = caget(pv1)
          get2 = caget(pv2)
          f3.write(pv1 + ' ' + pv2 + ' ==> ' + str(get1) + ' ' + str(get2) + '\n')
        f3.close()

############################################
#         __main__ block goes here         #
############################################
if __name__ == "__main__":
   
    # Create an arg parser object for user input 
    argparse = ARG_PARSER()

    # Create a comparison object to compare PVs
    comp = COMP_PVS()
    comp.compare()
