freedombone-app-dlna 6.3KB

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