freedombone/src/freedombone-app-dlna

307 lines
9.5 KiB
Plaintext
Raw Normal View History

2016-07-03 17:13:34 +02:00
#!/bin/bash
#
# .---. . .
# | | |
# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
#
# Freedom in the Cloud
#
# DLNA application
#
# License
# =======
#
2018-02-21 20:32:13 +01:00
# Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
2016-07-03 17:13:34 +02:00
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2016-10-12 11:49:12 +02:00
VARIANTS='full full-vim media'
2016-07-06 17:47:55 +02:00
IN_DEFAULT_INSTALL=0
SHOW_ON_ABOUT=0
2016-10-05 23:33:41 +02:00
dlna_variables=(SYSTEM_TYPE
2016-11-12 21:19:58 +01:00
USB_MOUNT_DLNA
2016-10-05 23:33:41 +02:00
INSTALLED_WITHIN_DOCKER
MY_USERNAME)
function logging_on_dlna {
echo -n ''
}
function logging_off_dlna {
echo -n ''
}
function configure_interactive_dlna {
while true
do
2018-02-26 14:50:40 +01:00
data=$(mktemp 2>/dev/null)
dialog --backtitle $"Freedombone Control Panel" \
--title $"Media Menu" \
--radiolist $"Choose an operation:" 13 70 3 \
1 $"Attach a drive containing playable media" off \
2 $"Remove a drive containing playable media" off \
2018-02-26 14:50:40 +01:00
3 $"Exit" on 2> "$data"
sel=$?
case $sel in
2018-02-26 14:50:40 +01:00
1) rm -f "$data"
break;;
255) rm -f "$data"
break;;
esac
case $(cat $data) in
2016-11-12 21:19:58 +01:00
1) attach-music;;
2) remove-music;;
2018-02-26 14:50:40 +01:00
3) rm -f "$data"
break;;
esac
2018-02-26 14:50:40 +01:00
rm -f "$data"
done
}
function install_interactive_dlna {
echo -n ''
APP_INSTALLED=1
}
2016-07-09 12:36:12 +02:00
function reconfigure_dlna {
2016-09-25 12:20:41 +02:00
echo ''
2016-07-09 12:36:12 +02:00
}
2016-07-06 16:01:28 +02:00
function upgrade_dlna {
2016-09-25 12:20:41 +02:00
echo ''
2016-07-06 16:01:28 +02:00
}
function configure_firewall_for_dlna {
2018-02-25 13:50:46 +01:00
if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
2016-09-25 12:20:41 +02:00
return
fi
if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
# docker does its own firewalling
return
fi
2016-10-22 11:39:34 +02:00
firewall_add DLNA 1900 udp
firewall_add DLNA 8200 tcp
2018-01-30 15:40:37 +01:00
2018-02-26 14:50:40 +01:00
{ echo '<?xml version="1.0" standalone="no"?><!--*-nxml-*-->';
echo '<!DOCTYPE service-group SYSTEM "avahi-service.dtd">';
echo '<service-group>';
echo ' <name replace-wildcards="yes">%h DLNA</name>';
echo ' <service>';
echo ' <type>_dlna._tcp</type>';
echo " <port>8200</port>";
echo ' </service>';
echo ' <service>';
echo ' <type>_dlna._udp</type>';
echo " <port>1900</port>";
echo ' </service>';
echo '</service-group>'; } > /etc/avahi/services/dlna.service
2018-01-30 15:40:37 +01:00
systemctl restart avahi-daemon
2018-02-25 13:50:46 +01:00
mark_completed "${FUNCNAME[0]}"
}
2016-07-06 15:55:09 +02:00
function backup_local_dlna {
2016-09-25 12:20:41 +02:00
source_directory=/var/cache/minidlna
if [ -d $source_directory ]; then
dest_directory=dlna
function_check backup_directory_to_usb
backup_directory_to_usb $source_directory $dest_directory
fi
2016-07-06 15:55:09 +02:00
}
2016-07-08 17:38:26 +02:00
function restore_local_dlna {
2016-09-25 12:20:41 +02:00
if [ -d /var/cache/minidlna ]; then
2018-02-26 14:50:40 +01:00
if [ -d "$USB_MOUNT_DLNA/backup/dlna" ]; then
2016-09-25 12:20:41 +02:00
echo $"Restoring DLNA cache"
temp_restore_dir=/root/tempdlna
function_check restore_directory_from_usb
restore_directory_from_usb $temp_restore_dir dlna
if [ -d $temp_restore_dir/var/cache/minidlna ]; then
cp -r $temp_restore_dir/var/cache/minidlna/* /var/cache/minidlna/
else
cp -r $temp_restore_dir/* /var/cache/minidlna/
fi
2018-02-26 14:50:40 +01:00
# shellcheck disable=SC2181
2016-09-25 12:20:41 +02:00
if [ ! "$?" = "0" ]; then
rm -rf $temp_restore_dir
function_check set_user_permissions
set_user_permissions
function_check backup_unmount_drive
backup_unmount_drive
exit 982572
fi
rm -rf $temp_restore_dir
fi
fi
2016-07-08 17:38:26 +02:00
}
2016-07-06 15:55:09 +02:00
function backup_remote_dlna {
2016-09-25 12:20:41 +02:00
if [ -d /var/cache/minidlna ]; then
backup_directory_to_friend /var/cache/minidlna dlna
fi
2016-07-08 17:38:26 +02:00
}
function restore_remote_dlna {
2016-09-25 12:20:41 +02:00
if [ -d /var/cache/minidlna ]; then
2018-02-26 14:50:40 +01:00
if [ -d "$SERVER_DIRECTORY/backup/dlna" ]; then
2016-09-25 12:20:41 +02:00
temp_restore_dir=/root/tempdlna
function_check restore_directory_from_friend
restore_directory_from_friend $temp_restore_dir dlna
if [ -d $temp_restore_dir/var/cache/minidlna ]; then
cp -r $temp_restore_dir/var/cache/minidlna/* /var/cache/minidlna/
else
cp -r $temp_restore_dir/* /var/cache/minidlna/
fi
2018-02-26 14:50:40 +01:00
# shellcheck disable=SC2181
2016-09-25 12:20:41 +02:00
if [ ! "$?" = "0" ]; then
exit 982
fi
rm -rf $temp_restore_dir
fi
fi
2016-07-04 22:02:22 +02:00
}
2016-07-05 21:07:43 +02:00
function remove_dlna {
2016-11-12 21:19:58 +01:00
systemctl stop minidlna
2016-10-23 20:38:14 +02:00
apt-get -yq remove --purge minidlna
2016-09-25 12:20:41 +02:00
if [ -f /etc/minidlna.conf ]; then
rm /etc/minidlna.conf
fi
2016-11-12 21:19:58 +01:00
rm /usr/bin/attach-music
rm /usr/bin/remove-music
2016-10-17 15:44:49 +02:00
remove_completion_param install_dlna
2016-10-22 11:39:34 +02:00
firewall_remove 1900 udp
firewall_remove 8200 tcp
2018-01-30 15:40:37 +01:00
rm /etc/avahi/services/dlna.service
systemctl restart avahi-daemon
2016-07-03 21:29:52 +02:00
}
2016-07-06 15:43:20 +02:00
function install_dlna_main {
2016-10-16 20:50:56 +02:00
if [[ $(app_is_installed dlna_main) == "1" ]]; then
2016-09-25 12:20:41 +02:00
return
fi
2016-10-23 20:38:14 +02:00
apt-get -yq install minidlna
2016-09-25 12:20:41 +02:00
if [ ! -f /etc/minidlna.conf ]; then
echo $"ERROR: minidlna does not appear to have installed. $CHECK_MESSAGE"
exit 55
fi
2018-02-26 14:50:40 +01:00
if [ ! "$USB_MOUNT_DLNA" ]; then
2016-11-12 21:19:58 +01:00
USB_MOUNT_DLNA=/mnt/dlna
fi
if [ ${#USB_MOUNT_DLNA} -eq 0 ]; then
USB_MOUNT_DLNA=/mnt/dlna
fi
2016-09-25 12:20:41 +02:00
sed -i "s|media_dir=/var/lib/minidlna|media_dir=A,/home/$MY_USERNAME/Music|g" /etc/minidlna.conf
if ! grep -q "/home/$MY_USERNAME/Pictures" /etc/minidlna.conf; then
echo "media_dir=P,/home/$MY_USERNAME/Pictures" >> /etc/minidlna.conf
fi
if ! grep -q "/home/$MY_USERNAME/Videos" /etc/minidlna.conf; then
echo "media_dir=V,/home/$MY_USERNAME/Videos" >> /etc/minidlna.conf
fi
2016-11-12 21:19:58 +01:00
if ! grep -q "$USB_MOUNT_DLNA/Music" /etc/minidlna.conf; then
echo "media_dir=A,$USB_MOUNT_DLNA/Music" >> /etc/minidlna.conf
2016-09-25 12:20:41 +02:00
fi
2016-11-12 21:19:58 +01:00
if ! grep -q "$USB_MOUNT_DLNA/Pictures" /etc/minidlna.conf; then
echo "media_dir=P,$USB_MOUNT_DLNA/Pictures" >> /etc/minidlna.conf
2016-09-25 12:20:41 +02:00
fi
2016-11-12 21:19:58 +01:00
if ! grep -q "$USB_MOUNT_DLNA/Videos" /etc/minidlna.conf; then
echo "media_dir=V,$USB_MOUNT_DLNA/Videos" >> /etc/minidlna.conf
2016-09-25 12:20:41 +02:00
fi
sed -i 's/#root_container=./root_container=B/g' /etc/minidlna.conf
2016-10-08 20:32:04 +02:00
if [[ $SYSTEM_TYPE != "mesh"* ]]; then
2016-10-21 11:43:50 +02:00
if [[ $(config_param_exists WIFI_INTERFACE) == "0" ]]; then
sed -i 's/#network_interface=/network_interface=eth0/g' /etc/minidlna.conf
else
sed -i "s/#network_interface=/network_interface=$WIFI_INTERFACE/g" /etc/minidlna.conf
fi
2016-09-25 12:20:41 +02:00
else
2016-10-21 11:43:50 +02:00
sed -i "s/#network_interface=/network_interface=$WIFI_INTERFACE/g" /etc/minidlna.conf
2016-09-25 12:20:41 +02:00
fi
sed -i "s/#friendly_name=/friendly_name=\"${PROJECT_NAME} Media\"/g" /etc/minidlna.conf
sed -i 's|#db_dir=/var/cache/minidlna|db_dir=/var/cache/minidlna|g' /etc/minidlna.conf
sed -i 's/#inotify=yes/inotify=yes/g' /etc/minidlna.conf
sed -i 's/#notify_interval=895/notify_interval=300/g' /etc/minidlna.conf
sed -i "s|#presentation_url=/|presentation_url=http://localhost:8200|g" /etc/minidlna.conf
2016-11-12 21:19:58 +01:00
systemctl reload minidlna
2016-09-25 12:20:41 +02:00
sed -i 's/fs.inotify.max_user_watches*/fs.inotify.max_user_watches=65536/g' /etc/sysctl.conf
2018-02-26 14:50:40 +01:00
if ! grep -q "max_user_watches" "$COMPLETION_FILE"; then
2016-09-25 12:20:41 +02:00
echo 'fs.inotify.max_user_watches=65536' >> /etc/sysctl.conf
fi
2016-09-29 13:50:00 +02:00
/sbin/sysctl -p -q
2016-09-25 12:20:41 +02:00
function_check configure_firewall_for_dlna
configure_firewall_for_dlna
2016-10-16 20:50:56 +02:00
install_completed dlna_main
2016-07-03 17:13:34 +02:00
}
function script_for_attaching_usb_drive {
2018-02-25 13:50:46 +01:00
if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
2016-09-25 12:20:41 +02:00
return
fi
2018-02-26 14:50:40 +01:00
{ echo '#!/bin/bash';
echo "source /usr/local/bin/${PROJECT_NAME}-vars";
echo "UTILS_FILES=/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*";
echo '';
echo "for f in \$UTILS_FILES";
echo 'do';
echo " source \$f";
echo 'done';
echo '';
echo 'USB_DRIVE=/dev/sda1';
echo 'detect_usb_drive';
echo '';
echo 'remove-music';
echo "if [ ! -d $USB_MOUNT_DLNA ]; then";
echo " mkdir $USB_MOUNT_DLNA";
echo 'fi';
echo -n "mount \$USB_DRIVE ";
echo "$USB_MOUNT_DLNA";
echo "chown root:root $USB_MOUNT_DLNA";
echo "chown -R minidlna:minidlna $USB_MOUNT_DLNA/*";
echo 'systemctl restart minidlna';
echo 'minidlnad -R';
echo 'exit 0'; } > /usr/bin/attach-music
2016-09-25 12:20:41 +02:00
chmod +x /usr/bin/attach-music
2018-02-26 14:50:40 +01:00
{ echo '#!/bin/bash';
echo "if [ -d $USB_MOUNT_DLNA ]; then";
echo " umount $USB_MOUNT_DLNA";
echo " rm -rf $USB_MOUNT_DLNA";
echo 'fi';
echo 'exit 0'; } > /usr/bin/remove-music
2016-09-25 12:20:41 +02:00
chmod +x /usr/bin/remove-music
2018-02-25 13:50:46 +01:00
mark_completed "${FUNCNAME[0]}"
2016-07-03 17:13:34 +02:00
}
2016-07-06 15:43:20 +02:00
function install_dlna {
2016-09-25 12:20:41 +02:00
install_dlna_main
script_for_attaching_usb_drive
APP_INSTALLED=1
2016-07-06 15:43:20 +02:00
}
2016-07-03 17:13:34 +02:00
# NOTE: deliberately no exit 0