configure.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/bin/bash
  2. # Function to display the menu
  3. show_menu() {
  4. DIALOG_RESULT=$(dialog --title "Configuration Menu" --menu "Choose an option:" 15 50 3 \
  5. 1 "Change Scaling, Kiosk Type, and Websites" \
  6. 2 "Change VNC Password" \
  7. 3 "Exit" \
  8. 3>&1 1>&2 2>&3 3>&-)
  9. echo $DIALOG_RESULT
  10. }
  11. # Function to configure start.sh
  12. configure_start_sh() {
  13. if [[ $# -eq 0 ]]; then # Interactive mode
  14. 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>&-)
  15. DIALOG_RESULT=$(dialog --title "start.sh Configuration" --menu "Choose the script example:" 15 50 2 \
  16. 1 "Simple website script" \
  17. 2 "Two or more tabs alternating every 30 seconds" \
  18. 3>&1 1>&2 2>&3 3>&-)
  19. if [ "$DIALOG_RESULT" = "1" ]; then
  20. 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>&-)
  21. set_start_script_single $SCALE_FACTOR $WEBSITE
  22. elif [ "$DIALOG_RESULT" = "2" ]; then
  23. TAB_COUNT=$(dialog --title "Number of Tabs" --inputbox "Enter number of tabs:" 8 50 "3" 3>&1 1>&2 2>&3 3>&-)
  24. local tab_urls=()
  25. for (( i=1; i<=TAB_COUNT; i++ )); do
  26. URL=$(dialog --title "Enter URL for Tab $i" --inputbox "Enter URL:" 8 50 "http://example$i.com" 3>&1 1>&2 2>&3 3>&-)
  27. tab_urls+=("$URL")
  28. done
  29. configure_tabs $SCALE_FACTOR "${tab_urls[@]}"
  30. fi
  31. else # Automated mode
  32. local scaling_factor=$1
  33. shift # Remove the first argument (scaling factor) to get URLs
  34. if [ $# -eq 1 ]; then # Single website
  35. set_start_script_single $scaling_factor $1
  36. else # Multiple tabs
  37. configure_tabs $scaling_factor "$@"
  38. fi
  39. fi
  40. }
  41. # Helper function to configure start.sh for a single website
  42. set_start_script_single() {
  43. local scaling_factor=$1
  44. local website=$2
  45. # Write the script with properly formatted commands
  46. cat > /home/kiosk/start.sh <<EOF
  47. #!/bin/bash
  48. /usr/bin/chromium --no-first-run --disable-translate --no-default-browser-check --disable-cache --kiosk $website
  49. EOF
  50. chmod a+x /home/kiosk/start.sh
  51. }
  52. # Helper function to configure start.sh for multiple tabs
  53. configure_tabs() {
  54. local scaling_factor=$1
  55. shift # Remove the first argument (scaling factor) to loop over URLs
  56. local tab_urls=""
  57. for url in "$@"; do
  58. tab_urls+="'$url' " # Ensure URLs are properly quoted
  59. done
  60. # Write the script with properly formatted commands
  61. cat > /home/kiosk/start.sh <<EOF
  62. #!/bin/bash
  63. /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 &
  64. sleep 10 # Allow time for Chromium to launch
  65. # Function definitions should be placed before they are called
  66. switch_tabs() {
  67. WINDOW_ID=\$(xdotool search --onlyvisible --class chromium | head -n 1)
  68. xdotool windowactivate --sync \$WINDOW_ID
  69. xdotool key --window \$WINDOW_ID ctrl+Tab
  70. }
  71. is_vnc_active() {
  72. netstat -an | grep ':5901' | grep -q 'ESTABLISHED'
  73. return \$?
  74. }
  75. # Main loop to handle tab switching
  76. while true; do
  77. if is_vnc_active; then
  78. echo "VNC session active, pausing tab switch..."
  79. sleep 5
  80. else
  81. echo "VNC session inactive, switching tabs..."
  82. switch_tabs
  83. sleep 30
  84. fi
  85. done
  86. EOF
  87. chmod a+x /home/kiosk/start.sh
  88. }
  89. # Function to change VNC password
  90. change_vnc_password() {
  91. VNC_PASSWORD=$(dialog --title "VNC Password" --inputbox "Please enter the new 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. echo "VNC password changed"
  97. }
  98. # Check for help option
  99. if [[ "$1" == "-h" || "$1" == "--help" ]]; then
  100. echo "Usage: $0 [--auto <scaling_factor> <url1> [<url2> ... <urlN>]]"
  101. echo "Options:"
  102. echo " --auto Run the script in automatic mode with no user interaction."
  103. echo " Requires specifying the scaling factor (e.g., '1' for 1080p, '2' for 4K),"
  104. echo " and at least one URL. Additional URLs are optional and will open in new tabs."
  105. echo " -h, --help Display this help and exit."
  106. echo ""
  107. echo "Example:"
  108. echo " $0 --auto 1 \"https://www.example.com\""
  109. echo " $0 --auto 2 \"https://www.example1.com\" \"https://www.example2.com\""
  110. exit 0
  111. fi
  112. # Automated or interactive mode check
  113. if [[ $1 == "--auto" ]]; then
  114. shift # Remove the first argument (script name)
  115. scaling_factor=$1
  116. shift # Remove the scaling factor to get URLs
  117. configure_start_sh $scaling_factor "$@"
  118. else
  119. # Main loop
  120. while true; do
  121. choice=$(show_menu)
  122. case $choice in
  123. 1) configure_start_sh ;;
  124. 2) change_vnc_password ;;
  125. 3) exit 0 ;;
  126. *) echo "Invalid option, please try again" ;;
  127. esac
  128. done
  129. fi