mirror of the now-defunct rocklinux.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

241 lines
4.6 KiB

  1. #!/bin/bash
  2. bold=""
  3. red=""
  4. green=""
  5. yellow=""
  6. blue=""
  7. normal=""
  8. # {{{ usage_functions
  9. usage_functions(){
  10. echo -n
  11. }
  12. # }}}
  13. # {{{ human readable
  14. human_readable() {
  15. size=${1}
  16. if [ ${size} -gt 1024 ] ; then
  17. if [ ${size} -gt $(( 1024*1024 )) ] ; then
  18. size="$(( ${size} / ( 1024*1024 ) )) MB"
  19. else
  20. size="$(( ${size} / 1024 )) kB"
  21. fi
  22. else
  23. size="${size} Byte"
  24. fi
  25. echo "${size}"
  26. }
  27. # }}}
  28. # {{{ lvp_read
  29. function lvp_read {
  30. var=$1
  31. shift
  32. default=${1}
  33. shift
  34. if [ ${LVP_USE_DEFAULTS} -eq 1 ] ; then
  35. echo "${@} ${default} (batch mode)"
  36. eval "${var}=\"${default}\""
  37. return
  38. fi
  39. eval "read -p \"${@}\" ${var}"
  40. return
  41. }
  42. # }}}
  43. # {{{ confirm
  44. function confirm {
  45. unset yesno
  46. if [ ${LVP_USE_DEFAULTS} -eq 1 ] ; then
  47. echo "${@} [yes|no] ? yes (batch mode)"
  48. yesno="yes"
  49. fi
  50. while [ "${yesno}" != "yes" -a "${yesno}" != "no" ] ; do
  51. echo -n "${@} [yes|no] ? "
  52. read yesno
  53. done
  54. [ "${yesno}" = "yes" ] && return 0
  55. return 1
  56. }
  57. # }}}
  58. # {{{ menu_init
  59. menu_init () {
  60. block_level=0
  61. items=0
  62. }
  63. # }}}
  64. # {{{ block_start
  65. block_start () {
  66. block_level=$(( ${block_level} + 2 ))
  67. echo "${@}"
  68. }
  69. # }}}
  70. # {{{ block_end
  71. block_end () {
  72. block_level=$(( ${block_level} - 2 ))
  73. }
  74. # }}}
  75. # {{{ bool
  76. bool () {
  77. # bool LVPCFG_FOO 1 "This is Option FOO"
  78. items=$(( ${items} + 1 ))
  79. eval "type_${items}=\"bool\""
  80. eval "option_${items}=\"${1}\""
  81. eval "value=\${${1}}"
  82. [ -z "${value}" ] && \
  83. eval "${1}=\"${2}\""
  84. eval "text_${items}=\"${3}\""
  85. }
  86. # }}}
  87. # {{{ multi
  88. multi () {
  89. # multi LVPCFG_FOO default "This is Multioption FOO" foo default bar
  90. items=$(( ${items} + 1 ))
  91. eval "type_${items}=\"multi\""
  92. eval "option_${items}=\"${1}\""
  93. eval "value=\${${1}}"
  94. [ -z "${value}" ] && \
  95. eval "${1}=\"${2}\""
  96. eval "text_${items}=\"${3}\""
  97. shift; shift; shift
  98. eval "values_${items}=\"\""
  99. while [ ! -z "${1}" ] ; do
  100. eval "values_${items}=\"\${values_${items}} ${1}\""
  101. shift
  102. done
  103. }
  104. # }}}
  105. # {{{ text
  106. text () {
  107. # text LVPCFG_FOO "default text" "This is textoption FOO"
  108. items=$(( ${items} + 1 ))
  109. eval "type_${items}=\"text\""
  110. eval "option_${items}=\"${1}\""
  111. eval "value=\${${1}}"
  112. [ -z "${value}" ] && \
  113. eval "${1}=\"${2}\""
  114. eval "text_${items}=\"${3}\""
  115. }
  116. # }}}
  117. # {{{ comment
  118. comment (){
  119. items=$(( ${items} + 1 ))
  120. eval "type_${items}=\"comment\""
  121. eval "text_${items}=\"${@}\""
  122. }
  123. # }}}
  124. # {{{ display
  125. display () {
  126. i=1
  127. while [ ${i} -le ${items} ] ; do
  128. eval "type=\${type_${i}}"
  129. case "${type}" in
  130. bool)
  131. eval "option=\${option_${i}}"
  132. eval "value=\${${option}}"
  133. desc="X"
  134. [ "${value}" == "0" ] && desc=" "
  135. eval "echo \"${i} - [${desc}] \${text_${i}}\""
  136. ;;
  137. multi)
  138. eval "option=\${option_${i}}"
  139. eval "value=\${${option}}"
  140. eval "echo \"${i} - (${value}) \${text_${i}}\""
  141. ;;
  142. text)
  143. eval "option=\${option_${i}}"
  144. eval "value=\${${option}}"
  145. dots="..."
  146. [ "${value:0:17}" == "${value}" ] && dots=""
  147. eval "echo \"${i} - (${value:0:17}${dots}) \${text_${i}}\""
  148. ;;
  149. comment)
  150. eval "echo \"\${text_${i}}\""
  151. ;;
  152. *)
  153. echo "${type} NOT IMPLEMENTED"
  154. ;;
  155. esac
  156. i=$(( ${i} + 1 ))
  157. done
  158. }
  159. # }}}
  160. # {{{ save
  161. save () {
  162. i=1
  163. echo "# LVP Configuration" > .config
  164. while [ ${i} -le ${items} ] ; do
  165. eval "option=\${option_${i}}"
  166. eval "value=\${${option}}" 2>/dev/null
  167. eval "type=\${type_${i}}"
  168. case "${type}" in
  169. bool)
  170. echo "${option}=${value}" >>.config
  171. ;;
  172. multi)
  173. echo "${option}=\"${value}\"" >>.config
  174. ;;
  175. text)
  176. echo "${option}=\"${value}\"" >>.config
  177. ;;
  178. esac
  179. i=$(( ${i} + 1 ))
  180. done
  181. }
  182. # }}}
  183. # {{{ load
  184. load () {
  185. . .config
  186. }
  187. # }}}
  188. # {{{ get
  189. get () {
  190. # get 3
  191. eval "option=\${option_${1}}"
  192. if [ -z "${option}" ] ; then
  193. echo "${bold}${red}No such option: ${1}${normal}"
  194. sleep 1
  195. return
  196. fi
  197. eval "type=\${type_${1}}"
  198. case "${type}" in
  199. text)
  200. eval "value=\${${option}}"
  201. read -erp "Enter new value for ${option} [${value}]> " new
  202. [ ! -z "${new}" ] && eval "${option}=\"${new}\""
  203. ;;
  204. multi)
  205. eval "value=\${${option}}"
  206. eval "values=\${values_${1}}"
  207. x=1
  208. for v in ${values} ; do
  209. [ "${value}" == "${v}" ] && echo -n "${bold}"
  210. echo "${x} - ${v}${normal}"
  211. eval "nval_${1}_${x}=\"${v}\""
  212. x=$(( ${x} + 1 ))
  213. done
  214. read -ep "Enter new value by number> " new
  215. [ -z "${new}" ] && return
  216. eval "new=\${nval_${1}_${new}}"
  217. if [ -z "${new}" ] ; then
  218. echo "${bold}${red}No such option!${normal}"
  219. sleep 1
  220. return
  221. else
  222. eval "${option}=\"${new}\""
  223. fi
  224. ;;
  225. bool)
  226. eval "value=\${${option}}"
  227. if [ ${value} -eq 1 ] ; then
  228. eval "${option}=0"
  229. else
  230. eval "${option}=1"
  231. fi
  232. ;;
  233. *)
  234. echo "${bold}${red}NOT IMPLEMENTED${normal}"
  235. sleep 1
  236. ;;
  237. esac
  238. }
  239. # }}}