#!/usr/bin/env python3
# Script to upload docs to gh-pages branch of dipy_web that will be
# automatically detected by the dipy website.
import os
from os import chdir as cd
import re
from subprocess import check_call
import sys


def sh(cmd):
    """Execute command in a subshell, return status code."""
    print("--------------------------------------------------")
    print(f"Executing: {cmd}")
    print("--------------------------------------------------")
    return check_call(cmd, shell=True)


# paths
docs_repo_path = "_build/docs_repo"
docs_repo_url = "https://github.com/dipy/dipy_web.git"

if __name__ == '__main__':
    # get current directory
    startdir = os.getcwd()

    # find the source version
    info_file = '../dipy/info.py'
    info_lines = open(info_file).readlines()
    source_version = '.'.join(
        [v.split('=')[1].strip(" '\n.")
         for v in info_lines if re.match(
            '^_version_(major|minor|micro|extra)',
            v)])
    print("Source version: ", source_version)

    # check for dev tag
    if source_version.split(".")[-1] == "dev":
        dev = True
        print("Development Version detected")
    else:
        dev = False

    # pull current docs_repo
    if not os.path.exists(docs_repo_path):
        print("docs_repo not found, pulling from git..")
        sh(f"git clone {docs_repo_url} {docs_repo_path}")
    cd(docs_repo_path)
    print(f"Moved to {os.getcwd()}")
    try:
        sh("git checkout gh-pages")
    except:
        while 1:
            print("\nLooks like gh-pages branch does not exist!")
            print("Do you want to create a new one? (y/n)")
            choice = str(input()).lower()
            if choice == 'y':
                sh("git checkout -b gh-pages")
                sh("rm -rf *")
                sh("git add .")
                sh("git commit -m 'cleaning gh-pages branch'")
                sh("git push origin gh-pages")
                break
            if choice == 'n':
                print("Please manually create a new gh-pages branch and try again.")
                sys.exit(0)
            else:
                print("Please enter valid choice ..")
    sh("git pull origin gh-pages")

    # check if docs for current version exists
    if os.path.exists(source_version) and not dev:
        print("docs for current version already exists")
    else:
        if dev:
            print("Re-building docs for development version")
        else:
            print("Building docs for a release")
        # build docs and copy to docs_repo
        cd(startdir)
        # remove old html and doctree files
        try:
            sh("rm -rf _build/json _build/doctrees")
        except:
            pass
        # generate new doc and copy to docs_repo
        sh("make api")
        sh("make rstexamples")
        sh("make json")
        sh(f"cp -r _build/json {docs_repo_path}/")
        cd(docs_repo_path)
        if dev:
            try:
                sh(f"rm -r {source_version}")
            except:
                pass
        sh(f"mv json {source_version}")
        sh("git add .")
        sh(f"git commit -m \"Add docs for {source_version}\"")
        sh("git push origin gh-pages")
