Skip to content
Snippets Groups Projects
freedombone-app-dlna 9.24 KiB
Newer Older
Bob Mottram's avatar
Bob Mottram committed
#!/bin/bash
Bob Mottram's avatar
Bob Mottram committed
#  _____               _           _
# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
Bob Mottram's avatar
Bob Mottram committed
#
Bob Mottram's avatar
Bob Mottram committed
#                              Freedom in the Cloud
Bob Mottram's avatar
Bob Mottram committed
#
# DLNA application
#
Bob Mottram's avatar
Bob Mottram committed
# Deprecated because there are no DLNA clients within F-droid
# and Android is the main reason that DLNA was useful
#
Bob Mottram's avatar
Bob Mottram committed
# License
# =======
#
Bob Mottram's avatar
Bob Mottram committed
# Copyright (C) 2014-2019 Bob Mottram <bob@freedombone.net>
Bob Mottram's avatar
Bob Mottram committed
#
# 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/>.

Bob Mottram's avatar
Bob Mottram committed
VARIANTS='full full-vim media'
Bob Mottram's avatar
Bob Mottram committed
APP_CATEGORY=media

IN_DEFAULT_INSTALL=0
Bob Mottram's avatar
Bob Mottram committed
SHOW_CLEARNET_ADDRESS_ON_ABOUT=0
SHOW_DOMAIN_IN_WEBADMIN=0
NOT_ON_HOMEPAGE=1
REQUIRES_APP=
Bob Mottram's avatar
Bob Mottram committed
DLNA_PORT=1900
DLNA_PRESENTATION_PORT=8200
USB_MOUNT_DLNA=/mnt/dlna

DLNA_SHORT_DESCRIPTION=$'Play media from any UPNP/DLNA device on your local network'
DLNA_DESCRIPTION=$'Play media from any UPNP/DLNA device on your local network'
DLNA_MOBILE_APP_URL=https://f-droid.org/packages/de.yaacc/
Bob Mottram's avatar
Bob Mottram committed
dlna_variables=(SYSTEM_TYPE
                MY_USERNAME)

function logging_on_dlna {
    echo -n ''
}

function logging_off_dlna {
    echo -n ''
}

function configure_interactive_dlna {
    W=(1 $"Attach a drive containing playable media"
       2 $"Remove a drive containing playable media")

        # shellcheck disable=SC2068
        selection=$(dialog --backtitle $"Freedombone Administrator Control Panel" --title $"Media Menu" --menu $"Choose an operation, or ESC to exit:" 10 60 2 "${W[@]}" 3>&2 2>&1 1>&3)

        if [ ! "$selection" ]; then
            break
        fi

        case $selection in
Bob Mottram's avatar
Bob Mottram committed
            1) attach-dlna;;
            2) unmount-dlna;;
function install_interactive_dlna {
    echo -n ''
    APP_INSTALLED=1
function reconfigure_dlna {
function upgrade_dlna {
function configure_firewall_for_dlna {
Bob Mottram's avatar
Bob Mottram committed
    if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
Bob Mottram's avatar
Bob Mottram committed
    firewall_add DLNAP $DLNA_PRESENTATION_PORT tcp
Bob Mottram's avatar
Bob Mottram committed
    firewall_add DLNA $DLNA_PORT udp
Bob Mottram's avatar
Bob Mottram committed
    { 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>';
Bob Mottram's avatar
Bob Mottram committed
      echo '    <type>_dlna._tcp</type>';
      echo "    <port>$DLNA_PRESENTATION_PORT</port>";
      echo '  </service>';
      echo '  <service>';
Bob Mottram's avatar
Bob Mottram committed
      echo '    <type>_dlna._udp</type>';
Bob Mottram's avatar
Bob Mottram committed
      echo "    <port>$DLNA_PORT</port>";
Bob Mottram's avatar
Bob Mottram committed
      echo '  </service>';
      echo '</service-group>'; } > /etc/avahi/services/dlna.service
Bob Mottram's avatar
Bob Mottram committed

    systemctl restart avahi-daemon

Bob Mottram's avatar
Bob Mottram committed
    mark_completed "${FUNCNAME[0]}"
Bob Mottram's avatar
Bob Mottram committed
function backup_local_dlna {
    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
function restore_local_dlna {
    if [ -d /var/cache/minidlna ]; then
Bob Mottram's avatar
Bob Mottram committed
        if [ -d "$USB_MOUNT_DLNA/backup/dlna" ]; then
            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
Bob Mottram's avatar
Bob Mottram committed
            # shellcheck disable=SC2181
            if [ ! "$?" = "0" ]; then
                rm -rf $temp_restore_dir
                function_check set_user_permissions
                set_user_permissions
                function_check backup_unmount_drive
                backup_unmount_drive
            fi
            rm -rf $temp_restore_dir
        fi
    fi
Bob Mottram's avatar
Bob Mottram committed
function backup_remote_dlna {
    echo -n ''
}

function restore_remote_dlna {
    echo -n ''
Bob Mottram's avatar
Bob Mottram committed
function remove_dlna {
    systemctl stop minidlna
    $REMOVE_PACKAGES_PURGE minidlna
    if [ -f /etc/minidlna.conf ]; then
        rm /etc/minidlna.conf
    fi
Bob Mottram's avatar
Bob Mottram committed
    rm /usr/bin/attach-dlna
    rm /usr/bin/unmount-dlna
Bob Mottram's avatar
Bob Mottram committed
    remove_completion_param install_dlna
Bob Mottram's avatar
Bob Mottram committed
    firewall_remove $DLNA_PORT udp
Bob Mottram's avatar
Bob Mottram committed
    firewall_remove $DLNA_PRESENTATION_PORT tcp
Bob Mottram's avatar
Bob Mottram committed

    rm /etc/avahi/services/dlna.service
    systemctl restart avahi-daemon
Bob Mottram's avatar
Bob Mottram committed
function install_dlna_main {
Bob Mottram's avatar
Bob Mottram committed
    if [[ $(app_is_installed dlna_main) == "1" ]]; then
Bob Mottram's avatar
Bob Mottram committed
    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    $INSTALL_PACKAGES_BACKPORTS minidlna
Bob Mottram's avatar
Bob Mottram committed
    increment_app_install_progress

    if [ ! -f /etc/minidlna.conf ]; then
        echo $"ERROR: minidlna does not appear to have installed. $CHECK_MESSAGE"
        exit 55
    fi

Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d /music ]; then
        mkdir /music
    fi
    if [ ! -d /pictures ]; then
        mkdir /pictures
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d /videos ]; then
        mkdir /videos
Bob Mottram's avatar
Bob Mottram committed
    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    sed -i "s|media_dir=.*|media_dir=A,/music|g" /etc/minidlna.conf
    if ! grep -q "=P,/pictures" /etc/minidlna.conf; then
        echo "media_dir=P,/pictures" >> /etc/minidlna.conf
Bob Mottram's avatar
Bob Mottram committed
    if ! grep -q "=V,/videos" /etc/minidlna.conf; then
        echo "media_dir=V,/videos" >> /etc/minidlna.conf
    if ! grep -q "$USB_MOUNT_DLNA/Music" /etc/minidlna.conf; then
        echo "media_dir=A,$USB_MOUNT_DLNA/Music" >> /etc/minidlna.conf
    if ! grep -q "$USB_MOUNT_DLNA/Pictures" /etc/minidlna.conf; then
        echo "media_dir=P,$USB_MOUNT_DLNA/Pictures" >> /etc/minidlna.conf
    if ! grep -q "$USB_MOUNT_DLNA/Videos" /etc/minidlna.conf; then
        echo "media_dir=V,$USB_MOUNT_DLNA/Videos" >> /etc/minidlna.conf
    fi
    sed -i 's/#root_container=./root_container=B/g' /etc/minidlna.conf
Bob Mottram's avatar
Bob Mottram committed
    if [[ $SYSTEM_TYPE != "mesh"* ]]; then
        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
        sed -i "s/#network_interface=/network_interface=$WIFI_INTERFACE/g" /etc/minidlna.conf
Bob Mottram's avatar
Bob Mottram committed
    sed -i "s/#friendly_name=.*/friendly_name=\"${PROJECT_NAME} Media\"/g" /etc/minidlna.conf
    sed -i 's|#db_dir=.*|db_dir=/var/cache/minidlna|g' /etc/minidlna.conf
    sed -i 's/#inotify=.*/inotify=yes/g' /etc/minidlna.conf
    sed -i 's/#notify_interval=.*/notify_interval=300/g' /etc/minidlna.conf
    sed -i "s|#presentation_url=.*/|presentation_url=http://localhost:$DLNA_PRESENTATION_PORT|g" /etc/minidlna.conf
    systemctl reload minidlna
Bob Mottram's avatar
Bob Mottram committed
    increment_app_install_progress

    sed -i 's/fs.inotify.max_user_watches*/fs.inotify.max_user_watches=65536/g' /etc/sysctl.conf
Bob Mottram's avatar
Bob Mottram committed
    if ! grep -q "max_user_watches" "$COMPLETION_FILE"; then
        echo 'fs.inotify.max_user_watches=65536' >> /etc/sysctl.conf
    fi
Bob Mottram's avatar
Bob Mottram committed
    /sbin/sysctl -p -q
Bob Mottram's avatar
Bob Mottram committed
    increment_app_install_progress

    function_check configure_firewall_for_dlna
    configure_firewall_for_dlna
Bob Mottram's avatar
Bob Mottram committed

    increment_app_install_progress

Bob Mottram's avatar
Bob Mottram committed
    install_completed dlna_main
Bob Mottram's avatar
Bob Mottram committed
}

function script_for_attaching_usb_drive {
Bob Mottram's avatar
Bob Mottram committed
    if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
Bob Mottram's avatar
Bob Mottram committed
    { 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=\$(detect_connected_drives)";
Bob Mottram's avatar
Bob Mottram committed
      echo "if [[ \"\$USB_DRIVE\" != 'sd'* ]]; then";
      echo '  exit 1';
      echo 'fi';
      echo '';
      echo "if [ -d $USB_MOUNT_DLNA ]; then";
      echo "  umount $USB_MOUNT_DLNA";
      echo "  rm -rf $USB_MOUNT_DLNA";
      echo 'fi';
Bob Mottram's avatar
Bob Mottram committed
      echo '';
      echo "if [ ! -d $USB_MOUNT_DLNA ]; then";
Bob Mottram's avatar
Bob Mottram committed
      echo "  mkdir $USB_MOUNT_DLNA";
Bob Mottram's avatar
Bob Mottram committed
      echo 'fi';
Bob Mottram's avatar
Bob Mottram committed
      echo "mount \$USB_DRIVE $USB_MOUNT_DLNA";
Bob Mottram's avatar
Bob Mottram committed
      echo "chown root:root $USB_MOUNT_DLNA";
      echo "chown -R minidlna:minidlna $USB_MOUNT_DLNA/*";
      echo 'systemctl restart minidlna';
      echo 'minidlnad -R';
Bob Mottram's avatar
Bob Mottram committed
      echo 'exit 0'; } > /usr/bin/attach-dlna
    chmod +x /usr/bin/attach-dlna
Bob Mottram's avatar
Bob Mottram committed
    { echo '#!/bin/bash';
      echo "if [ -d $USB_MOUNT_DLNA ]; then";
      echo "  umount $USB_MOUNT_DLNA";
      echo "  rm -rf $USB_MOUNT_DLNA";
      echo 'fi';
Bob Mottram's avatar
Bob Mottram committed
      echo 'exit 0'; } > /usr/bin/unmount-dlna
    chmod +x /usr/bin/unmount-dlna
Bob Mottram's avatar
Bob Mottram committed
    mark_completed "${FUNCNAME[0]}"
Bob Mottram's avatar
Bob Mottram committed
function install_dlna {
    install_dlna_main
Bob Mottram's avatar
Bob Mottram committed

    increment_app_install_progress

    script_for_attaching_usb_drive
Bob Mottram's avatar
Bob Mottram committed
# NOTE: deliberately no exit 0