#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# File: performance
# Author: Lorenz Stechauner <e12119052@student.tuwien.ac.at>
#         Lorenz Stechauner <lorenz.stechauner@necronda.net>

import argparse

import intercept
import intercept.standard


MODE: str


class Handler(intercept.standard.Handler):
    def before_pipe(self, fildes_ptr: intercept.Pointer) -> str:
        return 'return 0' if MODE == 'return' else 'ok'

    def after_pipe(self, fildes_ptr: intercept.Pointer,
                   ret_value: int, errno: str = None, fildes: list[int] = None) -> None:
        pass

    def before_close(self, fildes: int) -> str:
        return 'return 0' if MODE == 'return' else 'ok'

    def after_close(self, fildes: int,
                    ret_value: int, errno: str = None) -> None:
        pass


def main() -> None:
    global MODE
    parser = argparse.ArgumentParser()
    parser.add_argument('-m', '--mode', choices=['ok', 'return'])
    args = parser.parse_args()
    MODE = args.mode

    socket_name = f'/tmp/intercept.performance.{MODE}.sock'
    intercept.intercept(socket_name, Handler)


if __name__ == '__main__':
    main()
