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.

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