#!/usr/bin/bash

config_dir=$HOME/.config/toolenv


__is_inside_toolbx(){
    if [[ -f /run/.containerenv && -f /run/.toolboxenv ]]; then
        return 0 # true
    else
        return 1 # false
    fi
}

__get_toolbx_name(){
    if __is_inside_toolbx; then
        toolbx_name=$(echo $(. /var/run/.containerenv; echo ${name}))
    fi
    echo $toolbx_name
}

__get_default_shell(){
    # values
    default_shell=$(basename $(echo $SHELL))

    echo $default_shell
}

__get_environment_dir(){
    # arguments
    toolbx_name=${1:-$(__get_toolbx_name)}

    env_path="$config_dir/$toolbx_name"
    echo $env_path
}

__get_environment_path(){
    # arguments
    toolbx_name=${1:-$(__get_toolbx_name)}
    shell=${2:-$(__get_default_shell)}

    # values
    env_dir=$(__get_environment_dir $toolbx_name)

    env_path="$env_dir/env.$shell"
    echo $env_path
}

help(){
    echo "toolenv 0.1"
    echo "A simple utility to customize toolbx environments"
    echo ""
    echo "Usage: toolenv [subcommand]"
    echo "Main subcommands:"
    echo "    create: creates a toolbx-specific environment file"
    echo "    edit: opens a toolbx-specific environment file with \$EDITOR"
    echo "    remove: removes a toolbx-specific environment file"
    echo "    list: lists all available environments"
    echo ""
    echo "Get help with each subcommand using the -h flag"
    echo "like so: toolenv create -h"
}

init(){
    # arguments
    shell=${1:-$(__get_default_shell)}

    if __is_inside_toolbx; then
        toolbx_name=$(__get_toolbx_name)
        env_path=$(__get_environment_path $toolbx_name $shell)
        if [[ -f $env_path ]]; then
            echo "source $env_path"
        fi
    fi
}

create(){
    # arguments
    toolbx_name=${1:-$(__get_toolbx_name)}
    shell=${2:-$(__get_default_shell)}

    # values
    env_path=$(__get_environment_path $toolbx_name $shell)
    env_dir=$(__get_environment_dir $toolbx_name)

    if [[ -z "$toolbx_name" ]]; then
        echo "Error: no toolbx specified"
        exit 1
    fi

    if [[ -z "$shell" ]]; then
        echo "Error: no shell specified"
        exit 1
    fi

    if ! [[ -d env_vir ]]; then
        mkdir -p $env_dir
    fi

    if ! [[ -f $env_path ]]; then
        touch $env_path
        echo "Created $shell environment for \"$toolbx_name\" toolbx"
    else
        echo "Error: $shell enviroment for \"$toolbx_name\" toolbx already created"
        exit 1
    fi
}

edit(){
    # arguments
    toolbx_name=${1:-$(__get_toolbx_name)}
    shell=${2:-$(__get_default_shell)}

    # values
    env_path=$(__get_environment_path $toolbx_name $shell)

    if [[ -z "$toolbx_name" ]]; then
        echo "Error: no toolbx specified"
        exit 1
    fi

    if [[ -z "$shell" ]]; then
        echo "Error: no shell specified"
        exit 1
    fi

    if ! [[ -f $env_path ]]; then
        echo "Error: no $shell enviroment for \"$toolbx_name\" toolbx"
        exit 1
    fi

    if [[ -z "$EDITOR" ]]; then
        echo "Error: \$EDITOR environment variable is not set"
        exit 1
    else
        $EDITOR $env_path
    fi
}

list(){
    for dir in $config_dir/*/ ; do
        container_name=${dir%*/}
        echo -n "${container_name##*/}: "
        find $dir -type f | sed -rn 's|.*/[^/]+\.([^/.]+)$|\1|p' | sort -u | xargs
    done
}

remove(){
    # arguments
    toolbx_name=${1:-$(__get_toolbx_name)}
    shell=${2:-$(__get_default_shell)}

    # values
    env_path=$(__get_environment_path $toolbx_name $shell)
    env_dir=$(__get_environment_dir $toolbx_name)

    if [[ -z "$toolbx_name" ]]; then
        echo "Error: no toolbx specified"
        exit 1
    fi

    if [[ -z "$shell" ]]; then
        echo "Error: no shell specified"
        exit 1
    fi

    if [[ -f $env_path ]]; then
        rm $env_path
        rmdir $env_dir
        echo "Removed $shell environment for \"$toolbx_name\" toolbx"
    else
        echo "Error: no $shell enviroment for \"$toolbx_name\" toolbx"
        exit 1
    fi
}

if [[ $1 =~ ^(help|init|create|edit|list|remove)$ ]]; then
    "$@"
else
    echo "Invalid subcommand $1" >&2
    exit 1
fi

