freedombone-app-dlna 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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@robotics.uk.to>
  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 media'
  31. function reconfigure_dlna {
  32. echo ''
  33. }
  34. function upgrade_dlna {
  35. echo ''
  36. }
  37. function configure_firewall_for_dlna {
  38. if grep -Fxq "configure_firewall_for_dlna" $COMPLETION_FILE; then
  39. return
  40. fi
  41. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  42. # docker does its own firewalling
  43. return
  44. fi
  45. iptables -A INPUT -p udp --dport 1900 -j ACCEPT
  46. iptables -A INPUT -p tcp --dport 8200 -j ACCEPT
  47. function_check save_firewall_settings
  48. save_firewall_settings
  49. OPEN_PORTS+=('DLNA 1900')
  50. OPEN_PORTS+=('DLNA 8200')
  51. echo 'configure_firewall_for_dlna' >> $COMPLETION_FILE
  52. }
  53. function backup_local_dlna {
  54. source_directory=/var/cache/minidlna
  55. if [ -d $source_directory ]; then
  56. dest_directory=dlna
  57. echo $"Backing up $source_directory to $dest_directory"
  58. function_check backup_directory_to_usb
  59. backup_directory_to_usb $source_directory $dest_directory
  60. echo $"Backup to $dest_directory complete"
  61. fi
  62. }
  63. function restore_local_dlna {
  64. if [ -d /var/cache/minidlna ]; then
  65. if [ -d $USB_MOUNT/backup/dlna ]; then
  66. echo $"Restoring DLNA cache"
  67. temp_restore_dir=/root/tempdlna
  68. function_check restore_directory_from_usb
  69. restore_directory_from_usb $temp_restore_dir dlna
  70. cp -r $temp_restore_dir/var/cache/minidlna/* /var/cache/minidlna/
  71. if [ ! "$?" = "0" ]; then
  72. rm -rf $temp_restore_dir
  73. function_check set_user_permissions
  74. set_user_permissions
  75. function_check backup_unmount_drive
  76. backup_unmount_drive
  77. exit 982572
  78. fi
  79. rm -rf $temp_restore_dir
  80. fi
  81. fi
  82. }
  83. function backup_remote_dlna {
  84. if [ -d /var/cache/minidlna ]; then
  85. echo $"Backing up DLNA cache"
  86. backup_directory_to_friend /var/cache/minidlna dlna
  87. echo $"Backup of DLNA cache complete"
  88. fi
  89. }
  90. function restore_remote_dlna {
  91. echo -n ''
  92. }
  93. function remove_dlna {
  94. if ! grep -Fxq "install_dlna" $COMPLETION_FILE; then
  95. return
  96. fi
  97. service minidlna stop
  98. apt-get -y remove --purge minidlna
  99. if [ -f /etc/minidlna.conf ]; then
  100. rm /etc/minidlna.conf
  101. fi
  102. iptables -D INPUT -p udp --dport 1900 -j ACCEPT
  103. iptables -D INPUT -p tcp --dport 8200 -j ACCEPT
  104. function_check save_firewall_settings
  105. save_firewall_settings
  106. sed -i '/install_dlna/d' $COMPLETION_FILE
  107. }
  108. function install_dlna_main {
  109. if grep -Fxq "install_dlna_main" $COMPLETION_FILE; then
  110. return
  111. fi
  112. apt-get -y install minidlna
  113. if [ ! -f /etc/minidlna.conf ]; then
  114. echo $"ERROR: minidlna does not appear to have installed. $CHECK_MESSAGE"
  115. exit 55
  116. fi
  117. sed -i "s|media_dir=/var/lib/minidlna|media_dir=A,/home/$MY_USERNAME/Music|g" /etc/minidlna.conf
  118. if ! grep -q "/home/$MY_USERNAME/Pictures" /etc/minidlna.conf; then
  119. echo "media_dir=P,/home/$MY_USERNAME/Pictures" >> /etc/minidlna.conf
  120. fi
  121. if ! grep -q "/home/$MY_USERNAME/Videos" /etc/minidlna.conf; then
  122. echo "media_dir=V,/home/$MY_USERNAME/Videos" >> /etc/minidlna.conf
  123. fi
  124. if ! grep -q "$USB_MOUNT/Music" /etc/minidlna.conf; then
  125. echo "media_dir=A,$USB_MOUNT/Music" >> /etc/minidlna.conf
  126. fi
  127. if ! grep -q "$USB_MOUNT/Pictures" /etc/minidlna.conf; then
  128. echo "media_dir=P,$USB_MOUNT/Pictures" >> /etc/minidlna.conf
  129. fi
  130. if ! grep -q "$USB_MOUNT/Videos" /etc/minidlna.conf; then
  131. echo "media_dir=V,$USB_MOUNT/Videos" >> /etc/minidlna.conf
  132. fi
  133. sed -i 's/#root_container=./root_container=B/g' /etc/minidlna.conf
  134. if [[ $SYSTEM_TYPE != "$VARIANT_MESH" ]]; then
  135. sed -i 's/#network_interface=/network_interface=eth0/g' /etc/minidlna.conf
  136. else
  137. sed -i 's/#network_interface=/network_interface=$WIFI_INTERFACE/g' /etc/minidlna.conf
  138. fi
  139. sed -i "s/#friendly_name=/friendly_name=\"${PROJECT_NAME} Media\"/g" /etc/minidlna.conf
  140. sed -i 's|#db_dir=/var/cache/minidlna|db_dir=/var/cache/minidlna|g' /etc/minidlna.conf
  141. sed -i 's/#inotify=yes/inotify=yes/g' /etc/minidlna.conf
  142. sed -i 's/#notify_interval=895/notify_interval=300/g' /etc/minidlna.conf
  143. sed -i "s|#presentation_url=/|presentation_url=http://localhost:8200|g" /etc/minidlna.conf
  144. service minidlna force-reload
  145. service minidlna reload
  146. sed -i 's/fs.inotify.max_user_watches*/fs.inotify.max_user_watches=65536/g' /etc/sysctl.conf
  147. if ! grep -q "max_user_watches" $COMPLETION_FILE; then
  148. echo 'fs.inotify.max_user_watches=65536' >> /etc/sysctl.conf
  149. fi
  150. /sbin/sysctl -p
  151. function_check configure_firewall_for_dlna
  152. configure_firewall_for_dlna
  153. echo 'install_dlna_main' >> $COMPLETION_FILE
  154. }
  155. function script_for_attaching_usb_drive {
  156. if grep -Fxq "script_for_attaching_usb_drive" $COMPLETION_FILE; then
  157. return
  158. fi
  159. echo '#!/bin/bash' > /usr/bin/attach-music
  160. echo 'remove-music' >> /usr/bin/attach-music
  161. echo "if [ ! -d $USB_MOUNT ]; then" >> /usr/bin/attach-music
  162. echo " mkdir $USB_MOUNT" >> /usr/bin/attach-music
  163. echo 'fi' >> /usr/bin/attach-music
  164. echo "mount /dev/sda1 $USB_MOUNT" >> /usr/bin/attach-music
  165. echo "chown root:root $USB_MOUNT" >> /usr/bin/attach-music
  166. echo "chown -R minidlna:minidlna $USB_MOUNT/*" >> /usr/bin/attach-music
  167. echo 'service minidlna restart' >> /usr/bin/attach-music
  168. echo 'minidlnad -R' >> /usr/bin/attach-music
  169. chmod +x /usr/bin/attach-music
  170. ln -s /usr/bin/attach-music /usr/bin/attach-usb
  171. ln -s /usr/bin/attach-music /usr/bin/attach-videos
  172. ln -s /usr/bin/attach-music /usr/bin/attach-pictures
  173. ln -s /usr/bin/attach-music /usr/bin/attach-media
  174. echo '#!/bin/bash' > /usr/bin/remove-music
  175. echo "if [ -d $USB_MOUNT ]; then" >> /usr/bin/remove-music
  176. echo " umount $USB_MOUNT" >> /usr/bin/remove-music
  177. echo " rm -rf $USB_MOUNT" >> /usr/bin/remove-music
  178. echo 'fi' >> /usr/bin/remove-music
  179. chmod +x /usr/bin/remove-music
  180. ln -s /usr/bin/remove-music /usr/bin/detach-music
  181. ln -s /usr/bin/remove-music /usr/bin/detach-usb
  182. ln -s /usr/bin/remove-music /usr/bin/remove-usb
  183. ln -s /usr/bin/remove-music /usr/bin/detach-media
  184. ln -s /usr/bin/remove-music /usr/bin/remove-media
  185. ln -s /usr/bin/remove-music /usr/bin/detach-videos
  186. ln -s /usr/bin/remove-music /usr/bin/remove-videos
  187. ln -s /usr/bin/remove-music /usr/bin/detach-pictures
  188. ln -s /usr/bin/remove-music /usr/bin/remove-pictures
  189. echo 'script_for_attaching_usb_drive' >> $COMPLETION_FILE
  190. }
  191. function install_dlna {
  192. if grep -Fxq "install_dlna" $COMPLETION_FILE; then
  193. return
  194. fi
  195. install_dlna_main
  196. script_for_attaching_usb_drive
  197. echo 'install_dlna' >> $COMPLETION_FILE
  198. }
  199. # NOTE: deliberately no exit 0