| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765 | #!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Icecast application
#
# Notes: An attempt was made to get ices2 running with systemd, but that
#        was very unsuccessful. Instead there's a hacky cron entry which
#        starts icecast2 and ices2 if necessary
#
# License
# =======
#
# Copyright (C) 2017-2018 Bob Mottram <bob@freedombone.net>
#
# 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/>.
VARIANTS='full'
IN_DEFAULT_INSTALL=0
SHOW_ON_ABOUT=1
SHOW_ICANN_ADDRESS_ON_ABOUT=0
ICECAST_DOMAIN_NAME=
ICECAST_CODE=
ICECAST_PORT=8005
ICECAST_ONION_PORT=8146
ICECAST_DIR=/icestream
ICECAST_PLAYLIST_FILE=/etc/ices2/playlist.txt
ICECAST_LOGIN_TEXT=$"Icecast login"
icecast_variables=(MY_USERNAME
                   MY_EMAIL_ADDRESS
                   ONION_ONLY
                   ICECAST_DOMAIN_NAME
                   ICECAST_CODE
                   DEFAULT_LANGUAGE)
function icecast_rescan {
    if [ -d $ICECAST_DIR ]; then
        if [ -f $ICECAST_PLAYLIST_FILE ]; then
            rm $ICECAST_PLAYLIST_FILE
        fi
        icecast_add_file_to_playlist $ICECAST_DIR
    fi
}
function icecast_update_daemon {
    systemctl stop icecast2
    if [ -f /etc/init.d/icecast2 ]; then
        rm /etc/init.d/icecast2
    fi
    echo '#!/bin/sh' > /usr/bin/stop_icecast
    echo 'kill $(pidof ices2)' >> /usr/bin/stop_icecast
    echo 'systemctl stop icecast2' >> /usr/bin/stop_icecast
    chmod +x /usr/bin/stop_icecast
    # Note that the sleep here actually is important
    echo '#!/bin/bash' > /usr/bin/start_icecast
    echo 'isrunning=$(ps aux | grep ices2)' >> /usr/bin/start_icecast
    echo 'if [[ "$isrunning" != *"ices-playlist"* ]]; then' >> /usr/bin/start_icecast
    echo '    systemctl start icecast2' >> /usr/bin/start_icecast
    echo '    sleep 3' >> /usr/bin/start_icecast
    echo '    cd /etc/ices2' >> /usr/bin/start_icecast
    echo '    ices2 ices-playlist.xml' >> /usr/bin/start_icecast
    echo 'fi' >> /usr/bin/start_icecast
    chmod +x /usr/bin/start_icecast
    echo '[Unit]' > /etc/systemd/system/icecast2.service
    echo 'Description=Icecast' >> /etc/systemd/system/icecast2.service
    echo 'After=network.target' >> /etc/systemd/system/icecast2.service
    echo 'After=tor.service' >> /etc/systemd/system/icecast2.service
    echo '' >> /etc/systemd/system/icecast2.service
    echo '[Service]' >> /etc/systemd/system/icecast2.service
    echo 'User=icecast2' >> /etc/systemd/system/icecast2.service
    echo 'Group=icecast' >> /etc/systemd/system/icecast2.service
    echo 'ExecStart=/usr/bin/icecast2 -c /etc/icecast2/icecast.xml' >> /etc/systemd/system/icecast2.service
    echo 'Restart=on-failure' >> /etc/systemd/system/icecast2.service
    echo 'RestartSec=10' >> /etc/systemd/system/icecast2.service
    echo '' >> /etc/systemd/system/icecast2.service
    echo '[Install]' >> /etc/systemd/system/icecast2.service
    echo 'WantedBy=multi-user.target' >> /etc/systemd/system/icecast2.service
    chown -R icecast2:icecast /etc/ices2
    chown -R icecast2:icecast /etc/icecast2
    systemctl daemon-reload
    systemctl enable icecast2
    if ! grep -q "start_icecast" /etc/crontab; then
        echo '*/1            * *   *   *   root /usr/bin/start_icecast > /dev/null' >> /etc/crontab
    fi
}
function change_password_icecast {
    curr_username="$1"
    new_user_password="$2"
    stop_icecast
    sed -i  -e "s|<source-password>[^<]*</source-password>|<source-password>$new_user_password</source-password>|" \
        -e "s|<relay-password>[^<]*</relay-password>|<relay-password>$new_user_password</relay-password>|" \
        -e "s|<admin-password>[^<]*</admin-password>|<admin-password>$new_user_password</admin-password>|" \
        /etc/icecast2/icecast.xml
    sed -i "s|<password>.*|<password>${new_user_password}</password>|g" /etc/ices2/ices-playlist.xml
    ${PROJECT_NAME}-pass -u "$curr_username" -a icecast -p "$new_user_password"
    start_icecast
}
function logging_on_icecast {
    echo -n ''
}
function logging_off_icecast {
    echo -n ''
}
function reconfigure_icecast {
    echo -n ''
}
function icecast_convert_files {
    clear
    cd ${1}
    echo $'Converting any mp3 files to ogg format'
    find . -type f -name '*.mp3' -exec bash -c 'ffmpeg -i "$0" -c:a libvorbis -q:a 4 "${0/%mp3/ogg}"' '{}' \;
    find . -name "*.mp3" -print0 | xargs -0 rm -f
    echo $'Converting any mp4 files to ogv format'
    find . -type f -name '*.mp4' -exec bash -c 'ffmpeg -i "$0" -c:a libvorbis -q:a 4 "${0/%mp3/ogv}"' '{}' \;
    find . -name "*.mp4" -print0 | xargs -0 rm -f
    chown -R icecast2:icecast $ICECAST_DIR
}
function icecast_add_file_to_playlist {
    files_dir=${1}
    if [ ! -d $files_dir ]; then
        return
    fi
    echo $'Adding ogg files to playlist'
    find $files_dir -type f -name '*.ogg' -print0 | while read -d $'\0' file; do
        if ! grep -q "$file" $ICECAST_PLAYLIST_FILE; then
            echo "$file" >> $ICECAST_PLAYLIST_FILE
        fi
    done
    echo $'Adding ogv files to playlist'
    find $files_dir -type f -name '*.ogv' -print0 | while read -d $'\0' file; do
        if ! grep -q "$file" $ICECAST_PLAYLIST_FILE; then
            echo "$file" >> $ICECAST_PLAYLIST_FILE
        fi
    done
    chown icecast2:icecast $ICECAST_PLAYLIST_FILE
    stop_icecast
    start_icecast
}
function icecast_import_from_directory {
    data=$(tempfile 2>/dev/null)
    dialog --title "Choose a directory containing stream files" --dselect /home/$MY_USERNAME/ 30 60 2> $data
    selected_dir=$(cat $data)
    rm $data
    if [[ "$selected_dir" == "$ICECAST_DIR" ]]; then
        return
    fi
    if [ ! -d $selected_dir ]; then
        return
    fi
    if [[ "$selected_dir" == "/home/$MY_USERNAME/" ]]; then
        return
    fi
    if [[ "$selected_dir" == "/home/$MY_USERNAME/."* ]]; then
        return
    fi
    if [[ "$selected_dir" == *"/Maildir" || "$selected_dir" == *"/Sync" ]]; then
        return
    fi
    dialog --title $"Import stream files directory into Icecast" \
           --backtitle $"Freedombone Control Panel" \
           --defaultno \
           --yesno $"\nImport the directory:\n\n  $selected_dir" 12 75
    sel=$?
    case $sel in
        1) return;;
        255) return;;
    esac
    if [ ! -d $ICECAST_DIR ]; then
        mkdir -p $ICECAST_DIR
    fi
    dest_dir=$(basename "$selected_dir")
    mv "$selected_dir" $ICECAST_DIR
    icecast_convert_files $ICECAST_DIR/$dest_dir
    icecast_add_file_to_playlist $ICECAST_DIR/$dest_dir
    dialog --title $"Import stream files directory into Icecast" \
           --msgbox $"Import success" 6 40
}
function icecast_import_from_usb {
    clear
    detect_usb_drive
    if [ ! -b $USB_DRIVE ]; then
        dialog --title $"Import stream files from USB drive" --msgbox $'No USB drive found' 6 50
        return
    fi
    backup_mount_drive ${USB_DRIVE}
    if [ ! -d $USB_MOUNT$ICECAST_DIR ]; then
        dialog --title $"Import stream files from USB drive" --msgbox $'No stream files directory found on USB drive' 6 50
        backup_unmount_drive ${USB_DRIVE}
    fi
    cp -ru $USB_MOUNT$ICECAST_DIR/* $ICECAST_DIR
    backup_unmount_drive ${USB_DRIVE}
    icecast_convert_files $ICECAST_DIR
    dialog --title $"Import stream files from USB drive" --msgbox $'Import complete. You may now remove the USB drive' 6 50
}
function icecast_export_to_usb {
    clear
    detect_usb_drive
    if [ ! -b $USB_DRIVE ]; then
        dialog --title $"Export stream files to USB drive" --msgbox $'No USB drive found' 6 50
        return
    fi
    backup_mount_drive ${USB_DRIVE}
    if [ ! -d $USB_MOUNT$ICECAST_DIR ]; then
        mkdir -p $USB_MOUNT$ICECAST_DIR
    fi
    cp -ru $ICECAST_DIR/* $USB_MOUNT$ICECAST_DIR
    backup_unmount_drive ${USB_DRIVE}
    dialog --title $"Export stream files to USB drive" --msgbox $'Export complete. You may now remove the USB drive' 6 50
}
function icecast_format_drive {
    detect_usb_drive
    data=$(tempfile 2>/dev/null)
    trap "rm -f $data" 0 1 2 5 15
    dialog --title $"Format USB drive $USB_DRIVE for stream file storage" \
           --backtitle $"Freedombone Control Panel" \
           --defaultno \
           --yesno $"\nPlease confirm that you wish to format drive\n\n    ${USB_DRIVE}\n\nAll current data on the drive will be lost, and you will be prompted to give a password used to encrypt the drive.\n\nDANGER: If you screw up here and format the wrong drive it's your own fault!" 16 60
    sel=$?
    case $sel in
        1) return;;
        255) return;;
    esac
    rm $data
    clear
    echo ''
    echo $"Formatting drive $USB_DRIVE. ALL CONTENTS WILL BE LOST."
    echo ''
    ${PROJECT_NAME}-format $USB_DRIVE
    dialog --title $"Format USB drive $USB_DRIVE for stream file storage" --msgbox $'Format complete. You may now export stream files or remove the USB drive' 6 50
}
function icecast_edit_playlist {
    editor $ICECAST_PLAYLIST_FILE
    stop_icecast
    start_icecast
}
function icecast_change_login {
    read_config_param $MY_USERNAME
    ICECAST_USER_PASSWORD=$(${PROJECT_NAME}-pass -u $MY_USERNAME -a icecastuser)
    data=$(tempfile 2>/dev/null)
    trap "rm -f $data" 0 1 2 5 15
    dialog --title $"Change Icecast stream visitor login" \
           --backtitle $"Freedombone Control Panel" \
           --inputbox $"Enter the new login password for stream visitors" 8 60 "$ICECAST_USER_PASSWORD" 2>$data
    sel=$?
    case $sel in
        0)  ICECAST_USER_PASSWORD=$(<$data)
            if [[ "$ICECAST_USER_PASSWORD" != *' '* ]]; then
                if [ ${#ICECAST_USER_PASSWORD} -gt 8 ]; then
                    ${PROJECT_NAME}-pass -u $MY_USERNAME -a icecastuser -p $ICECAST_USER_PASSWORD
                    dialog --title $"Change Icecast stream visitor login" \
                           --msgbox $"Password changed to $ICECAST_USER_PASSWORD" 6 75
                fi
            fi
            ;;
    esac
    rm $data
}
function icecast_enable_login {
    dialog --title $"Enable Icecast login" \
           --backtitle $"Freedombone Control Panel" \
           --defaultno \
           --yesno $"\nDo you want to add a login so that random web users can't access your stream?" 10 60
    sel=$?
    case $sel in
        0) if grep -q '#auth_basic' /etc/nginx/sites-available/icecast; then
               sed -i 's|#auth_basic|auth_basic|g' /etc/nginx/sites-available/icecast
               systemctl restart nginx
           fi
           read_config_param $MY_USERNAME
           ICECAST_USER_PASSWORD=$(${PROJECT_NAME}-pass -u $MY_USERNAME -a icecastuser)
           dialog --title $"Enable Icecast login" \
                  --msgbox $"Icecast logins are now enabled with the password $ICECAST_USER_PASSWORD" 6 65
           ICECAST_USER_PASSWORD=
           ;;
        1) if ! grep -q '#auth_basic' /etc/nginx/sites-available/icecast; then
               sed -i 's|auth_basic|#auth_basic|g' /etc/nginx/sites-available/icecast
               systemctl restart nginx
           fi
           dialog --title $"Disable Icecast login" \
                  --msgbox $"Icecast logins are now disabled. Anyone can access your stream." 6 65
           ;;
    esac
}
function icecast_set_stream_name {
    data=$(tempfile 2>/dev/null)
    trap "rm -f $data" 0 1 2 5 15
    dialog --backtitle $"Freedombone Control Panel" \
           --title $"Change Icecast stream details" \
           --form "\n" 8 60 4 \
           $"Stream name:" 1 1 "Example stream name" 1 18 40 1000 \
           $"Description:" 2 1 "A short description of your stream" 2 18 40 1000 \
           $"Genre:" 3 1 "Example genre" 3 18 40 1000 \
           2> $data
    sel=$?
    case $sel in
        1) return;;
        255) return;;
    esac
    stream_name=$(cat $data | sed -n 1p)
    stream_description=$(cat $data | sed -n 2p)
    stream_genre=$(cat $data | sed -n 3p)
    if [ ${#stream_name} -gt 2 ]; then
        sed -i "s|<name>.*|<name>${stream_name}</name>|g" /etc/ices2/ices-playlist.xml
    fi
    if [ ${#stream_description} -gt 2 ]; then
        sed -i "s|<description>.*|<description>${stream_description}</description>|g" /etc/ices2/ices-playlist.xml
    fi
    if [ ${#stream_genre} -gt 2 ]; then
        sed -i "s|<genre>.*|<genre>${stream_genre}</genre>|g" /etc/ices2/ices-playlist.xml
    fi
    rm $data
    stop_icecast
    start_icecast
}
function icecast_set_maximum_streams {
    data=$(tempfile 2>/dev/null)
    trap "rm -f $data" 0 1 2 5 15
    dialog --title $"Set the maximum clients" \
           --backtitle $"Freedombone Control Panel" \
           --inputbox $"Maximum number of clients" 8 40 "10" 2>$data
    sel=$?
    case $sel in
        0)
            max_clients=$(<$data)
            if [ ${#max_clients} -gt 0 ]; then
                if [[ "$max_clients" != *' '* ]]; then
                    if [[ $max_clients =~ '^[0-9]+$' ]] ; then
                        sed -i "s|<clients>.*|<clients>${max_clients}</clients>|g" /etc/icecast2/icecast.xml
                        stop_icecast
                        start_icecast
                        dialog --title $"Set the maximum clients" \
                               --msgbox $"\nMaximum Icecast clients was set to ${max_clients}" 8 50
                    fi
                fi
            fi
            ;;
    esac
    rm $data
}
function configure_interactive_icecast {
    while true
    do
        data=$(tempfile 2>/dev/null)
        trap "rm -f $data" 0 1 2 5 15
        dialog --backtitle $"Freedombone Control Panel" \
               --title $"Icecast" \
               --radiolist $"Choose an operation:" 19 70 12 \
               1 $"Import stream files from directory" off \
               2 $"Import stream files from USB drive" off \
               3 $"Format a USB drive for stream file storage" off \
               4 $"Export stream files to USB drive" off \
               5 $"Manually edit playlist" off \
               6 $"Enable login for stream visitors" off \
               7 $"Change password for stream visitors" off \
               8 $"Re-scan playlist" off \
               9 $"Restart stream" off \
               10 $"Set Stream Name/Description/Genre" off \
               11 $"Set maximum number of clients/streams" off \
               12 $"Exit" on 2> $data
        sel=$?
        case $sel in
            1) break;;
            255) break;;
        esac
        case $(cat $data) in
            1) icecast_import_from_directory;;
            2) icecast_import_from_usb;;
            3) icecast_format_drive;;
            4) icecast_export_to_usb;;
            5) icecast_edit_playlist;;
            6) icecast_enable_login;;
            7) icecast_change_login;;
            8) clear
               echo $'Rescanning Icecast playlist'
               icecast_rescan;;
            9) clear
               echo $'Restarting Icecast stream'
               stop_icecast
               start_icecast;;
            10) icecast_set_stream_name;;
            11) icecast_set_maximum_streams;;
            12) break;;
        esac
    done
}
function upgrade_icecast {
    icecast_update_daemon
}
function backup_local_icecast {
    if [ ! -d $ICECAST_DIR ]; then
        return
    fi
    stop_icecast
    cp /etc/nginx/.icepasswd $ICECAST_DIR
    cp /etc/ices2/ices-playlist.xml $ICECAST_DIR
    function_check backup_directory_to_usb
    backup_directory_to_usb $ICECAST_DIR icecast
    rm $ICECAST_DIR/.icepasswd
    start_icecast
}
function restore_local_icecast {
    if [ ! -d $ICECAST_DIR ]; then
        return
    fi
    stop_icecast
    temp_restore_dir=/root/tempicecast
    function_check restore_directory_from_usb
    restore_directory_from_usb $temp_restore_dir icecast
    if [ -d $temp_restore_dir$ICECAST_DIR ]; then
        cp -r $temp_restore_dir$ICECAST_DIR $ICECAST_DIR/
    else
        cp -r $temp_restore_dir/* $ICECAST_DIR/*
    fi
    cp $ICECAST_DIR/.icepasswd /etc/nginx/.icepasswd
    rm $ICECAST_DIR/.icepasswd
    cp $ICECAST_DIR/ices-playlist.xml /etc/ices2
    rm $ICECAST_DIR/ices-playlist.xml
    chown -R icecast2:icecast $ICECAST_DIR
    chown -R icecast2:icecast /etc/ices2
    start_icecast
    rm -rf $temp_restore_dir
}
function backup_remote_icecast {
    if [ ! -d $ICECAST_DIR ]; then
        return
    fi
    stop_icecast
    cp /etc/nginx/.icepasswd $ICECAST_DIR
    cp /etc/ices2/ices-playlist.xml $ICECAST_DIR
    function_check backup_directory_to_friend
    backup_directory_to_friend $ICECAST_DIR icecast
    rm $ICECAST_DIR/.icepasswd
    start_icecast
}
function restore_remote_icecast {
    if [ ! -d $ICECAST_DIR ]; then
        return
    fi
    stop_icecast
    temp_restore_dir=/root/tempicecast
    function_check restore_directory_from_friend
    restore_directory_from_friend $temp_restore_dir icecast
    if [ -d $temp_restore_dir$ICECAST_DIR ]; then
        cp -r $temp_restore_dir$ICECAST_DIR $ICECAST_DIR/
    else
        cp -r $temp_restore_dir/* $ICECAST_DIR/*
    fi
    cp $ICECAST_DIR/.icepasswd /etc/nginx/.icepasswd
    rm $ICECAST_DIR/.icepasswd
    cp $ICECAST_DIR/ices-playlist.xml /etc/ices2
    rm $ICECAST_DIR/ices-playlist.xml
    chown -R icecast2:icecast $ICECAST_DIR
    start_icecast
    rm -rf $temp_restore_dir
}
function remove_icecast {
    nginx_dissite icecast
    sed -i '/start_icecast/d' /etc/crontab
    stop_icecast
    systemctl disable icecast2
    rm /etc/systemd/system/icecast2.service
    rm /usr/bin/start_icecast
    rm /usr/bin/stop_icecast
    if [ -f /etc/nginx/sites-available/icecast ]; then
        rm /etc/nginx/sites-available/icecast
    fi
    if [ -d /var/www/icecast ]; then
        rm -rf /var/www/icecast
    fi
    apt-get -yq remove --purge icecast2
    if [ -d /etc/icecast2 ]; then
        rm -rf /etc/icecast2
    fi
    if [ -d /etc/ices2 ]; then
        rm -rf /etc/ices2
    fi
    function_check remove_onion_service
    remove_onion_service icecast ${ICECAST_ONION_PORT}
    sed -i '/icecast/d' $COMPLETION_FILE
}
function install_icecast {
    apt-get -yq install software-properties-common debconf-utils
    apt-get -yq update
    debconf-set-selections <<< "icecast2 icecast2/icecast-setup boolean false"
    apt-get -yq install icecast2
    apt-get -yq install ices2 ffmpeg apache2-utils
    if [ ! -f /etc/icecast2/icecast.xml ]; then
        echo $'Icecast not installed'
        exit 7923528
    fi
    if [ ! ${ICECAST_PASSWORD} ]; then
        if [ -f ${IMAGE_PASSWORD_FILE} ]; then
            ICECAST_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
        else
            ICECAST_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
        fi
    fi
    ICECAST_ONION_HOSTNAME=$(add_onion_service icecast 80 ${ICECAST_ONION_PORT})
    sed -i  -e "s|<source-password>[^<]*</source-password>|<source-password>$ICECAST_PASSWORD</source-password>|" \
        -e "s|<relay-password>[^<]*</relay-password>|<relay-password>$ICECAST_PASSWORD</relay-password>|" \
        -e "s|<admin-password>[^<]*</admin-password>|<admin-password>$ICECAST_PASSWORD</admin-password>|" \
        -e "s|<hostname>[^<]*</hostname>|<hostname>$ICECAST_ONION_HOSTNAME</hostname>|" \
        /etc/icecast2/icecast.xml
    sed -i "s|<clients>.*|<clients>10</clients>|g" /etc/icecast2/icecast.xml
    sed -i "s|<port>.*|<port>$ICECAST_PORT</port>|g" /etc/icecast2/icecast.xml
    sed -i "s|<admin-user>.*|<admin-user>$MY_USERNAME</admin-user>|g" /etc/icecast2/icecast.xml
    sed -i "s|<admin>.*|<admin>$MY_EMAIL_ADDRESS</admin>|g" /etc/icecast2/icecast.xml
    sed -i "s|<location>.*|<location>The Interwebs</location>|g" /etc/icecast2/icecast.xml
    #sed -i 's|<!-- <bind-address>.*|<bind-address>127.0.0.1</bind-address>|g' /etc/icecast2/icecast.xml
    if [ ! -d /var/www/icecast/htdocs ]; then
        mkdir -p /var/www/icecast/htdocs
    fi
    icecast_nginx_site=/etc/nginx/sites-available/icecast
    echo 'server {' > $icecast_nginx_site
    echo "    listen 127.0.0.1:$ICECAST_ONION_PORT default_server;" >> $icecast_nginx_site
    echo "    server_name $ICECAST_ONION_HOSTNAME;" >> $icecast_nginx_site
    echo '' >> $icecast_nginx_site
    echo '  # Logs' >> $icecast_nginx_site
    echo '  access_log /dev/null;' >> $icecast_nginx_site
    echo '  error_log /dev/null;' >> $icecast_nginx_site
    echo '' >> $icecast_nginx_site
    echo '  location / {' >> $icecast_nginx_site
    function_check nginx_limits
    nginx_limits $ICECAST_ONION_HOSTNAME '15m'
    echo "      proxy_pass http://localhost:$ICECAST_PORT;" >> $icecast_nginx_site
    echo "      #auth_basic \"${ICECAST_LOGIN_TEXT}\";" >> $icecast_nginx_site
    echo '      #auth_basic_user_file /etc/nginx/.icepasswd;' >> $icecast_nginx_site
    echo '  }' >> $icecast_nginx_site
    echo '}' >> $icecast_nginx_site
    if [ ! -d /var/log/ices ]; then
        mkdir -p /var/log/ices
    fi
    if [ ! -d /etc/ices2 ]; then
        mkdir -p /etc/ices2
    fi
    echo '<?xml version="1.0"?>' > /etc/ices2/ices-playlist.xml
    echo '<ices>' >> /etc/ices2/ices-playlist.xml
    echo '    <!-- run in background -->' >> /etc/ices2/ices-playlist.xml
    echo '    <background>1</background>' >> /etc/ices2/ices-playlist.xml
    echo '    <!-- where logs, etc go. -->' >> /etc/ices2/ices-playlist.xml
    echo '    <logpath>/var/log/ices</logpath>' >> /etc/ices2/ices-playlist.xml
    echo '    <logfile>ices.log</logfile>' >> /etc/ices2/ices-playlist.xml
    echo '    <!-- 1=error,2=warn,3=info,4=debug -->' >> /etc/ices2/ices-playlist.xml
    echo '    <loglevel>1</loglevel>' >> /etc/ices2/ices-playlist.xml
    echo '    <!-- set this to 1 to log to the console instead of to the file above -->' >> /etc/ices2/ices-playlist.xml
    echo '    <consolelog>0</consolelog>' >> /etc/ices2/ices-playlist.xml
    echo '' >> /etc/ices2/ices-playlist.xml
    echo '    <!-- optional filename to write process id to -->' >> /etc/ices2/ices-playlist.xml
    echo '    <!-- <pidfile>/home/ices/ices.pid</pidfile> -->' >> /etc/ices2/ices-playlist.xml
    echo '' >> /etc/ices2/ices-playlist.xml
    echo '    <stream>' >> /etc/ices2/ices-playlist.xml
    echo '        <!-- metadata used for stream listing (not currently used) -->' >> /etc/ices2/ices-playlist.xml
    echo '        <metadata>' >> /etc/ices2/ices-playlist.xml
    echo '            <name>Example stream name</name>' >> /etc/ices2/ices-playlist.xml
    echo '            <genre>Example genre</genre>' >> /etc/ices2/ices-playlist.xml
    echo '            <description>A short description of your stream</description>' >> /etc/ices2/ices-playlist.xml
    echo '        </metadata>' >> /etc/ices2/ices-playlist.xml
    echo '' >> /etc/ices2/ices-playlist.xml
    echo '        <!-- input module' >> /etc/ices2/ices-playlist.xml
    echo '' >> /etc/ices2/ices-playlist.xml
    echo '            The module used here is the playlist module - it has ' >> /etc/ices2/ices-playlist.xml
    echo '            "submodules" for different types of playlist. There are' >> /etc/ices2/ices-playlist.xml
    echo '            two currently implemented, "basic", which is a simple' >> /etc/ices2/ices-playlist.xml
    echo '            file-based playlist, and "script" which invokes a command' >> /etc/ices2/ices-playlist.xml
    echo '            to returns a filename to start playing. -->' >> /etc/ices2/ices-playlist.xml
    echo '' >> /etc/ices2/ices-playlist.xml
    echo '        <input>' >> /etc/ices2/ices-playlist.xml
    echo '            <module>playlist</module>' >> /etc/ices2/ices-playlist.xml
    echo '            <param name="type">basic</param>' >> /etc/ices2/ices-playlist.xml
    echo "            <param name=\"file\">$ICECAST_PLAYLIST_FILE</param>" >> /etc/ices2/ices-playlist.xml
    echo '            <!-- random play -->' >> /etc/ices2/ices-playlist.xml
    echo '            <param name="random">0</param>' >> /etc/ices2/ices-playlist.xml
    echo '            <!-- if the playlist get updated that start at the beginning -->' >> /etc/ices2/ices-playlist.xml
    echo '            <param name="restart-after-reread">0</param>' >> /etc/ices2/ices-playlist.xml
    echo '            <!-- if set to 1 , plays once through, then exits. -->' >> /etc/ices2/ices-playlist.xml
    echo '            <param name="once">0</param>' >> /etc/ices2/ices-playlist.xml
    echo '        </input>' >> /etc/ices2/ices-playlist.xml
    echo '' >> /etc/ices2/ices-playlist.xml
    echo '            <!-- Stream instance' >> /etc/ices2/ices-playlist.xml
    echo '            You may have one or more instances here. This allows you to ' >> /etc/ices2/ices-playlist.xml
    echo '            send the same input data to one or more servers (or to different' >> /etc/ices2/ices-playlist.xml
    echo '            mountpoints on the same server). Each of them can have different' >> /etc/ices2/ices-playlist.xml
    echo '            parameters. This is primarily useful for a) relaying to multiple' >> /etc/ices2/ices-playlist.xml
    echo '            independent servers, and b) encoding/reencoding to multiple' >> /etc/ices2/ices-playlist.xml
    echo '            bitrates.' >> /etc/ices2/ices-playlist.xml
    echo '            If one instance fails (for example, the associated server goes' >> /etc/ices2/ices-playlist.xml
    echo '            down, etc), the others will continue to function correctly.' >> /etc/ices2/ices-playlist.xml
    echo '            This example defines two instances as two mountpoints on the' >> /etc/ices2/ices-playlist.xml
    echo '            same server.  -->' >> /etc/ices2/ices-playlist.xml
    echo '        <instance>' >> /etc/ices2/ices-playlist.xml
    echo '            <!-- Server details:' >> /etc/ices2/ices-playlist.xml
    echo '                You define hostname and port for the server here, along with' >> /etc/ices2/ices-playlist.xml
    echo '                the source password and mountpoint.  -->' >> /etc/ices2/ices-playlist.xml
    echo '            <hostname>localhost</hostname>' >> /etc/ices2/ices-playlist.xml
    echo "            <port>$ICECAST_PORT</port>" >> /etc/ices2/ices-playlist.xml
    echo "            <password>$ICECAST_PASSWORD</password>" >> /etc/ices2/ices-playlist.xml
    echo '            <mount>/example1.ogg</mount>' >> /etc/ices2/ices-playlist.xml
    echo '            <!-- Reconnect parameters:' >> /etc/ices2/ices-playlist.xml
    echo '                When something goes wrong (e.g. the server crashes, or the' >> /etc/ices2/ices-playlist.xml
    echo '                network drops) and ices disconnects from the server, these' >> /etc/ices2/ices-playlist.xml
    echo '                control how often it tries to reconnect, and how many times' >> /etc/ices2/ices-playlist.xml
    echo '                it tries to reconnect. Delay is in seconds.' >> /etc/ices2/ices-playlist.xml
    echo '                If you set reconnectattempts to -1, it will continue ' >> /etc/ices2/ices-playlist.xml
    echo '                indefinately. Suggest setting reconnectdelay to a large value' >> /etc/ices2/ices-playlist.xml
    echo '                if you do this.' >> /etc/ices2/ices-playlist.xml
    echo '            -->' >> /etc/ices2/ices-playlist.xml
    echo '            <reconnectdelay>2</reconnectdelay>' >> /etc/ices2/ices-playlist.xml
    echo '            <reconnectattempts>5</reconnectattempts> ' >> /etc/ices2/ices-playlist.xml
    echo '' >> /etc/ices2/ices-playlist.xml
    echo '            <!-- maxqueuelength:' >> /etc/ices2/ices-playlist.xml
    echo '                This describes how long the internal data queues may be. This' >> /etc/ices2/ices-playlist.xml
    echo '                basically lets you control how much data gets buffered before' >> /etc/ices2/ices-playlist.xml
    echo '                ices decides it cant send to the server fast enough, and ' >> /etc/ices2/ices-playlist.xml
    echo '                either shuts down or flushes the queue (dropping the data)' >> /etc/ices2/ices-playlist.xml
    echo '                and continues. ' >> /etc/ices2/ices-playlist.xml
    echo '                For advanced users only.' >> /etc/ices2/ices-playlist.xml
    echo '            -->' >> /etc/ices2/ices-playlist.xml
    echo '            <maxqueuelength>80</maxqueuelength>' >> /etc/ices2/ices-playlist.xml
    echo '' >> /etc/ices2/ices-playlist.xml
    echo '            <!-- Live encoding/reencoding:' >> /etc/ices2/ices-playlist.xml
    echo '                Currrently, the parameters given here for encoding MUST' >> /etc/ices2/ices-playlist.xml
    echo '                match the input data for channels and sample rate. That ' >> /etc/ices2/ices-playlist.xml
    echo '                restriction will be relaxed in the future.' >> /etc/ices2/ices-playlist.xml
    echo '                Remove this section if you dont want your files getting reencoded.' >> /etc/ices2/ices-playlist.xml
    echo '            -->' >> /etc/ices2/ices-playlist.xml
    echo '            <encode>  ' >> /etc/ices2/ices-playlist.xml
    echo '                <nominal-bitrate>64000</nominal-bitrate>' >> /etc/ices2/ices-playlist.xml
    echo '                <samplerate>22050</samplerate>' >> /etc/ices2/ices-playlist.xml
    echo '                <channels>1</channels>' >> /etc/ices2/ices-playlist.xml
    echo '            </encode>' >> /etc/ices2/ices-playlist.xml
    echo '        </instance>' >> /etc/ices2/ices-playlist.xml
    echo '' >> /etc/ices2/ices-playlist.xml
    echo '        </stream>' >> /etc/ices2/ices-playlist.xml
    echo '</ices>' >> /etc/ices2/ices-playlist.xml
    sed -i 's|ENABLE=.*|ENABLE=true|g' /etc/default/icecast2
    if [ ! -d $ICECAST_DIR ]; then
        mkdir $ICECAST_DIR
    fi
    chown -R icecast2:icecast $ICECAST_DIR
    # create a password for users
    ICECAST_USER_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
    if grep -q "$MY_USERNAME:" /etc/nginx/.icepasswd; then
        sed -i "/$MY_USERNAME:/d" /etc/nginx/.icepasswd
    fi
    echo "$ICECAST_USER_PASSWORD" | htpasswd -i -s -c /etc/nginx/.icepasswd $MY_USERNAME
    if [ ! -f /etc/nginx/.icepasswd ]; then
        echo $'/etc/nginx/.icepasswd not found'
        exit 73528235
    fi
    ${PROJECT_NAME}-pass -u $MY_USERNAME -a icecast -p "$ICECAST_PASSWORD"
    ${PROJECT_NAME}-pass -u $MY_USERNAME -a icecastuser -p "$ICECAST_USER_PASSWORD"
    groupadd icecast
    useradd -c "Icecast system account" -d /etc/icecast2 -m -r -g icecast icecast2
    icecast_update_daemon
    nginx_ensite icecast
    systemctl restart nginx
    icecast_rescan
    start_icecast
    APP_INSTALLED=1
}
function install_interactive_icecast {
    install_icecast
}
# NOTE: deliberately no exit 0
 |