aboutsummaryrefslogtreecommitdiff
path: root/bin/ak
blob: 539176a9d819e47887af6ed07a3f9f7e091d9a21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/bash
##
## Arching Kaos CLI tool is the main executable script to use for exploring,
## creating and distributing local blockchain(s) called zchain(s).
##
## It is accompanied with several utilities that help to create new blocks,
## reference them, export blocks to HTML pages, "roll-back" your zchain to an
## older block, reset your zchain and more.
##
##    -h, --help                  Prints this help message
##
## Run with no arguments to see available commands
##
fullprogrampath="$(realpath $0)"
PROGRAM="$(basename $0)"
descriptionString="Arching Kaos CLI"

source $AK_LIBDIR/_ak_log
source $AK_LIBDIR/_ak_script
source $AK_WORKDIR/rc

if [ $# -eq 0 ]
then
    logit "WARNING" "No command given"
    (
        find $AK_BINDIR | grep 'ak-' | while read available
        do
            #subcmd="$(basename $available | cut -d '-' -f 2)"
            subcmd="$(basename $available)"
            args="-h"
            if [ -n "$subcmd" ]
            then
                echo $subcmd | cut -d '-' -f 2 | sort | uniq | sed -e 's/^/ak /g'
                #$(echo $subcmd) $args
            fi
        done
    ) | sort | uniq
    exit 1
fi

if [ $# -eq 1 ]
then
    case "$1" in
        -h|--help)
            _ak_title_description
            _ak_help
            exit 1
            ;;
    esac
fi

subcmd="$(echo $* | sed -e 's/ /-/g')"

# Add functionality for separate modules directory
case "$1" in
    -m|--module)
        shift
        subcmd="$AK_MODULESDIR/$(echo $1 | sed -e 's/ /-/g')"
        shift
        if [ -z $* ] || [ ! -n "$*" ]
        then
            logit "ERROR" "No module selected"
            ls -1 $AK_MODULESDIR
            exit 1
        fi
        subargs="$*"
        $subcmd/main.sh $subargs
        _ak_exit_program $? "$subcmd module finished";
        ;;
esac

if [ -f "$AK_BINDIR/ak-$subcmd" ]
then
    $(echo ak-$subcmd)
    _ak_exit_program $? "ak-$subcmd command finished";
else
    argc=$#
    argv="$*"
    counter=1
    while [ $counter -lt $argc ]
    do
        subcmd="$(echo $argv | cut -d ' ' -f -$counter | sed -e 's/ /\-/g')"
        args="$(echo "$argv"| cut -d ' ' -f $(($counter + 1))-)"
        if [ -n "$subcmd" ] && [ -f "$AK_BINDIR/ak-$subcmd" ]
        then
            logit "INFO" "Running: ak-$subcmd with args: $args"
            $(echo ak-$subcmd) $args
            _ak_exit_program $? "ak-$subcmd command finished";
        fi
        counter=$(($counter + 1))
    done
    find $AK_BINDIR | while read available
    do
        echo $(basename $available) |\
            grep ak-$(echo $argv | cut -d ' ' -f 1) |\
            sed -e 's/-/ /g'
    done
    logit "ERROR" "Unknown subcommand: $*"
    exit 1
fi