freedombone-app-dlna 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 full-vim media'
  31. dlna_variables=(SYSTEM_TYPE
  32. USB_MOUNT
  33. INSTALLED_WITHIN_DOCKER
  34. MY_USERNAME)
  35. function configure_interactive_dlna {
  36. while true
  37. do
  38. data=$(tempfile 2>/dev/null)
  39. trap "rm -f $data" 0 1 2 5 15
  40. dialog --backtitle $"Freedombone Control Panel" \
  41. --title $"Media Menu" \
  42. --radiolist $"Choose an operation:" 13 70 3 \
  43. 1 $"Attach a drive containing playable media" off \
  44. 2 $"Remove a drive containing playable media" off \
  45. 3 $"Exit" on 2> $data
  46. sel=$?
  47. case $sel in
  48. 1) break;;
  49. 255) break;;
  50. esac
  51. case $(cat $data) in
  52. 1) remove-music
  53. attach-music;;
  54. 2) remove-music;;
  55. 3) break;;
  56. esac
  57. done
  58. }
  59. function install_interactive_dlna {
  60. echo -n ''
  61. APP_INSTALLED=1
  62. }
  63. function change_password_dlna {
  64. echo -n ''
  65. }
  66. function reconfigure_dlna {
  67. echo ''
  68. }
  69. function upgrade_dlna {
  70. echo ''
  71. }
  72. function configure_firewall_for_dlna {
  73. if grep -Fxq "configure_firewall_for_dlna" $COMPLETION_FILE; then
  74. return
  75. fi
  76. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  77. # docker does its own firewalling
  78. return
  79. fi
  80. iptables -A INPUT -p udp --dport 1900 -j ACCEPT
  81. iptables -A INPUT -p tcp --dport 8200 -j ACCEPT
  82. function_check save_firewall_settings
  83. save_firewall_settings
  84. OPEN_PORTS+=('DLNA 1900')
  85. OPEN_PORTS+=('DLNA 8200')
  86. echo 'configure_firewall_for_dlna' >> $COMPLETION_FILE
  87. }
  88. function backup_local_dlna {
  89. source_directory=/var/cache/minidlna
  90. if [ -d $source_directory ]; then
  91. dest_directory=dlna
  92. echo $"Backing up $source_directory to $dest_directory"
  93. function_check backup_directory_to_usb
  94. backup_directory_to_usb $source_directory $dest_directory
  95. echo $"Backup to $dest_directory complete"
  96. fi
  97. }
  98. function restore_local_dlna {
  99. if [ -d /var/cache/minidlna ]; then
  100. if [ -d $USB_MOUNT/backup/dlna ]; then
  101. echo $"Restoring DLNA cache"
  102. temp_restore_dir=/root/tempdlna
  103. function_check restore_directory_from_usb
  104. restore_directory_from_usb $temp_restore_dir dlna
  105. cp -r $temp_restore_dir/var/cache/minidlna/* /var/cache/minidlna/
  106. if [ ! "$?" = "0" ]; then
  107. rm -rf $temp_restore_dir
  108. function_check set_user_permissions
  109. set_user_permissions
  110. function_check backup_unmount_drive
  111. backup_unmount_drive
  112. exit 982572
  113. fi
  114. rm -rf $temp_restore_dir
  115. fi
  116. fi
  117. }
  118. function backup_remote_dlna {
  119. if [ -d /var/cache/minidlna ]; then
  120. echo $"Backing up DLNA cache"
  121. backup_directory_to_friend /var/cache/minidlna dlna
  122. echo $"Backup of DLNA cache complete"
  123. fi
  124. }
  125. function restore_remote_dlna {
  126. if [ -d /var/cache/minidlna ]; then
  127. if [ -d $SERVER_DIRECTORY/backup/dlna ]; then
  128. echo $"Restoring DLNA cache"
  129. temp_restore_dir=/root/tempdlna
  130. function_check restore_directory_from_friend
  131. restore_directory_from_friend $temp_restore_dir dlna
  132. cp -r $temp_restore_dir/var/cache/minidlna/* /var/cache/minidlna/
  133. if [ ! "$?" = "0" ]; then
  134. exit 982
  135. fi
  136. rm -rf $temp_restore_dir
  137. echo $"Restore of DLNA complete"
  138. fi
  139. fi
  140. }
  141. function remove_dlna {
  142. service minidlna stop
  143. apt-get -y remove --purge minidlna
  144. if [ -f /etc/minidlna.conf ]; then
  145. rm /etc/minidlna.conf
  146. fi
  147. iptables -D INPUT -p udp --dport 1900 -j ACCEPT
  148. iptables -D INPUT -p tcp --dport 8200 -j ACCEPT
  149. function_check save_firewall_settings
  150. save_firewall_settings
  151. sed -i '/install_dlna/d' $COMPLETION_FILE
  152. }
  153. function install_dlna_main {
  154. if grep -Fxq "install_dlna_main" $COMPLETION_FILE; then
  155. return
  156. fi
  157. apt-get -y install minidlna
  158. if [ ! -f /etc/minidlna.conf ]; then
  159. echo $"ERROR: minidlna does not appear to have installed. $CHECK_MESSAGE"
  160. exit 55
  161. fi
  162. sed -i "s|media_dir=/var/lib/minidlna|media_dir=A,/home/$MY_USERNAME/Music|g" /etc/minidlna.conf
  163. if ! grep -q "/home/$MY_USERNAME/Pictures" /etc/minidlna.conf; then
  164. echo "media_dir=P,/home/$MY_USERNAME/Pictures" >> /etc/minidlna.conf
  165. fi
  166. if ! grep -q "/home/$MY_USERNAME/Videos" /etc/minidlna.conf; then
  167. echo "media_dir=V,/home/$MY_USERNAME/Videos" >> /etc/minidlna.conf
  168. fi
  169. if ! grep -q "$USB_MOUNT/Music" /etc/minidlna.conf; then
  170. echo "media_dir=A,$USB_MOUNT/Music" >> /etc/minidlna.conf
  171. fi
  172. if ! grep -q "$USB_MOUNT/Pictures" /etc/minidlna.conf; then
  173. echo "media_dir=P,$USB_MOUNT/Pictures" >> /etc/minidlna.conf
  174. fi
  175. if ! grep -q "$USB_MOUNT/Videos" /etc/minidlna.conf; then
  176. echo "media_dir=V,$USB_MOUNT/Videos" >> /etc/minidlna.conf
  177. fi
  178. sed -i 's/#root_container=./root_container=B/g' /etc/minidlna.conf
  179. if [[ $SYSTEM_TYPE != "mesh"* ]]; then
  180. sed -i 's/#network_interface=/network_interface=eth0/g' /etc/minidlna.conf
  181. else
  182. sed -i 's/#network_interface=/network_interface=$WIFI_INTERFACE/g' /etc/minidlna.conf
  183. fi
  184. sed -i "s/#friendly_name=/friendly_name=\"${PROJECT_NAME} Media\"/g" /etc/minidlna.conf
  185. sed -i 's|#db_dir=/var/cache/minidlna|db_dir=/var/cache/minidlna|g' /etc/minidlna.conf
  186. sed -i 's/#inotify=yes/inotify=yes/g' /etc/minidlna.conf
  187. sed -i 's/#notify_interval=895/notify_interval=300/g' /etc/minidlna.conf
  188. sed -i "s|#presentation_url=/|presentation_url=http://localhost:8200|g" /etc/minidlna.conf
  189. service minidlna force-reload
  190. service minidlna reload
  191. sed -i 's/fs.inotify.max_user_watches*/fs.inotify.max_user_watches=65536/g' /etc/sysctl.conf
  192. if ! grep -q "max_user_watches" $COMPLETION_FILE; then
  193. echo 'fs.inotify.max_user_watches=65536' >> /etc/sysctl.conf
  194. fi
  195. /sbin/sysctl -p -q
  196. function_check configure_firewall_for_dlna
  197. configure_firewall_for_dlna
  198. echo 'install_dlna_main' >> $COMPLETION_FILE
  199. }
  200. function script_for_attaching_usb_drive {
  201. if grep -Fxq "script_for_attaching_usb_drive" $COMPLETION_FILE; then
  202. return
  203. fi
  204. echo '#!/bin/bash' > /usr/bin/attach-music
  205. echo 'remove-music' >> /usr/bin/attach-music
  206. echo "if [ ! -d $USB_MOUNT ]; then" >> /usr/bin/attach-music
  207. echo " mkdir $USB_MOUNT" >> /usr/bin/attach-music
  208. echo 'fi' >> /usr/bin/attach-music
  209. echo "mount /dev/sda1 $USB_MOUNT" >> /usr/bin/attach-music
  210. echo "chown root:root $USB_MOUNT" >> /usr/bin/attach-music
  211. echo "chown -R minidlna:minidlna $USB_MOUNT/*" >> /usr/bin/attach-music
  212. echo 'service minidlna restart' >> /usr/bin/attach-music
  213. echo 'minidlnad -R' >> /usr/bin/attach-music
  214. chmod +x /usr/bin/attach-music
  215. ln -s /usr/bin/attach-music /usr/bin/attach-usb
  216. ln -s /usr/bin/attach-music /usr/bin/attach-videos
  217. ln -s /usr/bin/attach-music /usr/bin/attach-pictures
  218. ln -s /usr/bin/attach-music /usr/bin/attach-media
  219. echo '#!/bin/bash' > /usr/bin/remove-music
  220. echo "if [ -d $USB_MOUNT ]; then" >> /usr/bin/remove-music
  221. echo " umount $USB_MOUNT" >> /usr/bin/remove-music
  222. echo " rm -rf $USB_MOUNT" >> /usr/bin/remove-music
  223. echo 'fi' >> /usr/bin/remove-music
  224. chmod +x /usr/bin/remove-music
  225. ln -s /usr/bin/remove-music /usr/bin/detach-music
  226. ln -s /usr/bin/remove-music /usr/bin/detach-usb
  227. ln -s /usr/bin/remove-music /usr/bin/remove-usb
  228. ln -s /usr/bin/remove-music /usr/bin/detach-media
  229. ln -s /usr/bin/remove-music /usr/bin/remove-media
  230. ln -s /usr/bin/remove-music /usr/bin/detach-videos
  231. ln -s /usr/bin/remove-music /usr/bin/remove-videos
  232. ln -s /usr/bin/remove-music /usr/bin/detach-pictures
  233. ln -s /usr/bin/remove-music /usr/bin/remove-pictures
  234. echo 'script_for_attaching_usb_drive' >> $COMPLETION_FILE
  235. }
  236. function install_dlna {
  237. install_dlna_main
  238. script_for_attaching_usb_drive
  239. APP_INSTALLED=1
  240. }
  241. # NOTE: deliberately no exit 0