autoxinitrc.sh 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env bash
  2. # Place a template xinitrc in ~/.xinitrc based on the system one without
  3. # default exec lines (twm, xterm etc.) This solves the problem for new users
  4. # who don't know how to base their ~/.xinitrc on /etc/X11/xinit/xinitrc. It
  5. # should make it easier for them use the template from the system file.
  6. # Written by Adnan Shameem; License: MIT (Expat)
  7. #
  8. # Usage:
  9. # - Make sure you have xorg, xinit installed on your system
  10. # - Optional, replace your desired exec command on the last echo statement of this script
  11. # - Run: ./autoxinitrc.sh
  12. # Automatically remove default exec lines from system xinitrc and put it on ~.
  13. # Add your custom lines afterwards to the destination file.
  14. # Params:
  15. # - $1: Source filename (default: /etc/X11/xinit/xinitrc) (optional)
  16. # - $2: Destination filename (default: $HOME/.xinitrc) (optional)
  17. function autoxinitrc() {
  18. # Default vars
  19. [ -z "$1" ] && local filename=/etc/X11/xinit/xinitrc || local filename="$1"
  20. [ -z "$2" ] && local destfile="$HOME/.xinitrc" || local destfile="$2"
  21. # To store sed expression for line deletion
  22. local exp=''
  23. # Line on which last paragraph started
  24. local para_start=1
  25. # Is last line blank
  26. local last_blank=1
  27. # Does paragraph has 'exec ' line
  28. local para_has_exec=0
  29. # Read file into array
  30. readarray -t lines < "$filename"
  31. for i in $(seq 1 "${#lines[@]}")
  32. do
  33. # Stores the current line contents
  34. line="${lines[$i]}"
  35. # Reached paragraph end
  36. if [ -z "$line" ]; then
  37. last_blank=1
  38. if [ "$para_has_exec" = "1" ]; then
  39. exp+="${para_start},${i}d;"
  40. fi
  41. para_has_exec=0 # reset exec search for paragraph
  42. # Inside a paragraph, encountered a non-empty line
  43. elif [ ! -z "$line" ]; then
  44. if [ "$last_blank" = "1" ]; then # first line of paragraph
  45. para_start=$i
  46. para_has_exec=0
  47. last_blank=0
  48. fi
  49. if echo "$line" | grep -e 'exec\s' &>/dev/null; then
  50. para_has_exec=1
  51. fi
  52. fi
  53. done
  54. if [ -f "$HOME/.xinitrc" ]; then
  55. mv "$HOME/.xinitrc" "$HOME/.xinitrc.bak"
  56. fi
  57. cp "$filename" "$destfile"
  58. sed -i'' -e "$exp" "$destfile"
  59. }
  60. # We're eager to replace the ~/.xinitrc
  61. confirm=1
  62. # When there is already a ~/.xinitrc file, ask if user wants to replace it
  63. if [ -f "$HOME/.xinitrc" ]; then
  64. echo -n "There is already an existing ~/.xinitrc. Do you want to replace it [y/N]? "
  65. read sure_input
  66. if [[ ! "$sure_input" =~ ^([yY][eE][sS]|[yY])$ ]]; then
  67. confirm=0
  68. fi
  69. fi
  70. if [ "$confirm" = "1" ]; then
  71. # Place a template xinitrc in ~/.xinitrc without default exec lines
  72. autoxinitrc
  73. # Actual exec commands we want when 'startx' is run
  74. echo "
  75. # Run openbox
  76. exec openbox-session
  77. " >> "$HOME/.xinitrc"
  78. fi