install.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/usr/bin/env bash
  2. debug="off" # turn "on" for debugging
  3. # IMPORTANT OPTIONS #
  4. frame_address="192.168.1.100:2221" # you can find this on the frame, best to make static
  5. source_dir="$HOME/Pictures/picture_frame" # source pictures directory
  6. # Less important options
  7. user="$(whoami)" # default run as current user
  8. user_id="$(id -u)"
  9. group_id="$(id -g)"
  10. script_dir="$PWD" # where to copy the script to, default is $PWD
  11. dest_dir="/media/picture_frame_ftp" # the ftp mount path
  12. temp_dir="/tmp/picture_frame_ftp" # ftp does not support rsync temp files
  13. # Service settings
  14. service_dir="$HOME/.config/systemd/user"
  15. service_name="rsync-to-picture-frame"
  16. on_calendar="hourly"
  17. # END USER OPTIONS #
  18. # if debug is on, echo additional output
  19. debug() { [[ $debug == "on" ]] && echo "debug: $*"; }
  20. check_and_install() {
  21. if ! command -v "$1" > /dev/null 2>&1; then
  22. echo "Installing $1"
  23. [[ -f /etc/os-release ]] && source /etc/os-release
  24. if [[ "${NAME,,}" =~ (fedora|centos) ]]; then
  25. debug "sudo dnf install -y $1"
  26. if ! sudo dnf install -y "$1"; then
  27. echo "Could not install $1, exiting"
  28. exit 1
  29. fi
  30. elif [[ "${NAME,,}" =~ (debian|ubuntu) ]]; then
  31. debug "sudo apt install -y $1"
  32. if ! sudo apt install -y "$1"; then
  33. echo "Could not install $1, exiting"
  34. exit 1
  35. fi
  36. else
  37. echo "$1 must be installed"
  38. exit 1
  39. fi
  40. fi
  41. return $?
  42. }
  43. # Happily make directories
  44. mk_dir() {
  45. for DIR in "$@"; do
  46. if [[ ! -d "$DIR" ]]; then
  47. debug "mkdir -p $DIR"
  48. if ! mkdir -p "$DIR"; then
  49. debug "sudo mkdir -p $DIR"
  50. if ! sudo mkdir -p "$DIR"; then
  51. echo "sudo mkdir $DIR failed, exiting"
  52. exit 1
  53. fi
  54. fi
  55. fi
  56. done
  57. }
  58. # Happily chown directories as $user
  59. chown_dir() {
  60. for DIR in "$@"; do
  61. debug "chown $user:$user -R $DIR"
  62. if ! chown "$user":"$user" -R "$DIR"; then
  63. debug "sudo chown $user:$user -R $DIR"
  64. if ! sudo chown "$user":"$user" -R "$DIR"; then
  65. echo "sudo chown on $DIR failed, exiting"
  66. exit 1
  67. fi
  68. fi
  69. done
  70. return $?
  71. }
  72. # Happily make files executable
  73. make_exec() {
  74. for FILE in "$@"; do
  75. debug "chmod a+x $FILE"
  76. if ! chmod a+x "$FILE"; then
  77. debug "sudo chmod a+x $FILE"
  78. if ! sudo chmod a+x "$FILE"; then
  79. echo "sudo chmod on $FILE failed, exiting"
  80. exit 1
  81. fi
  82. fi
  83. done
  84. return $?
  85. }
  86. # Happily copy files
  87. cp_file() {
  88. if [[ ! -f "$1" ]]; then
  89. echo "$1 is missing"
  90. exit 1
  91. elif ! cp -af "$1" "$2"; then
  92. echo "failed, retrying with sudo"
  93. debug "sudo cp -f $1 $2"
  94. if ! sudo cp -af "$1" "$2"; then
  95. echo "Copying script failed, exiting"
  96. exit 1
  97. fi
  98. fi
  99. }
  100. # Use sed to find ($1) and replace ($2) a string in a file ($3)
  101. f_and_r() {
  102. debug "s#$1#$2#" "$3"
  103. if ! sed -i "s#$1#$2#g" "$3"; then
  104. exit 1
  105. fi
  106. }
  107. main() {
  108. # Install curlftps
  109. debug "check_and_install curlftpfs"
  110. check_and_install "curlftpfs"
  111. # Disable existing timer
  112. debug "systemctl --user disable --now $service_name.timer"
  113. systemctl --user disable --now "$service_name.timer" &> /dev/null
  114. # Unmount existing ftp share
  115. mountpoint -q -- "$dest_dir" && fusermount -u "$dest_dir"
  116. # Create directories
  117. mk_dir "$source_dir" "$dest_dir" "$service_dir" "$script_dir"
  118. chown_dir "$source_dir" "$dest_dir" "$service_dir" "$script_dir"
  119. # Copy script file
  120. cp_file "$service_name.sh.original" "$script_dir/$service_name.sh"
  121. make_exec "$script_dir/$service_name.sh"
  122. f_and_r "{{ftp_mount_path}}" "$dest_dir" "$script_dir/$service_name.sh"
  123. f_and_r "{{source_dir}}" "$source_dir" "$script_dir/$service_name.sh"
  124. f_and_r "{{frame_address}}" "$frame_address" "$script_dir/$service_name.sh"
  125. f_and_r "{{user_id}}" "$user_id" "$script_dir/$service_name.sh"
  126. f_and_r "{{group_id}}" "$group_id" "$script_dir/$service_name.sh"
  127. f_and_r "{{temp_dir}}" "$temp_dir" "$script_dir/$service_name.sh"
  128. # Copy service file
  129. cp_file "$service_name.service.original" "$service_dir/$service_name.service"
  130. f_and_r "{{path_to_script}}" "$script_dir/$service_name.sh" "$service_dir/$service_name.service"
  131. # Copy timer file
  132. cp_file "$service_name.timer.original" "$service_dir/$service_name.timer"
  133. f_and_r "{{on_calendar}}" "$on_calendar" "$service_dir/$service_name.timer"
  134. # Enable timer
  135. debug "systemctl --user daemon-reload"
  136. systemctl --user daemon-reload
  137. debug "systemctl --user enable --now $service_name.timer"
  138. systemctl --user enable --now "$service_name.timer"
  139. }
  140. main "$@"
  141. exit $?