/ lib / _ak_pkg
_ak_pkg
  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 $AK_LIBDIR/_ak_lib_load
 21  _ak_lib_load _ak_log
 22  _ak_lib_load _ak_script
 23  _ak_lib_load _ak_fs
 24  _ak_lib_load _ak_gpg
 25  _ak_lib_load _ak_zblock
 26  
 27  function _ak_pkg_all_modules_make_release_from_local_installation(){
 28      outdir="$(pwd)"
 29      find $AK_MODULESDIR -type l | xargs ls -l  | rev | cut -d ' ' -f 1 | rev | while read mod
 30      do
 31          mod_root="$(echo $mod | rev | cut -d '/' -f 2- | rev)"
 32          mod_name="$(basename $mod)"
 33          cd ${mod_root}/${mod_name}
 34          if [ ! -f version ]
 35          then
 36              echo -n 'v0.0.0' > version
 37          fi
 38          mod_version="$(cat version)"
 39          if [ ! -f ${outdir}/${mod_name}-${mod_version}.tar.gz ]
 40          then
 41              tar cfz ${outdir}/${mod_name}-${mod_version}.tar.gz *
 42          fi
 43      done
 44  }
 45  
 46  function _ak_pkg_all_modules_make_release_from_local_installation(){
 47      outdir="$(pwd)"
 48      select mod in $(find $AK_MODULESDIR -type l | xargs ls -l  | rev | cut -d ' ' -f 1 | rev | tr '\n' ' ') # | while read mod
 49      do
 50          mod_root="$(echo $mod | rev | cut -d '/' -f 2- | rev)"
 51          mod_name="$(basename $mod)"
 52          cd ${mod_root}/${mod_name}
 53          if [ ! -f version ]
 54          then
 55              echo -n 'v0.0.0' > version
 56          fi
 57          mod_version="$(cat version)"
 58          outfile="${mod_name}-${mod_version}.tar.gz"
 59          fout="${outdir}/${outfile}"
 60          if [ ! -f ${fout} ]
 61          then
 62              tar cfz ${fout} *
 63              cd ${outdir}
 64              _ak_gpg_sign_detached ${outfile}.asc ${outfile}
 65              akfsmap="$(_ak_fs_import ${outfile})"
 66              akfsmapsig="$(_ak_fs_import ${outfile}.asc)"
 67              cat > data << EOF
 68  {
 69      "package":"$mod_name",
 70      "version":"$mod_version",
 71      "akfsmap":"$akfsmap",
 72      "detach":"$akfsmapsig"
 73  }
 74  EOF
 75              _ak_zblock_pack pkg/add data
 76          else
 77              tfile="$(_ak_make_temp_file).tar.gz"
 78              tar cfz ${tfile} *
 79              hasht="$(sha512sum ${tfile} | cut -d ' ' -f 1)"
 80              hashf="$(sha512sum ${fout} | cut -d ' ' -f 1)"
 81              if [ "$hashf" != "$hasht" ]
 82              then
 83                  _ak_log_warning "Please manually pick-up your archive from ${tfile} or delete the copy on your directory and rerun."
 84                  exit 5
 85              else
 86                  _ak_log_info "You already have latest release"
 87                  exit 0
 88              fi
 89          fi
 90          break
 91      done
 92  }
 93  
 94  function _ak_pkg_all_modules_make_release_from_local_directory(){
 95      outdir="$(pwd)"
 96      if [ ! -z $1 ] && [ -n "$1" ] && [ -d "$1" ]
 97      then
 98          mod="$(realpath $1)"
 99          mod_root="$(echo $mod | rev | cut -d '/' -f 2- | rev)"
100          mod_name="$(basename $mod)"
101          cd ${mod_root}/${mod_name}
102          if [ ! -f version ]
103          then
104              echo -n 'v0.0.0' > version
105          fi
106          mod_version="$(cat version)"
107          outfile="${mod_name}-${mod_version}.tar.gz"
108          fout="${outdir}/${outfile}"
109          if [ ! -f ${fout} ]
110          then
111              tar cfz ${fout} *
112              cd ${outdir}
113              _ak_gpg_sign_detached ${outfile}.asc ${outfile}
114              akfsmap="$(_ak_fs_import ${outfile})"
115              akfsmapsig="$(_ak_fs_import ${outfile}.asc)"
116              cat > data << EOF
117  {
118      "package":"$mod_name",
119      "version":"$mod_version",
120      "akfsmap":"$akfsmap",
121      "detach":"$akfsmapsig"
122  }
123  EOF
124              _ak_zblock_pack pkg/add data
125          else
126              tfile="$(_ak_make_temp_file).tar.gz"
127              tar cfz ${tfile} *
128              hasht="$(sha512sum ${tfile} | cut -d ' ' -f 1)"
129              hashf="$(sha512sum ${fout} | cut -d ' ' -f 1)"
130              if [ "$hashf" != "$hasht" ]
131              then
132                  _ak_log_warning "Please manually pick-up your archive from ${tfile} or delete the copy on your directory and rerun."
133                  exit 5
134              else
135                  _ak_log_info "You already have latest release"
136                  exit 0
137              fi
138          fi
139          break
140      else
141          _ak_log_error "You need to provide an existing directory as an argument"
142          exit 1
143      fi
144  }
145  
146  function _ak_pkg_install_from_akfsmap(){
147      TEMP="$(_ak_make_temp_directory)"
148      cd $TEMP
149      filename="$(_ak_fs_from_map_net_get_original_filename $1)"
150      _ak_fs_net_get_from_map_hash $1
151      module_name="$(echo -n $filename | cut -d '-' -f 1)"
152      module_version="$(echo -n $filename | sed -e 's/\.tar\.gz//g' | rev | cut -d '-' -f 1 | rev)"
153      if [ -d $AK_MODULESDIR/$module_name ] || [ -L $AK_MODULESDIR/$module_name ]
154      then
155          _ak_log_error "$module_name seems already installed"
156          if [ -f $AK_MODULESDIR/$module_name/version ]
157          then
158              imv=$(cat $AK_MODULESDIR/$module_name/version)
159              if [ "$filename" == "${module_name}-${imv}.tar.gz" ]
160              then
161                  _ak_log_debug "and in latest version: ${imv}"
162                  exit 1
163              else
164                  _ak_log_debug "version mismatch: attempted to install $module_version but found $imv installed"
165                  exit 1
166              fi
167          fi
168      else
169          _ak_log_error "$module_name doesn't seem installed"
170          mkdir -p $AK_MODULESDIR/$module_name
171          if [ $? -ne 0 ]
172          then
173              _ak_log_error "Something went wrong when making directory"
174              exit 1
175          fi
176          tar -C $AK_MODULESDIR/$module_name -xf $TEMP/$filename
177          if [ $? -ne 0 ]
178          then
179              _ak_log_error "$TEMP Something went wrong while extracting $filename"
180              exit 1
181          fi
182          _ak_log_info "$filename was installed successfully!"
183          _ak_log_info "Enter: ak -m $module_name --help for help"
184          rm -rf $TEMP
185          exit 0
186      fi
187      exit 224
188  }
189  
190  function _ak_pkg_install_from_zblock(){
191      if [ ! -z $1 ] && [ -n "$1" ]
192      then
193          tempzblock="$(_ak_make_temp_file)"
194          _ak_zblock_show $1 | jq > $tempzblock
195          if [ $? -ne 0 ]
196          then
197              _ak_log_error "Something must happened wrong"
198              exit 1
199          fi
200          tzmod="$(cat $tempzblock|jq -r '.module')"
201          tzdata="$(cat $tempzblock|jq -r '.data')"
202          tzakfsmap="$(cat $tempzblock|jq -r '.'$tzdata'.akfsmap')"
203          _ak_pkg_install_from_akfsmap $tzakfsmap
204      else
205          _ak_log_error "No zblock hash provided"
206          exit 1
207      fi
208  }
209  
210  function _ak_pkg_uninstall(){
211      select x in $(ls -1 $AK_MODULESDIR | tr '\n' ' ')
212      do
213          if [ -n "$x" ]
214          then
215              if [ -d $AK_MODULESDIR/$x ] || [ -L $AK_MODULESDIR/$x ]
216              then
217                  _ak_log_info "Removing module $x..."
218                  rm -rf $AK_MODULESDIR/$x
219                  if [ $? -ne 0 ]
220                  then
221                      _ak_log_error "Failed to remove module $x"
222                      exit 1
223                  fi
224                  _ak_log_info "$x module was uninstalled!"
225              fi
226              break
227          fi
228      done
229  }
230  
231  _ak_log_debug "_ak_pkg loaded $(caller)"