/ lib / _ak_script
_ak_script
  1  #!/usr/bin/env bash
  2  ###
  3  ### arching-kaos-tools
  4  ### Tools to interact and build an Arching Kaos Infochain
  5  ### Copyright (C) 2021 - 2026  kaotisk
  6  ###
  7  ### This program is free software: you can redistribute it and/or modify
  8  ### it under the terms of the GNU General Public License as published by
  9  ### the Free Software Foundation, either version 3 of the License, or
 10  ### (at your option) any later version.
 11  ###
 12  ### This program is distributed in the hope that it will be useful,
 13  ### but WITHOUT ANY WARRANTY; without even the implied warranty of
 14  ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15  ### GNU General Public License for more details.
 16  ###
 17  ### You should have received a copy of the GNU General Public License
 18  ### along with this program.  If not, see <http://www.gnu.org/licenses/>.
 19  ###
 20  source ./lib/_ak_lib_load > /dev/null 2>&1 || source $AK_LIBDIR/_ak_lib_load > /dev/null 2>&1
 21  _ak_lib_load _ak_log
 22  
 23  # Wanna talk about it?
 24  function _ak_new_line(){
 25      printf '\n'
 26  }
 27  
 28  function _ak_exit_program(){
 29  # Needs rework:
 30  #    cd $curdir
 31  #    if [ "$3" == "save_log" ]
 32  #    then
 33  #        tar cvfz $curdir/$(basename $tempdir)-$launch_timestamp.tgz $tempdir
 34  #    fi
 35  #    rm -rf $tempdir
 36      if [ $1 -ne 0 ]
 37      then
 38          _ak_log_error "$2 ($1)"
 39      else
 40          _ak_log_exit "$2"
 41      fi
 42      exit $1
 43  }
 44  
 45  function _ak_help(){
 46      if [ ! -z $fullprogrampath ] && [ -n "$fullprogrampath" ]
 47      then
 48          cat $fullprogrampath | grep -v '^###' | grep '^##'| sed 's/^##//g;s/^ //g' >&2
 49          exit 1
 50      else
 51          _ak_log_error "fullprogrampath was not set"
 52          exit 1
 53      fi
 54  }
 55  
 56  function _ak_license(){
 57      if [ ! -z $fullprogrampath ] && [ -n "$fullprogrampath" ]
 58      then
 59          cat $fullprogrampath | grep '^###' | sed 's/^###//g;s/^ //g' >&2
 60      else
 61          _ak_log_error "fullprogrampath was not set"
 62          exit 1
 63      fi
 64  }
 65  
 66  function _ak_title_description(){
 67      if [ ! -n "$descriptionString" ]
 68      then
 69          # Choose to exit with error to enforce standard
 70          _ak_log_error "No description string"
 71          exit 1
 72          # Alternative solution
 73          # full_title="$(printf '%s' "$PROGRAM")"
 74      fi
 75      full_title="$(printf '%s - %s' "$PROGRAM" "$descriptionString")"
 76      delimiter_count=`echo -n $full_title | wc -c`
 77      (
 78      printf '%s' "$full_title"
 79      _ak_new_line
 80      while [ $delimiter_count -gt 0 ]
 81      do
 82          printf '='
 83          delimiter_count=$(($delimiter_count-1))
 84      done
 85      _ak_new_line ) 1>&2
 86  }
 87  
 88  function _ak_usage(){
 89      if [ ! -z $1 ] && [ -n "$1" ] && [ "$1" == "err" ]
 90      then
 91          (
 92              _ak_title_description 2>&1
 93              _ak_license 2>&1
 94              _ak_help 2>&1
 95          # ) | sed 's/^/# /g' | while read line; do _ak_log_info "${line}"; done
 96          ) 1>&2
 97          exit 1
 98      else
 99          (
100              _ak_title_description 2>&1
101              _ak_license 2>&1
102              _ak_help 2>&1
103          # ) | sed 's/^/# /g' | while read line; do _ak_log_info "${line}" 2>&1; done
104          )
105      fi
106  }
107  
108  function _ak_print_version(){
109      git_repo="$(ls -l $AK_LIBDIR/_ak_script | cut -d '>' -f 2 | sed 's/^ //' | rev | cut -d '/' -f 3- | rev)"
110      patch_version="$(git --git-dir="${git_repo}/.git" -P log --reverse --oneline --decorate | tail -n 1 | cut -d ' ' -f 1)"
111      gtags="$(git --git-dir="${git_repo}/.git" -P tag | tail -n 1)"
112      if [ -f "$AK_WORKDIR/version" ]
113      then
114          cat $AK_WORKDIR/version
115      elif [ -n "${gtags}" ]
116      then
117          echo "${gtags}"
118      else
119          echo "v0.0.0-${patch_version}"
120      fi
121  }
122  
123  function _ak_version(){
124      printf '%s - ' "$PROGRAM"
125      _ak_print_version 2>&1
126  }
127  
128  function _ak_not_implemented(){
129      #_ak_title_description
130      _ak_log_error "Not implemented: $*"
131  }
132  
133  function _ak_make_temp_directory(){
134      AK_TEMP="$(mktemp -d /tmp/aktmp-XXXXXXX)"
135      if [ ! -d "$AK_TEMP" ]
136      then
137          _ak_log_error "Could not make $AK_TEMP directory to work in"
138          exit 1
139      fi
140      _ak_log_info "Directory $AK_TEMP created successfully"
141      echo "$AK_TEMP"
142  }
143  
144  function _ak_make_temp_file(){
145      AK_TEMP="$(mktemp /tmp/aktmp-XXXXXXX)"
146      if [ ! -f "$AK_TEMP" ]
147      then
148          _ak_log_error "Could not make $AK_TEMP file to work in"
149          exit 1
150      fi
151      _ak_log_info "File $AK_TEMP created successfully"
152      echo "$AK_TEMP"
153  }
154  
155  function _ak_check_and_create_dir(){
156      if [ ! -z "$1" ] && [ -n "$1" ] && [ ! -d "$1" ]
157      then
158          mkdir -p "$1"
159          if [ $? -eq 0 ]
160          then
161              _ak_log_info "Directory $1 created!"
162          else
163              _ak_log_error "Problem occured while creating $1"
164              exit 1
165          fi
166      else
167          _ak_log_debug "$1 dir found"
168      fi
169  }
170  
171  function _ak_let_there_be_file(){
172      if [ ! -f "$1" ]
173      then
174          touch "$1"
175          if [ $? -eq 0 ]
176          then
177              _ak_log_info "File $1 created!"
178          else
179              _ak_log_error "Problem occured while creating $1"
180              exit 1
181          fi
182      else
183          _ak_log_debug "$1 file found"
184      fi
185  }
186  
187  function _ak_init_file_as_json_array(){
188      if [ ! -f "$1" ]
189      then
190          printf "[]" > $1
191          if [ $? -eq 0 ]
192          then
193              _ak_log_info "File $1 created!"
194          else
195              _ak_log_error "Problem occured while creating $1"
196              exit 1
197          fi
198      else
199          _ak_log_debug "$1 file found"
200      fi
201  }
202  
203  function _ak_countdown_seconds(){
204      default_countdown=5
205      if [ ! -z "$1" ] && [ -n "$1" ]
206      then
207          if [ "$(echo -n $1 | sed -e 's/^[0-9]*$//g')" == "" ]
208          then
209              countdown=$1
210          else
211              countdown=${default_countdown}
212          fi
213      else
214          countdown=${default_countdown}
215      fi
216      printf " %s" "$countdown"
217      countdown="$(expr $countdown - 1)"
218      sleep 1
219      while [ $countdown -gt 0 ]
220      do
221          if [ $countdown -lt 10 ]
222          then
223              printf "\b\b %s" "$countdown"
224          else
225              printf "\b\b%s" "$countdown"
226          fi
227          countdown="$(expr $countdown - 1)"
228          sleep 1
229      done
230      printf "\b\b starting!!!"
231      sleep 1
232      printf "\n"
233  }
234  
235  # _ak_log_debug "_ak_script loaded $(caller)"