#!/usr/bin/env python3

import logging
import os
import shutil
import sys

import coloredlogs

coloredlogs.install(level="DEBUG", fmt="%(levelname)s %(message)s")
log = logging.getLogger()

MUSICS_FOLDER = os.path.join(os.path.expanduser("~"), "Musique")
BOF_FOLDER = os.path.join(os.path.expanduser("~"), ".MusiqueBof")

for f in sys.argv[1:]:
    src = os.path.realpath(f)
    if not os.path.isfile(src):
        log.error("{} does not exists".format(src))
        continue

    srcBase = None
    if src.startswith(MUSICS_FOLDER):
        srcBase = MUSICS_FOLDER
        dstBase = BOF_FOLDER
    elif src.startswith(BOF_FOLDER):
        srcBase = BOF_FOLDER
        dstBase = MUSIC_FOLDER
    else:
        log.error("{} not in any music folder".format(src))
        continue

    common = os.path.relpath(src, srcBase)
    dst = os.path.join(dstBase, common)
    dstFolder = os.path.dirname(dst)

    log.info("{} → {}".format(src, dst))
    os.makedirs(dstFolder, exist_ok=True)
    shutil.move(src, dst)