diff options
Diffstat (limited to 'lib/_ak_script')
-rwxr-xr-x | lib/_ak_script | 160 |
1 files changed, 147 insertions, 13 deletions
diff --git a/lib/_ak_script b/lib/_ak_script index 81da347..02741cb 100755 --- a/lib/_ak_script +++ b/lib/_ak_script @@ -1,12 +1,31 @@ -#!/bin/bash -source $AK_LIBDIR/_ak_log +#!/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 ./lib/_ak_lib_load > /dev/null 2>&1 || source $AK_LIBDIR/_ak_lib_load > /dev/null 2>&1 +_ak_lib_load _ak_log # Wanna talk about it? -_ak_new_line(){ +function _ak_new_line(){ printf '\n' } -_ak_exit_program(){ +function _ak_exit_program(){ # Needs rework: # cd $curdir # if [ "$3" == "save_log" ] @@ -23,10 +42,10 @@ _ak_exit_program(){ exit $1 } -_ak_help(){ +function _ak_help(){ if [ ! -z $fullprogrampath ] && [ -n "$fullprogrampath" ] then - cat $fullprogrampath |grep '^##'| sed 's/^##//g;s/^ //g' 1>&2 + cat $fullprogrampath | grep -v '^###' | grep '^##'| sed 's/^##//g;s/^ //g' >&2 exit 1 else _ak_log_error "fullprogrampath was not set" @@ -34,7 +53,17 @@ _ak_help(){ fi } -_ak_title_description(){ +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 @@ -56,17 +85,40 @@ _ak_title_description(){ _ak_new_line ) 1>&2 } -_ak_usage(){ - _ak_title_description - _ak_help +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 } -_ak_not_implemented(){ +function _ak_not_implemented(){ #_ak_title_description _ak_log_error "Not implemented: $*" } -_ak_make_temp_directory(){ +function _ak_make_temp_directory(){ AK_TEMP="$(mktemp -d /tmp/aktmp-XXXXXXX)" if [ ! -d "$AK_TEMP" ] then @@ -77,7 +129,7 @@ _ak_make_temp_directory(){ echo "$AK_TEMP" } -_ak_make_temp_file(){ +function _ak_make_temp_file(){ AK_TEMP="$(mktemp /tmp/aktmp-XXXXXXX)" if [ ! -f "$AK_TEMP" ] then @@ -87,3 +139,85 @@ _ak_make_temp_file(){ _ak_log_info "File $AK_TEMP created successfully" echo "$AK_TEMP" } + +function _ak_check_and_create_dir(){ + if [ ! -z "$1" ] && [ -n "$1" ] && [ ! -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 +} + +function _ak_init_file_as_json_array(){ + if [ ! -f "$1" ] + then + printf "[]" > $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 +} + +function _ak_countdown_seconds(){ + default_countdown=5 + if [ ! -z "$1" ] && [ -n "$1" ] + then + if [ "$(echo -n $1 | sed -e 's/^[0-9]*$//g')" == "" ] + then + countdown=$1 + else + countdown=${default_countdown} + fi + else + countdown=${default_countdown} + fi + printf " %s" "$countdown" + countdown="$(expr $countdown - 1)" + sleep 1 + while [ $countdown -gt 0 ] + do + if [ $countdown -lt 10 ] + then + printf "\b\b %s" "$countdown" + else + printf "\b\b%s" "$countdown" + fi + countdown="$(expr $countdown - 1)" + sleep 1 + done + printf "\b\b starting!!!" + sleep 1 + printf "\n" +} + +# _ak_log_debug "_ak_script loaded $(caller)" |