#!/usr/bin/python

import argparse
import textwrap
from simulation import Simulation


def main(numframes, algorithm, refresh, tau, tracefile):
    """
    Run the main Simulation with the command line parameters
    """
    sim = Simulation(tracefile, algorithm, numframes, refresh, tau)
    sim.run()
    print(sim)

if __name__ == '__main__':
    # Argument parsing
    parser = argparse.ArgumentParser(
        prog='vmsim',
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description=textwrap.dedent('''\
            Virtual Memory Simulator
            ------------------------
            Virtual Memory Simulator for CS 1550
        '''))
    parser.add_argument('-n', '--numframes', type=int,
                        help='Number of frames available')
    parser.add_argument('-a', '--algorithm', type=str,
                        choices=['opt', 'clock', 'nru', 'work'],
                        help='The replacement algorithm to use')
    parser.add_argument('-r', '--refresh', type=int,
                        help='Refresh rate')
    parser.add_argument('-t', '--tau', type=int,
                        help='Value for Tau')
    parser.add_argument('tracefile', type=str,
                        help='The file to trace')
    args = parser.parse_args()

    # Error handling, to ensure the parameters we need exist
    if args.algorithm is None:
        raise ValueError('You need to specify an algorithm to run')
    if args.numframes is None:
        raise ValueError('You need to specify the number of page frames')
    if args.tracefile is None:
        raise ValueError('You need to specify the file to read from')

    # Invoke the main program with our parameters
    main(numframes=args.numframes,
         algorithm=args.algorithm,
         refresh=args.refresh,
         tau=args.tau,
         tracefile=args.tracefile)
