kiosk.sh 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #!/bin/bash
  2. # Function to display command output in a dialog box
  3. execute_command() {
  4. output=$(mktemp /tmp/command_output.XXXXXX)
  5. if "$@" >"$output" 2>&1; then
  6. dialog --title "Command Output" --textbox "$output" 20 60
  7. else
  8. dialog --title "Error Occurred" --textbox "$output" 20 60
  9. fi
  10. rm "$output"
  11. }
  12. # Function to install necessary packages for Debian
  13. install_packages_debian() {
  14. echo "Starting Package installation on Debian. Please check the terminal output."
  15. sudo apt-get update && sudo apt-get install -y chromium tightvncserver xorg x11-xserver-utils unclutter openbox xdotool vnstat x11vnc
  16. }
  17. # Function to install necessary packages for Raspberry Pi OS Lite
  18. install_packages_raspberry() {
  19. echo "Starting Package installation on Raspberry Pi OS Lite. Please check the terminal output."
  20. sudo apt-get update && sudo apt-get install -y chromium-browser tightvncserver xorg x11-xserver-utils unclutter openbox xdotool vnstat x11vnc
  21. sudo ln -s /usr/bin/chromium-browser /usr/bin/chromium
  22. }
  23. # Installation selector based on distro
  24. install_packages() {
  25. DIALOG_RESULT=$(dialog --title "Select Distribution" --menu "Choose your Linux distribution:" 15 50 2 \
  26. 1 "Debian" \
  27. 2 "Raspberry Pi OS Lite" \
  28. 3>&1 1>&2 2>&3 3>&-)
  29. case "$DIALOG_RESULT" in
  30. 1) install_packages_debian ;;
  31. 2) install_packages_raspberry ;;
  32. esac
  33. }
  34. # Function to configure .xinitrc
  35. configure_xinitrc() {
  36. cat > /home/kiosk/.xinitrc << 'EOF'
  37. #!/bin/sh
  38. # Disable power management, screen blanking and make cursor disappear if not moved
  39. xset s off
  40. xset -dpms
  41. xset s noblank
  42. unclutter -idle 0.1 -root &
  43. # Start Openbox session
  44. openbox-session &
  45. x11vnc -display :0 -auth guess -forever -rfbport 5901 &
  46. # Start Kiosk Script
  47. /bin/bash /home/kiosk/start.sh
  48. EOF
  49. sudo chmod a+x /home/kiosk/.xinitrc
  50. }
  51. # Function to configure automatic sign-in
  52. configure_autologin() {
  53. dialog --title "Automatic Login Configuration" --msgbox "Configuring automatic login..." 10 50
  54. sudo mkdir -p /etc/systemd/system/getty@tty1.service.d
  55. sudo bash -c "cat > /etc/systemd/system/getty@tty1.service.d/autologin.conf << 'EOF'
  56. [Service]
  57. ExecStart=
  58. ExecStart=-/sbin/agetty --autologin $USER --noclear %I \$TERM
  59. EOF"
  60. }
  61. # Function to configure start.sh with dynamic scaling factor for interactive or automated mode
  62. configure_start_sh() {
  63. SCALE_FACTOR=$(dialog --title "Display Scaling Factor" --inputbox "Enter the display scaling factor (e.g., 1 for 1080p, 2 for 4K):" 8 50 "1" 3>&1 1>&2 2>&3 3>&-)
  64. if [[ $# -eq 0 ]]; then # If no arguments, it's interactive mode
  65. DIALOG_RESULT=$(dialog --title "start.sh Configuration" --menu "Choose the script example:" 15 50 2 \
  66. 1 "Simple website script" \
  67. 2 "Two or more tabs alternating every 30 seconds" \
  68. 3>&1 1>&2 2>&3 3>&-)
  69. if [ "$DIALOG_RESULT" = "1" ]; then
  70. WEBSITE=$(dialog --title "Enter Website URL" --inputbox "Please enter the URL for the simple website:" 8 50 "https://www.example.com" 3>&1 1>&2 2>&3 3>&-)
  71. set_start_script_single $SCALE_FACTOR $WEBSITE
  72. elif [ "$DIALOG_RESULT" = "2" ]; then
  73. TAB_COUNT=$(dialog --title "Number of Tabs" --inputbox "Enter number of tabs:" 8 50 "3" 3>&1 1>&2 2>&3 3>&-)
  74. local tab_urls=()
  75. for (( i=1; i<=TAB_COUNT; i++ )); do
  76. URL=$(dialog --title "Enter URL for Tab $i" --inputbox "Enter URL:" 8 50 "http://example$i.com" 3>&1 1>&2 2>&3 3>&-)
  77. tab_urls+=("$URL")
  78. done
  79. configure_tabs $SCALE_FACTOR "${tab_urls[@]}"
  80. fi
  81. else # Automated mode with URLs as arguments
  82. if [ $# -eq 1 ]; then # Single website
  83. set_start_script_single $SCALE_FACTOR $1
  84. else # Multiple tabs
  85. configure_tabs $SCALE_FACTOR "$@"
  86. fi
  87. fi
  88. }
  89. # Helper function to configure start.sh for a single website
  90. set_start_script_single() {
  91. VNC_PASSWORD=$(dialog --title "VNC Password" --inputbox "Please enter the VNC password:" 8 50 "" 3>&1 1>&2 2>&3 3>&-)
  92. mkdir -p /home/kiosk/.vnc
  93. rm -f /home/kiosk/.vnc/passwd # Remove existing password file if it exists
  94. echo "$VNC_PASSWORD" | vncpasswd -f > /home/kiosk/.vnc/passwd
  95. chmod 600 /home/kiosk/.vnc/passwd
  96. # Remove existing VNC server configuration if it exists
  97. sudo systemctl stop x11vnc.service
  98. sudo rm -f /etc/systemd/system/x11vnc.service
  99. cat > /home/kiosk/start.sh <<EOF
  100. #!/bin/bash
  101. /usr/bin/chromium --no-first-run --disable-translate --no-default-browser-check --disable-cache --kiosk $2 &
  102. x11vnc -display :0 -auth guess -forever -rfbport 5901 -passwdfile /home/kiosk/.vnc/passwd &
  103. EOF
  104. sudo chmod a+x /home/kiosk/start.sh
  105. # Create a new VNC server configuration
  106. sudo bash -c "cat > /etc/systemd/system/x11vnc.service << 'EOF'
  107. [Unit]
  108. Description=Start x11vnc at startup.
  109. After=multi-user.target
  110. [Service]
  111. Type=simple
  112. ExecStart=/usr/bin/x11vnc -display :0 -auth guess -forever -rfbport 5901 -passwdfile /home/kiosk/.vnc/passwd
  113. [Install]
  114. WantedBy=multi-user.target
  115. EOF"
  116. sudo systemctl daemon-reload
  117. sudo systemctl enable x11vnc.service
  118. sudo systemctl start x11vnc.service
  119. }
  120. # Helper function to configure start.sh for multiple tabs
  121. configure_tabs() {
  122. local scaling_factor=$1
  123. shift # Remove the first argument (scaling factor) to loop over URLs
  124. local tab_urls=""
  125. for url in "$@"; do
  126. tab_urls+="'$url' " # Ensure URLs are properly quoted
  127. done
  128. VNC_PASSWORD=$(dialog --title "VNC Password" --inputbox "Please enter the VNC password:" 8 50 "" 3>&1 1>&2 2>&3 3>&-)
  129. mkdir -p /home/kiosk/.vnc
  130. rm -f /home/kiosk/.vnc/passwd # Remove existing password file if it exists
  131. echo "$VNC_PASSWORD" | vncpasswd -f > /home/kiosk/.vnc/passwd
  132. chmod 600 /home/kiosk/.vnc/passwd
  133. # Remove existing VNC server configuration if it exists
  134. sudo systemctl stop x11vnc.service
  135. sudo rm -f /etc/systemd/system/x11vnc.service
  136. # Write the script with properly formatted commands
  137. cat > /home/kiosk/start.sh <<EOF
  138. #!/bin/bash
  139. /usr/bin/chromium --incognito --force-device-scale-factor=$scaling_factor --temp-profile --disable-profiles --no-first-run --disable-translate --no-default-browser-check --disable-cache --kiosk $tab_urls &
  140. sleep 10 # Allow time for Chromium to launch
  141. # Function definitions should be placed before they are called
  142. switch_tabs() {
  143. WINDOW_ID=\$(xdotool search --onlyvisible --class chromium | head -n 1)
  144. xdotool windowactivate --sync \$WINDOW_ID
  145. xdotool key --window \$WINDOW_ID ctrl+Tab
  146. }
  147. is_vnc_active() {
  148. netstat -an | grep ':5901' | grep -q 'ESTABLISHED'
  149. return \$?
  150. }
  151. # Main loop to handle tab switching
  152. while true; do
  153. if is_vnc_active; then
  154. echo "VNC session active, pausing tab switch..."
  155. sleep 5
  156. else
  157. echo "VNC session inactive, switching tabs..."
  158. switch_tabs
  159. sleep 30
  160. fi
  161. done
  162. x11vnc -display :0 -auth guess -forever -rfbport 5901 -passwdfile /home/kiosk/.vnc/passwd &
  163. EOF
  164. chmod a+x /home/kiosk/start.sh
  165. # Create a new VNC server configuration
  166. sudo bash -c "cat > /etc/systemd/system/x11vnc.service << 'EOF'
  167. [Unit]
  168. Description=Start x11vnc at startup.
  169. After=multi-user.target
  170. [Service]
  171. Type=simple
  172. ExecStart=/usr/bin/x11vnc -display :0 -auth guess -forever -rfbport 5901 -passwdfile /home/kiosk/.vnc/passwd
  173. [Install]
  174. WantedBy=multi-user.target
  175. EOF"
  176. sudo systemctl daemon-reload
  177. sudo systemctl enable x11vnc.service
  178. sudo systemctl start x11vnc.service
  179. }
  180. # Function to edit .bashrc for automatic start of X session
  181. edit_bashrc() {
  182. echo '[[ -z $DISPLAY && $XDG_VTNR -eq 1 ]] && exec startx' >> /home/$USER/.bashrc
  183. }
  184. # Check for help option
  185. if [[ "$1" == "-h" || "$1" == "--help" ]]; then
  186. echo "Usage: $0 [--auto <distro> <scaling_factor> <url1> [<url2> ... <urlN>]]"
  187. echo "Options:"
  188. echo " --auto Run the script in automatic mode with no user interaction."
  189. echo " Requires specifying the distribution ('debian' or 'raspberry'),"
  190. echo " scaling factor (e.g., '1' for 1080p, '2' for 4K), and at least one URL."
  191. echo " Additional URLs are optional and will open in new tabs."
  192. echo " -h, --help Display this help and exit."
  193. echo ""
  194. echo "Example:"
  195. echo " $0 --auto debian 1 \"https://www.example.com\""
  196. echo " $0 --auto raspberry 2 \"https://www.example1.com\" \"https://www.example2.com\""
  197. exit 0
  198. fi
  199. # Automated or interactive mode check
  200. if [[ $1 == "--auto" ]]; then
  201. distro=$2
  202. shift 2 # Remove the first two arguments (script name and distro) for URLs and scaling factor
  203. scaling_factor=$1
  204. shift # Remove the scaling factor to get URLs
  205. [[ $distro == "debian" ]] && install_packages_debian
  206. [[ $distro == "raspberry" ]] && install_packages_raspberry
  207. configure_xinitrc
  208. configure_start_sh "$@"
  209. configure_autologin
  210. edit_bashrc
  211. else
  212. dialog --title "WARNING !!!" --msgbox "Do not run this script under any user other than "kiosk" and make sure the Kiosk user has sudo rights for the duration of the installation. In addition check out the -h command on this script if you wish to do the installation more automated!" 10 50
  213. # Main menu for guided installation
  214. current_step=1
  215. while true; do
  216. case $current_step in
  217. 1)
  218. install_packages
  219. ;;
  220. 2)
  221. configure_xinitrc
  222. ;;
  223. 3)
  224. configure_start_sh
  225. ;;
  226. 4)
  227. configure_autologin
  228. ;;
  229. 5)
  230. edit_bashrc
  231. ;;
  232. 6)
  233. break
  234. ;;
  235. *)
  236. echo "Invalid option. Try another one." >&2
  237. ;;
  238. esac
  239. # Automatically proceed to the next step if the previous step was successful
  240. if [ $? -eq 0 ]; then
  241. current_step=$((current_step + 1))
  242. if [ $current_step -gt 5 ]; then
  243. current_step=6
  244. fi
  245. else
  246. dialog --title "Error" --msgbox "An error occurred. Please check the output and try again." 10 50
  247. fi
  248. # Exit the loop if the last step is completed
  249. if [ $current_step -eq 6 ]; then
  250. break
  251. fi
  252. done
  253. dialog --title "Exit" --msgbox "If you completed all steps and no errors were shown to you, reboot now to test if the installation was successful." 10 50
  254. fi
  255. clear