#!/usr/bin/env python3

# vi: set filetype=python

import sys
import time
import argparse
import subprocess

parser = argparse.ArgumentParser()
parser.add_argument('mode', choices=[
    'disable_apple_bce',
    'enable_apple_bce',
])

options = parser.parse_args()

if options.mode == 'disable_apple_bce':
    while True:
        ret = subprocess.call([
            'systemctl', 'stop', 'iwd',
        ])
        if ret != 0:
            time.sleep(1)
        else:
            break

    while True:
        ret = subprocess.call([
            'modprobe', '-r', 'brcmfmac_wcc',
        ])
        if ret != 0:
            time.sleep(1)
        else:
            break

    while True:
        ret = subprocess.call([
            'modprobe', '-r', 'brcmfmac',
        ])
        if ret != 0:
            time.sleep(1)
        else:
            break

    while True:
        ret = subprocess.call([
            'modprobe', '-r', 'applesmc',
        ])
        if ret != 0:
            time.sleep(1)
        else:
            break

    while True:
        ret = subprocess.call([
            'rmmod', '-f', 'apple-bce',
        ])
        #if ret != 0:
        #    time.sleep(1)
        #else:
        #    break
        break

elif options.mode == 'enable_apple_bce':
    while True:
        ret = subprocess.call([
            'modprobe', 'applesmc',
        ])
        if ret != 0:
            time.sleep(1)
        else:
            break

    while True:
        ret = subprocess.call([
            'modprobe', 'apple-bce',
        ])
        if ret != 0:
            time.sleep(1)
        else:
            break

    while True:
        ret = subprocess.call([
            'modprobe', 'brcmfmac',
        ])
        if ret != 0:
            time.sleep(1)
        else:
            break

    while True:
        ret = subprocess.call([
            'modprobe', 'brcmfmac_wcc',
        ])
        if ret != 0:
            time.sleep(1)
        else:
            break



    while True:
        ret = subprocess.call([
            'systemctl', 'start', 'iwd',
        ])
        if ret != 0:
            time.sleep(1)
        else:
            break
else:
    raise NotImplementedError
