aboutsummaryrefslogtreecommitdiff
path: root/lib/_ak_script
blob: 33d712b16536a3e6f0a16c58e96ace4a70aa3569 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env bash
###
### arching-kaos-tools
### Tools to interact and build an Arching Kaos Infochain
### Copyright (C) 2021 - 2025  kaotisk
###
### This program is free software: you can redistribute it and/or modify
### it under the terms of the GNU General Public License as published by
### the Free Software Foundation, either version 3 of the License, or
### (at your option) any later version.
###
### This program is distributed in the hope that it will be useful,
### but WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
### GNU General Public License for more details.
###
### You should have received a copy of the GNU General Public License
### along with this program.  If not, see <http://www.gnu.org/licenses/>.
###
source $AK_LIBDIR/_ak_log

# Wanna talk about it?
function _ak_new_line(){
    printf '\n'
}

function _ak_exit_program(){
# Needs rework:
#    cd $curdir
#    if [ "$3" == "save_log" ]
#    then
#        tar cvfz $curdir/$(basename $tempdir)-$launch_timestamp.tgz $tempdir
#    fi
#    rm -rf $tempdir
    if [ $1 -ne 0 ]
    then
        _ak_log_error "$2 ($1)"
    else
        _ak_log_exit "$2"
    fi
    exit $1
}

function _ak_help(){
    if [ ! -z $fullprogrampath ] && [ -n "$fullprogrampath" ]
    then
        cat $fullprogrampath | grep -v '^###' | grep '^##'| sed 's/^##//g;s/^ //g' >&2 #| \
        # cat | while read line
        # do
        #     echo -n "$line" | xxd
        #     _ak_log_info "$line"
        # done
        exit 1
    else
        _ak_log_error "fullprogrampath was not set"
        exit 1
    fi
}

function _ak_license(){
    if [ ! -z $fullprogrampath ] && [ -n "$fullprogrampath" ]
    then
        cat $fullprogrampath | grep '^###' | sed 's/^###//g;s/^ //g' >&2
    else
        _ak_log_error "fullprogrampath was not set"
        exit 1
    fi
}

function _ak_title_description(){
    if [ ! -n "$descriptionString" ]
    then
        # Choose to exit with error to enforce standard
        _ak_log_error "No description string"
        exit 1
        # Alternative solution
        # full_title="$(printf '%s' "$PROGRAM")"
    fi
    full_title="$(printf '%s - %s' "$PROGRAM" "$descriptionString")"
    delimiter_count=`echo -n $full_title | wc -c`
    (
    printf '%s' "$full_title"
    _ak_new_line
    while [ $delimiter_count -gt 0 ]
    do
        printf '='
        delimiter_count=$(($delimiter_count-1))
    done
    _ak_new_line ) 1>&2
}

function _ak_usage(){
    (
        _ak_title_description 2>&1
        _ak_license 2>&1
        _ak_help 2>&1
    ) | sed 's/^/# /g' | while read line; do  _ak_log_info "$line"; done
}

function _ak_print_version(){
    git_repo="$(ls -l $AK_LIBDIR/_ak_script | cut -d '>' -f 2 | sed 's/^ //' | rev | cut -d '/' -f 3- | rev)"
    patch_version="$(git --git-dir="${git_repo}/.git" -P log --reverse --oneline --decorate | tail -n 1 | cut -d ' ' -f 1)"
    gtags="$(git --git-dir="${git_repo}/.git" -P tag | tail -n 1)"
    if [ -f "$AK_WORKDIR/version" ]
    then
        cat $AK_WORKDIR/version
    elif [ -n "${gtags}" ]
    then
        echo "${gtags}"
    else
        echo "v0.0.0-${patch_version}"
    fi
}

function _ak_version(){
    printf '%s - ' "$PROGRAM"
    _ak_print_version 2>&1
}

function _ak_not_implemented(){
    #_ak_title_description
    _ak_log_error "Not implemented: $*"
}

function _ak_make_temp_directory(){
    AK_TEMP="$(mktemp -d /tmp/aktmp-XXXXXXX)"
    if [ ! -d "$AK_TEMP" ]
    then
        _ak_log_error "Could not make $AK_TEMP directory to work in"
        exit 1
    fi
    _ak_log_info "Directory $AK_TEMP created successfully"
    echo "$AK_TEMP"
}

function _ak_make_temp_file(){
    AK_TEMP="$(mktemp /tmp/aktmp-XXXXXXX)"
    if [ ! -f "$AK_TEMP" ]
    then
        _ak_log_error "Could not make $AK_TEMP file to work in"
        exit 1
    fi
    _ak_log_info "File $AK_TEMP created successfully"
    echo "$AK_TEMP"
}

function _ak_check_and_create_dir(){
    if [ ! -d "$1" ]
    then
        mkdir -p "$1"
        if [ $? -eq 0 ]
        then
            _ak_log_info "Directory $1 created!"
        else
            _ak_log_error "Problem occured while creating $1"
            exit 1
        fi
    else
        _ak_log_debug "$1 dir found"
    fi
}

function _ak_let_there_be_file(){
    if [ ! -f "$1" ]
    then
        touch "$1"
        if [ $? -eq 0 ]
        then
            _ak_log_info "File $1 created!"
        else
            _ak_log_error "Problem occured while creating $1"
            exit 1
        fi
    else
        _ak_log_debug "$1 file found"
    fi
}