freedombone-app-dlna 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. if [ -d /var/cache/minidlna ]; then
  92. if [ -d $SERVER_DIRECTORY/backup/dlna ]; then
  93. echo $"Restoring DLNA cache"
  94. temp_restore_dir=/root/tempdlna
  95. function_check restore_directory_from_friend
  96. restore_directory_from_friend $temp_restore_dir dlna
  97. cp -r $temp_restore_dir/var/cache/minidlna/* /var/cache/minidlna/
  98. if [ ! "$?" = "0" ]; then
  99. exit 982
  100. fi
  101. rm -rf $temp_restore_dir
  102. echo $"Restore of DLNA complete"
  103. fi
  104. fi
  105. }
  106. function remove_dlna {
  107. if ! grep -Fxq "install_dlna" $COMPLETION_FILE; then
  108. return
  109. fi
  110. service minidlna stop
  111. apt-get -y remove --purge minidlna
  112. if [ -f /etc/minidlna.conf ]; then
  113. rm /etc/minidlna.conf
  114. fi
  115. iptables -D INPUT -p udp --dport 1900 -j ACCEPT
  116. iptables -D INPUT -p tcp --dport 8200 -j ACCEPT
  117. function_check save_firewall_settings
  118. save_firewall_settings
  119. sed -i '/install_dlna/d' $COMPLETION_FILE
  120. }
  121. function install_dlna_main {
  122. if grep -Fxq "install_dlna_main" $COMPLETION_FILE; then
  123. return
  124. fi
  125. apt-get -y install minidlna
  126. if [ ! -f /etc/minidlna.conf ]; then
  127. echo $"ERROR: minidlna does not appear to have installed. $CHECK_MESSAGE"
  128. exit 55
  129. fi
  130. sed -i "s|media_dir=/var/lib/minidlna|media_dir=A,/home/$MY_USERNAME/Music|g" /etc/minidlna.conf
  131. if ! grep -q "/home/$MY_USERNAME/Pictures" /etc/minidlna.conf; then
  132. echo "media_dir=P,/home/$MY_USERNAME/Pictures" >> /etc/minidlna.conf
  133. fi
  134. if ! grep -q "/home/$MY_USERNAME/Videos" /etc/minidlna.conf; then
  135. echo "media_dir=V,/home/$MY_USERNAME/Videos" >> /etc/minidlna.conf
  136. fi
  137. if ! grep -q "$USB_MOUNT/Music" /etc/minidlna.conf; then
  138. echo "media_dir=A,$USB_MOUNT/Music" >> /etc/minidlna.conf
  139. fi
  140. if ! grep -q "$USB_MOUNT/Pictures" /etc/minidlna.conf; then
  141. echo "media_dir=P,$USB_MOUNT/Pictures" >> /etc/minidlna.conf
  142. fi
  143. if ! grep -q "$USB_MOUNT/Videos" /etc/minidlna.conf; then
  144. echo "media_dir=V,$USB_MOUNT/Videos" >> /etc/minidlna.conf
  145. fi
  146. sed -i 's/#root_container=./root_container=B/g' /etc/minidlna.conf
  147. if [[ $SYSTEM_TYPE != "$VARIANT_MESH" ]]; then
  148. sed -i 's/#network_interface=/network_interface=eth0/g' /etc/minidlna.conf
  149. else
  150. sed -i 's/#network_interface=/network_interface=$WIFI_INTERFACE/g' /etc/minidlna.conf
  151. fi
  152. sed -i "s/#friendly_name=/friendly_name=\"${PROJECT_NAME} Media\"/g" /etc/minidlna.conf
  153. sed -i 's|#db_dir=/var/cache/minidlna|db_dir=/var/cache/minidlna|g' /etc/minidlna.conf
  154. sed -i 's/#inotify=yes/inotify=yes/g' /etc/minidlna.conf
  155. sed -i 's/#notify_interval=895/notify_interval=300/g' /etc/minidlna.conf
  156. sed -i "s|#presentation_url=/|presentation_url=http://localhost:8200|g" /etc/minidlna.conf
  157. service minidlna force-reload
  158. service minidlna reload
  159. sed -i 's/fs.inotify.max_user_watches*/fs.inotify.max_user_watches=65536/g' /etc/sysctl.conf
  160. if ! grep -q "max_user_watches" $COMPLETION_FILE; then
  161. echo 'fs.inotify.max_user_watches=65536' >> /etc/sysctl.conf
  162. fi
  163. /sbin/sysctl -p
  164. function_check configure_firewall_for_dlna
  165. configure_firewall_for_dlna
  166. echo 'install_dlna_main' >> $COMPLETION_FILE
  167. }
  168. function script_for_attaching_usb_drive {
  169. if grep -Fxq "script_for_attaching_usb_drive" $COMPLETION_FILE; then
  170. return
  171. fi
  172. echo '#!/bin/bash' > /usr/bin/attach-music
  173. echo 'remove-music' >> /usr/bin/attach-music
  174. echo "if [ ! -d $USB_MOUNT ]; then" >> /usr/bin/attach-music
  175. echo " mkdir $USB_MOUNT" >> /usr/bin/attach-music
  176. echo 'fi' >> /usr/bin/attach-music
  177. echo "mount /dev/sda1 $USB_MOUNT" >> /usr/bin/attach-music
  178. echo "chown root:root $USB_MOUNT" >> /usr/bin/attach-music
  179. echo "chown -R minidlna:minidlna $USB_MOUNT/*" >> /usr/bin/attach-music
  180. echo 'service minidlna restart' >> /usr/bin/attach-music
  181. echo 'minidlnad -R' >> /usr/bin/attach-music
  182. chmod +x /usr/bin/attach-music
  183. ln -s /usr/bin/attach-music /usr/bin/attach-usb
  184. ln -s /usr/bin/attach-music /usr/bin/attach-videos
  185. ln -s /usr/bin/attach-music /usr/bin/attach-pictures
  186. ln -s /usr/bin/attach-music /usr/bin/attach-media
  187. echo '#!/bin/bash' > /usr/bin/remove-music
  188. echo "if [ -d $USB_MOUNT ]; then" >> /usr/bin/remove-music
  189. echo " umount $USB_MOUNT" >> /usr/bin/remove-music
  190. echo " rm -rf $USB_MOUNT" >> /usr/bin/remove-music
  191. echo 'fi' >> /usr/bin/remove-music
  192. chmod +x /usr/bin/remove-music
  193. ln -s /usr/bin/remove-music /usr/bin/detach-music
  194. ln -s /usr/bin/remove-music /usr/bin/detach-usb
  195. ln -s /usr/bin/remove-music /usr/bin/remove-usb
  196. ln -s /usr/bin/remove-music /usr/bin/detach-media
  197. ln -s /usr/bin/remove-music /usr/bin/remove-media
  198. ln -s /usr/bin/remove-music /usr/bin/detach-videos
  199. ln -s /usr/bin/remove-music /usr/bin/remove-videos
  200. ln -s /usr/bin/remove-music /usr/bin/detach-pictures
  201. ln -s /usr/bin/remove-music /usr/bin/remove-pictures
  202. echo 'script_for_attaching_usb_drive' >> $COMPLETION_FILE
  203. }
  204. function install_dlna {
  205. if grep -Fxq "install_dlna" $COMPLETION_FILE; then
  206. return
  207. fi
  208. install_dlna_main
  209. script_for_attaching_usb_drive
  210. echo 'install_dlna' >> $COMPLETION_FILE
  211. }
  212. # NOTE: deliberately no exit 0