| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
#title :lslib-core |
| 4 |
#description :core library for LS scripts |
| 5 |
#author :Łukasz A. Grabowski <[email protected]> |
| 6 |
#date :20130928 |
| 7 |
#version :1.0.3 |
| 8 |
#notes : |
| 9 |
#bash_version :4.2.37(1)-release |
| 10 |
#copywrite :Copyright (C) 2013 Łukasz A. Grabowski |
| 11 |
#license :This program is free software: you can redistribute |
| 12 |
# :it and/or modify it under the terms of the GNU General |
| 13 |
# :Public License as published by the Free Software |
| 14 |
# :Foundation, either version 2 of the License or |
| 15 |
# :any later version. |
| 16 |
# : |
| 17 |
# :This program is distributed in the hope that it will |
| 18 |
# :be useful, but WITHOUT ANY WARRANTY; without even the |
| 19 |
# :implied warranty of MERCHANTABILITY or FITNESS FOR |
| 20 |
# :A PARTICULAR PURPOSE. See the GNU General Public |
| 21 |
# :License for more details. |
| 22 |
# : |
| 23 |
# :You should have received a copy of the GNU General |
| 24 |
# :Public License along with this program. If not, see |
| 25 |
# :http://www.gnu.org/licenses/. |
| 26 |
#======================================================================= |
| 27 |
|
| 28 |
################# |
| 29 |
# Configuration # |
| 30 |
################# |
| 31 |
|
| 32 |
PACD="/usr/share/ls" #main directory for LS scripts |
| 33 |
LIBD="lib" #library dir for LS scripts |
| 34 |
|
| 35 |
####################### |
| 36 |
# Predefined and init # |
| 37 |
####################### |
| 38 |
|
| 39 |
if [ -z "$LS_EXEC" ]; then |
| 40 |
echo "This script are only for inclusion in LS packet scripts. Don't use it itself." 1>&2 |
| 41 |
exit 1 |
| 42 |
fi |
| 43 |
|
| 44 |
############################## |
| 45 |
# configuration read / write # |
| 46 |
############################## |
| 47 |
|
| 48 |
#get configuration |
| 49 |
# cfg_g PATH |
| 50 |
cfg_g() { |
| 51 |
local PTH="" |
| 52 |
if [ $# -gt 0 ]; then |
| 53 |
local PTH="$1"; |
| 54 |
fi; |
| 55 |
local DAT |
| 56 |
local ERR |
| 57 |
DAT="$($PACD/$LIBD/ls-config -f "$CFGFN" -qv --get="$PTH")" |
| 58 |
ERR=$? |
| 59 |
echo "$DAT" |
| 60 |
return $ERR |
| 61 |
} |
| 62 |
|
| 63 |
#get configuration type |
| 64 |
# cfg_t PATH |
| 65 |
cfg_t() { |
| 66 |
local PTH="" |
| 67 |
if [ $# -gt 0 ]; then |
| 68 |
local PTH="$1"; |
| 69 |
fi; |
| 70 |
local DAT |
| 71 |
local ERR |
| 72 |
DAT="$($PACD/$LIBD/ls-config -f "$CFGFN" -qt --get="$PTH")" |
| 73 |
ERR=$? |
| 74 |
echo "$DAT" |
| 75 |
return $ERR |
| 76 |
} |
| 77 |
|
| 78 |
#get configuration items count |
| 79 |
# cfg_c PATH |
| 80 |
cfg_c() { |
| 81 |
local PTH="" |
| 82 |
if [ $# -gt 0 ]; then |
| 83 |
local PTH="$1"; |
| 84 |
fi; |
| 85 |
local DAT |
| 86 |
local ERR |
| 87 |
DAT="$($PACD/$LIBD/ls-config -f "$CFGFN" -qc --get="$PTH")" |
| 88 |
ERR=$? |
| 89 |
echo "$DAT" |
| 90 |
return $ERR |
| 91 |
} |
| 92 |
|
| 93 |
#set configuration |
| 94 |
# cfg_s PATH DATA [TYPE=string] |
| 95 |
cfg_s() { |
| 96 |
local PTH="" |
| 97 |
if [ $# -gt 0 ]; then |
| 98 |
local PTH="$1"; |
| 99 |
fi; |
| 100 |
local DATA="" |
| 101 |
if [ $# -gt 1 ]; then |
| 102 |
local DATA="$2"; |
| 103 |
fi; |
| 104 |
local TYPE="string" |
| 105 |
if [ $# -gt 2 ]; then |
| 106 |
local TYPE="$3"; |
| 107 |
fi; |
| 108 |
local DAT |
| 109 |
local ERR |
| 110 |
DAT="$($PACD/$LIBD/ls-config -f "$CFGFN" -q --set="$PTH" --data="$DATA" --type="$TYPE")" |
| 111 |
ERR=$? |
| 112 |
echo "$DAT" |
| 113 |
return $ERR |
| 114 |
} |
| 115 |
|
| 116 |
#remove configuration |
| 117 |
# cfg_u PATH |
| 118 |
cfg_u() { |
| 119 |
local PTH="" |
| 120 |
if [ $# -gt 0 ]; then |
| 121 |
local PTH="$1"; |
| 122 |
fi; |
| 123 |
local DAT |
| 124 |
local ERR |
| 125 |
DAT="$($PACD/$LIBD/ls-config -f "$CFGFN" -q --set="$PTH" --unset)" |
| 126 |
ERR=$? |
| 127 |
return $ERR |
| 128 |
} |
| 129 |
|
| 130 |
cfg_f_g() { |
| 131 |
local BCFN="$CFGFN" |
| 132 |
local EX |
| 133 |
CFGFN="$1" |
| 134 |
shift |
| 135 |
cfg_g "$@" |
| 136 |
EX=$? |
| 137 |
CFGFN="$BCFN" |
| 138 |
return $EX |
| 139 |
} |
| 140 |
|
| 141 |
cfg_f_t() { |
| 142 |
local BCFN="$CFGFN" |
| 143 |
local EX |
| 144 |
CFGFN="$1" |
| 145 |
shift |
| 146 |
cfg_t "$@" |
| 147 |
EX=$? |
| 148 |
CFGFN="$BCFN" |
| 149 |
return $EX |
| 150 |
} |
| 151 |
|
| 152 |
cfg_f_c() { |
| 153 |
local BCFN="$CFGFN" |
| 154 |
local EX |
| 155 |
CFGFN="$1" |
| 156 |
shift |
| 157 |
cfg_c "$@" |
| 158 |
EX=$? |
| 159 |
CFGFN="$BCFN" |
| 160 |
return $EX |
| 161 |
} |
| 162 |
|
| 163 |
cfg_f_s() { |
| 164 |
local BCFN="$CFGFN" |
| 165 |
local EX |
| 166 |
CFGFN="$1" |
| 167 |
shift |
| 168 |
cfg_s "$@" |
| 169 |
EX=$? |
| 170 |
CFGFN="$BCFN" |
| 171 |
return $EX |
| 172 |
} |
| 173 |
|
| 174 |
cfg_f_u() { |
| 175 |
local BCFN="$CFGFN" |
| 176 |
local EX |
| 177 |
CFGFN="$1" |
| 178 |
shift |
| 179 |
cfg_u "$@" |
| 180 |
EX=$? |
| 181 |
CFGFN="$BCFN" |
| 182 |
return $EX |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
###################### |
| 187 |
# base variable init # |
| 188 |
###################### |
| 189 |
|
| 190 |
#package name |
| 191 |
SCRFN="$(basename "$0")" |
| 192 |
|
| 193 |
#configuriation directory and file |
| 194 |
if [ -z "$CFGD" ]; then |
| 195 |
CFGD="/etc/ls" |
| 196 |
fi; |
| 197 |
CFGFN="$CFGD/$SCRFN" |
| 198 |
|
| 199 |
|