unixwz0r

setup-tuxhat (modified cdm code for my scripts main menu)

Jan 20th, 2015
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. #!/bin/bash
  2. # Tux Hat Linux Main Menu Setup Script
  3.  
  4. name=$(basename "$0")
  5. longname='Tux Hat Linux'
  6. ver='3.3 - Main Menu Setup'
  7.  
  8. trap '' SIGINT SIGTSTP
  9.  
  10. # Helper functions.
  11.  
  12. warn() { (printf ' \033[01;33m*\033[00m '; echo "$name: $*") > /dev/stderr; }
  13. error() { (printf ' \033[01;31m*\033[00m '; echo "$name: $*") > /dev/stderr; }
  14. exitnormal() { exit 0; }
  15. exiterror() { sleep 1; exit 1; }
  16. exitcancel() { exit 2; }
  17. yesno()
  18. {
  19. [ -z "$1" ] && return 1
  20. eval value=\$${1}
  21.  
  22. case "$value" in
  23. [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) return 0;;
  24. [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) return 1;;
  25. *) warn "invalid value for \`$1'; falling back to \`no' for now.";;
  26. esac
  27. }
  28.  
  29. # Source setup-tuxhat configurations.
  30.  
  31. if [[ -n "$1" ]]; then
  32. if [[ -f "$1" ]]
  33. then
  34. source "$1"
  35. else
  36. error "config file \`$1' does not exist."
  37. exiterror
  38. fi
  39. elif [[ -f "$HOME/.sthrc" ]]; then
  40. source "$HOME/.sthrc"
  41. elif [[ -f /etc/sthrc ]]; then
  42. source /etc/sthrc
  43. fi
  44.  
  45. # Default options.
  46.  
  47. dialogrc=${dialogrc:-}
  48. display=${display:-0}
  49. [[ -z "${binlist[*]}" ]] && binlist=()
  50. [[ -z "${namelist[*]}" ]] && namelist=()
  51. [[ -z "${flaglist[*]}" ]] && flaglist=()
  52.  
  53. # If $binlist is not set in cdmrc or by files in /etc/X11/Sessions,
  54. # try .desktop files in /usr/share/xsessions/ .
  55.  
  56. if [[ "${#binlist[@]}" == 0 && -d /usr/share/xsessions ]]; then
  57. desktopsessions=($(find /usr/share/xsessions/ -regex .\*.desktop))
  58. #TODO: allow full quoting and expansion according to desktop entry spec:
  59. # http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#exec-variables
  60.  
  61. for ((count=0; count < ${#desktopsessions[@]}; count++)); do
  62. # TryExec key is there to determine if executable is present,
  63. # but as we are going to test the Exec key anyway, we ignore it.
  64. execkey=$(sed -n -e 's/^Exec=//p' <${desktopsessions[${count}]})
  65. namekey=$(sed -n -e 's/^Name=//p' <${desktopsessions[${count}]})
  66. if [[ -n ${execkey} && -n ${namekey} ]]; then
  67. # The .desktop files allow there Exec keys to use $PATH lookup.
  68. binitem="$(which $(cut -f 1 -d ' ' <<< ${execkey}))"
  69. # If which fails to return valid path, skip to next .desktop file.
  70. if [[ $? != 0 ]]; then continue; fi
  71. binlist+=("${binitem} $(cut -s -f 2- -d ' ' <<< ${execkey})")
  72. flaglist+=('X')
  73. namelist+=("${namekey}")
  74. fi
  75. done
  76. fi
  77.  
  78. case "${#binlist[@]}" in
  79. 0)
  80. error "No programs found in cdm config file, /etc/X11/Sessions or /usr/share/xsessions."
  81. exiterror
  82. ;;
  83. 1)
  84. # No need to call dialog AND clear, only one possible program
  85. binindex=0
  86. ;;
  87. *)
  88. menu=()
  89. for ((count = 0; count < ${#namelist[@]}; count++)); do
  90. menu=("${menu[@]}" "$((count+countfrom))" "${namelist[${count}]}")
  91. done
  92. binindex=$(
  93. DIALOGRC="$dialogrc" dialog --colors --stdout \
  94. --backtitle "$longname v$ver" --ok-label ' Select ' \
  95. --cancel-label ' Exit ' --menu 'Select Setup Script' 0 0 0 "${menu[@]}"
  96. )
  97. if [[ $? != 0 ]]; then
  98. clear; exitcancel
  99. fi
  100. clear
  101. let binindex-=countfrom
  102. ;;
  103. esac
  104.  
  105. # Run $bin according to its flag.
  106. bin=($(eval echo "${binlist[${binindex}]}"))
  107. case ${flaglist[$binindex]} in
  108. # *C*onsole programs.
  109. [Cc])
  110. # If $bin is a login shell, it might `exec' cdm again, causing an endless
  111. # loop. To solve this problem, export $CDM_SPAWN when `exec'ing $bin and
  112. # only let the shell automatically `exec' cdm when $CDM_SPAWN is not set.
  113. # See also the example shell profile file shipped with the cdm package.
  114.  
  115. # Also untrap SIGINT and SIGTSTP before spawning process: If this is not
  116. # done, any child process of any child (bash) shell will completely
  117. # ignore SIGINT, which is rather confusing, and cannot be undone.
  118.  
  119. trap - SIGINT SIGTSTP
  120. CDM_SPAWN=$$ exec "${bin[@]}"
  121. ;;
  122.  
  123.  
  124. esac
  125.  
  126. # vim:ts=4:sw=4:et
Advertisement
Add Comment
Please, Sign In to add comment