#!/usr/bin/env bash

# Colors we load in
TOOLPATH=$(python3 -c 'from dragon.util import tool_path; print(tool_path())')
source $TOOLPATH/dragoncolors

drexit_reason()
{
  prefix_print $1
  drexit
}

drexit()
{
    # This function resets terminal colors, fixes stty (just in case), and then exits with arg0's value
    # It should be called on *EVERY* exit, no matter what.
    echo -e "${NC}"
    stty sane
    python3 -m dragon.update_check
    exit $1
}

cleanbuildfail()
{
    # Run this when we hit an error building, so we can clean things up.
    # This needs to eventually be updated for the new {name}.ninja format
    # Currently it'll just leave everything sitting out

    # shellcheck disable=SC2154
    prefix_print "Build failed"
    prefix_print "Cleaning Up"
    find . -name '.clean' -type f -delete
    if [[ $norm -eq 1 ]]; then
        mkdir -p $DRAGON_DIR/ninja && mv build.ninja $DRAGON_DIR/ninja/build.ninja
        if ! [[ -z $1 ]]; then
            cd $1 || drexit
            mkdir -p $DRAGON_DIR/ninja && mv build.ninja $DRAGON_DIR/ninja/build.ninja
        fi
    else
        find . -type f -name "*.ninja" -delete
    fi
    drexit 1
}

setupRemote()
{
    # remote.py | remote.sh
    prefix_print "Enter Server IP:"
    read IPA
    mkdir -p $DRAGON_ROOT_DIR/state
    echo "export IPA=\"$IPA\"" > $DRAGON_ROOT_DIR/state/remoteip
    prefix_print "Enter Username:"
    read USERN
    if [[ -z $USERN ]]; then
        USERN="root"
    fi
    echo "export USERN=\"$USERN\"" >> $DRAGON_ROOT_DIR/state/remoteip
}

update()
{
    # update.sh
    hold="$PWD"
    cd $DRAGON_ROOT_DIR || drexit
    git pull
    git submodule update --init --recursive
    cd "$hold" || drexit
}

usage()
{
    echo -e ""
    echo -e "${PrefixColor}dragon v${DRAGON_VERS} ${BoldColor}-=-=-${NC}"
    echo -e "  usage: dragon [commands]"
    echo ""
    echo -e "${PrefixColor}Building ${BoldColor}-=-=-${NC}"
    echo -e "  ${PackageColor}n${NC}  | new|nic|edit    - ${BoldColor}Open the project editor (For creating new projects/adding to existing ones)${NC}"
    echo -e "  ${PackageColor}do${NC}                   - ${BoldColor}Build and Install${NC}"
    echo -e "  ${PackageColor}c${NC}  | clean           - ${BoldColor}Clear build cache${NC}"
    echo -e "  ${PackageColor}b${NC}  | build|make      - ${BoldColor}Compile, link, and package your project${NC}"
    echo -e "  ${PackageColor}g${NC}  | gen|generate    - ${BoldColor}Create build files without actually building${NC}"
    echo ""
    echo -e "${PrefixColor}Configuration ${BoldColor}-=-=-${NC}"
    echo -e "  ${PackageColor}r${NC}   | release        - ${BoldColor}Create a release build (defines NDEBUG, enables 'releaseflags' value)${NC}"
    echo -e "  ${PackageColor}ro${NC}  | rootless       - ${BoldColor}Build for rootless package scheme${NC}"
    # Debug commands. We should print them _somewhere_ but not here :thonk:
    # echo -e "  ${PackageColor}ddebug${NC}               - ${BoldColor}Enable verbose debug logs${NC}"
    # echo -e "  ${PackageColor}vn${NC}                   - ${BoldColor}Enable verbose ninja(-build)${NC}"
    # echo -e "  ${PackageColor}vd${NC}                   - ${BoldColor}Enable verbose dragon${NC}"
    # echo -e "  ${PackageColor}vg${NC}                   - ${BoldColor}Enable verbose dragongen${NC}"
    # echo -e "  ${PackageColor}norm${NC}                 - ${BoldColor}Retain build.ninja after building${NC}"
    echo ""
    echo "Common invocations are 'dragon c b i' (clean all, build, install) or 'dragon do' (build and install)"
    echo ""
    echo -e "${PrefixColor}Distribution ${BoldColor}-=-=-${NC}"
    echo -e "  ${PackageColor}i${NC}   | install        - ${BoldColor}Install latest build to build device${NC}"
    echo -e "  ${PackageColor}u${NC}   | uninstall      - ${BoldColor}Uninstall latest build from build device${NC}"
    echo -e "  ${PackageColor}sn${NC}  | send <file>    - ${BoldColor}Install an arbitrary .deb to the device${NC}"
    echo -e "  ${PackageColor}sim${NC} | simulator      - ${BoldColor}Install to the simulator instead of a device (e.g. dragon c b i sim)${NC}"
    echo ""
    echo -e "${PrefixColor}Device Management ${BoldColor}-=-=-${NC}"
    echo -e "  ${PackageColor}s${NC}  | device          - ${BoldColor}Set build device IP/Port${NC}"
    echo -e "  ${PackageColor}rs${NC} | respring        - ${BoldColor}Respring the current build device${NC}"
    echo -e "  ${PackageColor}dr${NC} | devicerun       - ${BoldColor}Run anything after this flag on device${NC}"
    echo ""
    echo -e "${PrefixColor}Tools ${BoldColor}-=-=-${NC}"
    echo -e "  ${PackageColor}lo${NC}  | objcs          - ${BoldColor}Configure llvm-objcs for use with dragon${NC}"
    echo -e "  ${PackageColor}debug [Process]${NC}      - ${BoldColor}Start a debugging server on device and connect to it (Can be used with the install flag as well)${NC}"
    echo -e "  ${PackageColor}exp${NC} | export         - ${BoldColor}Tell ninja to create a compile_commands.json${NC}"
    # echo -e "  ${PackageColor}sr${NC}  | rconf          - ${BoldColor}Setup remote project build${NC}"
    # echo -e "  ${PackageColor}test${NC}                 - ${BoldColor}Run the test suite${NC}"
    # echo -e "  ${PackageColor}time${NC}                 - ${BoldColor}Print time for each bash subcommand to run${NC}"
    echo ""
    echo -e "${PrefixColor}Misc ${BoldColor}-=-=-${NC}"
    echo -e "  ${PackageColor}up${NC}  | update|upgrade - ${BoldColor}Update dragon${NC}"
    echo -e "  ${PackageColor}v${NC} | -v               - ${BoldColor}Prints version information${NC}"
    echo -e "  ${PackageColor}h${NC} | -h|help          - ${BoldColor}Prints this help page${NC}"
    echo ""
    echo -e "${PrefixColor}-=-=-${NC}"
    echo ""
    echo "dragon v${DRAGON_VERS}"
    echo "created by cynder, made possible through crucial contributions from Lorenzo Pane, Lightmann"
    echo "with contributions from: iCrazeiOS, Squidkingdom, Amy While, Conor, MrGcGamer, jaidan, Diego Magdaleno"
    # <3
}
