## ## ** cle-mod: CLE module management ** # #* version: 2021-10-11 #* author: Michael Arbet (marbet@redhat.com) #* home: https://github.com/micharbet/CLE #* license: GNU GPL v2 #* Copyright (C) 2016-2021 by Michael Arbet # # 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 2 # of the License, or (at your option) any later version. # local INDEXFN=modulist local INDEXFILE=$CLE_D/$INDEXFN # variable used only in this script local MOD MODS DMOD MUPD MSTAT # # cle-mod helper functions # # check module status: installed/upgradable/custom _clemodstat () ( if [ -f $CLE_D/$1 ]; then SUMM=`md5sum $CLE_D/$1 | cut -d' ' -f1` SUMI=`sed -n "/^$1:/s/$1:[^:]*:\([^:]*\):.*/\1/p" $INDEXFILE` if [ -z "$SUMI" ]; then # custom module, not found in index echo C elif [ $SUMI = $SUMM ]; then # installed echo I else # installed, upgradable echo U fi else # no such module here echo fi ) # extract module description _clemodesc () ( if [ -f $CLE_D/$1 ]; then # get description from module directly sed -n "/^##/s/.*$(basename $1):\s*\([^*]*\)\**/\1/p" $CLE_D/$1 else # get description from index file sed -n "/^$1:/s/$1:.*:\(.*\)/\1/p" $INDEXFILE fi ) # extract module version _clemodver () ( if [ -f $CLE_D/$1 ]; then # get version from module directly sed -n "s/^#\* version:\s*//p" $CLE_D/$1 else # get version from index file sed -n "/^$1:/s/$1:\([^:]*\):.*/\1/p" $INDEXFILE fi ) # print module deails _clemodet () ( MDESC=`_clemodesc $1` MSTAT=`_clemodstat $1` MVER=`_clemodver $1` printf "$_CN [%1s] $_CL%-15s $_CN$_CD %10s $_CN %s\n" "$MSTAT" "$1" "$MVER" "$MDESC" ) # get list of installed and available modules _clemodlist () { pushd $CLE_D >/dev/null INST=`ls cle-* mod-* bin-* 2>/dev/null` AVAIL=`cut -d: -f1 $INDEXFILE` echo "$INST $AVAIL" | tr ' ' '\n' | sort | uniq popd >/dev/null } # download list of available modules _clemodindex () ( curl -ksS $CLE_SRC/modules/$INDEXFN >$INDEXFILE.tmp MLINE=`head -1 $INDEXFILE.tmp` # check if the file appears like module index if [[ $MLINE =~ ..*:.*:.*:.* ]]; then mv -f $INDEXFILE.tmp $INDEXFILE else rm -f $INDEXFILE.tmp echo Module list could not be downloaded from $CLE_SRC return 1 fi ) # install module _clemodadd () { DMOD=$CLE_D/dl-$1 curl -ksS $CLE_SRC/modules/$1 >$DMOD # check module signature # every regular cle module should have at least it's identification strings # (comments at the beginning, see this file or mod-example) if grep -q "^##* *.*$1: ..*" $DMOD ; then mv -f $DMOD $CLE_D/$1 if [[ $1 =~ 'mod-' ]]; then # now include the module _clexe $CLE_D/$1 fi _clemodet $1 else # either download error or this is no module echo $1 download failed! rm -f $DMOD return 1 fi } # # cle mod script main # case "$1" in ls) ## `cle mod ls` - list modules _clebold "Repository: $_CN$CLE_SRC" _clebold "Local store: $_CN$CLE_D" _clemodindex echo for MOD in `_clemodlist`; do _clemodet $MOD done ;; add) ## `cle mod add [mod]` - install module from repository #_clemodindex || return $? # get matching modules _clemodindex MODS=`sed -n "/[^:]*$2[^:]*/s/\([^:]*\):.*/\1/p" $INDEXFILE` [ "$MODS" ] || { echo Nothing like $2 to install; return 1; } MODN=`wc -w <<<$MODS` if [ $MODN -gt 1 ]; then # more matches, choose one PS3="$_CL choose module # $_CN" select MOD in $MODS; do [ -n "$MOD" ] && break done else # exactly one module MOD=$MODS fi [ $MOD ] || return # show selected module details echo _clemodet $MOD _cleask "Do you want to install this module?" || return _clemodadd $MOD ;; rm) ## `cle mod rm [mod]` - delete module MODS=`cd $CLE_D; ls cle-* mod-* bin-* 2>/dev/null | grep $2` [ "$MODS" ] || { echo Nothing like $2 to remove; return 1; } MODN=`wc -w <<<$MODS` if [ $MODN -gt 1 ]; then # chose one in case of more matches PS3="$_CL choose module to remove # $_CN" select MOD in $MODS; do [ -n "$MOD" ] && break done echo else MOD=$MODS fi [ $MOD ] || return _clemodet $MOD _cleask "Do yo want to remove this?" || return mkdir -p $CLE_D/off mv -f $CLE_D/$MOD $CLE_D/off echo Module $MOD moved into $CLE_D/off ;; update) ## `cle mod update` - update all modules _clemodindex || return $? MODS=`cd $CLE_D;ls cle-* mod-* bin-* 2>/dev/null` MUPD='' for MOD in $MODS; do MSTAT=`_clemodstat $MOD` [ "$MSTAT" = "U" ] && MUPD="$MUPD $MOD" done [ "$MUPD" ] || { echo Everything up to date.; return; } _clebold Modules to update:$_CN $MUPD _cleask "Continue?" || return mkdir -p $CLE_D/off for MOD in $MUPD; do cp -f $CLE_D/$MOD $CLE_D/off _clemodadd $MOD done ;; '') _clebold " ${_CU}CLE module management" _clebold "Repository: $_CN$CLE_SRC" _clebold "Local store: $_CN$CLE_D" echo cle help "cle mod" ;; help) cle help "cle mod" ;; *) echo "'cle mod $1' not implemented" echo "Try 'cle mod help '" return 1 ;; esac # remove internal functions unset _clemodstat _clemodesc _clemodver _clemodet _clemodlist _clemodindex