freedombone-app-dlna 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # DLNA application
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2016 Bob Mottram <bob@freedombone.net>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. VARIANTS='full full-vim media'
  31. IN_DEFAULT_INSTALL=0
  32. SHOW_ON_ABOUT=0
  33. dlna_variables=(SYSTEM_TYPE
  34. USB_MOUNT_DLNA
  35. INSTALLED_WITHIN_DOCKER
  36. MY_USERNAME)
  37. function configure_interactive_dlna {
  38. while true
  39. do
  40. data=$(tempfile 2>/dev/null)
  41. trap "rm -f $data" 0 1 2 5 15
  42. dialog --backtitle $"Freedombone Control Panel" \
  43. --title $"Media Menu" \
  44. --radiolist $"Choose an operation:" 13 70 3 \
  45. 1 $"Attach a drive containing playable media" off \
  46. 2 $"Remove a drive containing playable media" off \
  47. 3 $"Exit" on 2> $data
  48. sel=$?
  49. case $sel in
  50. 1) break;;
  51. 255) break;;
  52. esac
  53. case $(cat $data) in
  54. 1) attach-music;;
  55. 2) remove-music;;
  56. 3) break;;
  57. esac
  58. done
  59. }
  60. function install_interactive_dlna {
  61. echo -n ''
  62. APP_INSTALLED=1
  63. }
  64. function reconfigure_dlna {
  65. echo ''
  66. }
  67. function upgrade_dlna {
  68. echo ''
  69. }
  70. function configure_firewall_for_dlna {
  71. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  72. return
  73. fi
  74. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  75. # docker does its own firewalling
  76. return
  77. fi
  78. firewall_add DLNA 1900 udp
  79. firewall_add DLNA 8200 tcp
  80. mark_completed $FUNCNAME
  81. }
  82. function backup_local_dlna {
  83. source_directory=/var/cache/minidlna
  84. if [ -d $source_directory ]; then
  85. dest_directory=dlna
  86. function_check backup_directory_to_usb
  87. backup_directory_to_usb $source_directory $dest_directory
  88. fi
  89. }
  90. function restore_local_dlna {
  91. if [ -d /var/cache/minidlna ]; then
  92. if [ -d $USB_MOUNT_DLNA/backup/dlna ]; then
  93. echo $"Restoring DLNA cache"
  94. temp_restore_dir=/root/tempdlna
  95. function_check restore_directory_from_usb
  96. restore_directory_from_usb $temp_restore_dir dlna
  97. cp -r $temp_restore_dir/var/cache/minidlna/* /var/cache/minidlna/
  98. if [ ! "$?" = "0" ]; then
  99. rm -rf $temp_restore_dir
  100. function_check set_user_permissions
  101. set_user_permissions
  102. function_check backup_unmount_drive
  103. backup_unmount_drive
  104. exit 982572
  105. fi
  106. rm -rf $temp_restore_dir
  107. fi
  108. fi
  109. }
  110. function backup_remote_dlna {
  111. if [ -d /var/cache/minidlna ]; then
  112. backup_directory_to_friend /var/cache/minidlna dlna
  113. fi
  114. }
  115. function restore_remote_dlna {
  116. if [ -d /var/cache/minidlna ]; then
  117. if [ -d $SERVER_DIRECTORY/backup/dlna ]; then
  118. temp_restore_dir=/root/tempdlna
  119. function_check restore_directory_from_friend
  120. restore_directory_from_friend $temp_restore_dir dlna
  121. cp -r $temp_restore_dir/var/cache/minidlna/* /var/cache/minidlna/
  122. if [ ! "$?" = "0" ]; then
  123. exit 982
  124. fi
  125. rm -rf $temp_restore_dir
  126. fi
  127. fi
  128. }
  129. function remove_dlna {
  130. systemctl stop minidlna
  131. apt-get -yq remove --purge minidlna
  132. if [ -f /etc/minidlna.conf ]; then
  133. rm /etc/minidlna.conf
  134. fi
  135. rm /usr/bin/attach-music
  136. rm /usr/bin/remove-music
  137. remove_completion_param install_dlna
  138. firewall_remove 1900 udp
  139. firewall_remove 8200 tcp
  140. }
  141. function install_dlna_main {
  142. if [[ $(app_is_installed dlna_main) == "1" ]]; then
  143. return
  144. fi
  145. apt-get -yq install minidlna
  146. if [ ! -f /etc/minidlna.conf ]; then
  147. echo $"ERROR: minidlna does not appear to have installed. $CHECK_MESSAGE"
  148. exit 55
  149. fi
  150. if [ ! $USB_MOUNT_DLNA ]; then
  151. USB_MOUNT_DLNA=/mnt/dlna
  152. fi
  153. if [ ${#USB_MOUNT_DLNA} -eq 0 ]; then
  154. USB_MOUNT_DLNA=/mnt/dlna
  155. fi
  156. sed -i "s|media_dir=/var/lib/minidlna|media_dir=A,/home/$MY_USERNAME/Music|g" /etc/minidlna.conf
  157. if ! grep -q "/home/$MY_USERNAME/Pictures" /etc/minidlna.conf; then
  158. echo "media_dir=P,/home/$MY_USERNAME/Pictures" >> /etc/minidlna.conf
  159. fi
  160. if ! grep -q "/home/$MY_USERNAME/Videos" /etc/minidlna.conf; then
  161. echo "media_dir=V,/home/$MY_USERNAME/Videos" >> /etc/minidlna.conf
  162. fi
  163. if ! grep -q "$USB_MOUNT_DLNA/Music" /etc/minidlna.conf; then
  164. echo "media_dir=A,$USB_MOUNT_DLNA/Music" >> /etc/minidlna.conf
  165. fi
  166. if ! grep -q "$USB_MOUNT_DLNA/Pictures" /etc/minidlna.conf; then
  167. echo "media_dir=P,$USB_MOUNT_DLNA/Pictures" >> /etc/minidlna.conf
  168. fi
  169. if ! grep -q "$USB_MOUNT_DLNA/Videos" /etc/minidlna.conf; then
  170. echo "media_dir=V,$USB_MOUNT_DLNA/Videos" >> /etc/minidlna.conf
  171. fi
  172. sed -i 's/#root_container=./root_container=B/g' /etc/minidlna.conf
  173. if [[ $SYSTEM_TYPE != "mesh"* ]]; then
  174. if [[ $(config_param_exists WIFI_INTERFACE) == "0" ]]; then
  175. sed -i 's/#network_interface=/network_interface=eth0/g' /etc/minidlna.conf
  176. else
  177. sed -i "s/#network_interface=/network_interface=$WIFI_INTERFACE/g" /etc/minidlna.conf
  178. fi
  179. else
  180. sed -i "s/#network_interface=/network_interface=$WIFI_INTERFACE/g" /etc/minidlna.conf
  181. fi
  182. sed -i "s/#friendly_name=/friendly_name=\"${PROJECT_NAME} Media\"/g" /etc/minidlna.conf
  183. sed -i 's|#db_dir=/var/cache/minidlna|db_dir=/var/cache/minidlna|g' /etc/minidlna.conf
  184. sed -i 's/#inotify=yes/inotify=yes/g' /etc/minidlna.conf
  185. sed -i 's/#notify_interval=895/notify_interval=300/g' /etc/minidlna.conf
  186. sed -i "s|#presentation_url=/|presentation_url=http://localhost:8200|g" /etc/minidlna.conf
  187. systemctl reload minidlna
  188. sed -i 's/fs.inotify.max_user_watches*/fs.inotify.max_user_watches=65536/g' /etc/sysctl.conf
  189. if ! grep -q "max_user_watches" $COMPLETION_FILE; then
  190. echo 'fs.inotify.max_user_watches=65536' >> /etc/sysctl.conf
  191. fi
  192. /sbin/sysctl -p -q
  193. function_check configure_firewall_for_dlna
  194. configure_firewall_for_dlna
  195. install_completed dlna_main
  196. }
  197. function script_for_attaching_usb_drive {
  198. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  199. return
  200. fi
  201. echo '#!/bin/bash' > /usr/bin/attach-music
  202. echo "source /usr/local/bin/${PROJECT_NAME}-vars" >> /usr/bin/attach-music
  203. echo "UTILS_FILES=/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*" >> /usr/bin/attach-music
  204. echo '' >> /usr/bin/attach-music
  205. echo 'for f in $UTILS_FILES' >> /usr/bin/attach-music
  206. echo 'do' >> /usr/bin/attach-music
  207. echo ' source $f' >> /usr/bin/attach-music
  208. echo 'done' >> /usr/bin/attach-music
  209. echo '' >> /usr/bin/attach-music
  210. echo 'USB_DRIVE=/dev/sda1' >> /usr/bin/attach-music
  211. echo 'detect_usb_drive' >> /usr/bin/attach-music
  212. echo '' >> /usr/bin/attach-music
  213. echo 'remove-music' >> /usr/bin/attach-music
  214. echo "if [ ! -d $USB_MOUNT_DLNA ]; then" >> /usr/bin/attach-music
  215. echo " mkdir $USB_MOUNT_DLNA" >> /usr/bin/attach-music
  216. echo 'fi' >> /usr/bin/attach-music
  217. echo -n 'mount $USB_DRIVE ' >> /usr/bin/attach-music
  218. echo "$USB_MOUNT_DLNA" >> /usr/bin/attach-music
  219. echo "chown root:root $USB_MOUNT_DLNA" >> /usr/bin/attach-music
  220. echo "chown -R minidlna:minidlna $USB_MOUNT_DLNA/*" >> /usr/bin/attach-music
  221. echo 'systemctl restart minidlna' >> /usr/bin/attach-music
  222. echo 'minidlnad -R' >> /usr/bin/attach-music
  223. echo 'exit 0' >> /usr/bin/attach-music
  224. chmod +x /usr/bin/attach-music
  225. echo '#!/bin/bash' > /usr/bin/remove-music
  226. echo "if [ -d $USB_MOUNT_DLNA ]; then" >> /usr/bin/remove-music
  227. echo " umount $USB_MOUNT_DLNA" >> /usr/bin/remove-music
  228. echo " rm -rf $USB_MOUNT_DLNA" >> /usr/bin/remove-music
  229. echo 'fi' >> /usr/bin/remove-music
  230. echo 'exit 0' >> /usr/bin/remove-music
  231. chmod +x /usr/bin/remove-music
  232. mark_completed $FUNCNAME
  233. }
  234. function install_dlna {
  235. install_dlna_main
  236. script_for_attaching_usb_drive
  237. APP_INSTALLED=1
  238. }
  239. # NOTE: deliberately no exit 0