#!/usr/bin/env python3
import sys
import os
import argparse
from pathlib import Path
import shutil

profiles = Path(__file__).parent.parent / 'jupyter'
target = Path('~/.jupyter/lab/user-settings/@jupyterlab').expanduser()

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("profile")
    args = parser.parse_args()
    selected = str(profiles / args.profile)

    if not Path(selected).exists():
        available = [
            repr(dir_) for dir_ in os.listdir(profiles)
            if not dir_.startswith('.')
        ]

        message = ', '.join(available)

        sys.exit(f'Profile {args.profile!r} does not exist. '
                 f'Choose one from: {message}')

    try:
        shutil.rmtree(target)
    except OSError:
        Path(target).unlink()

    os.system(f'ln -s {selected} {target}')

    print(f'Created symlink: {target} -> {selected}')