11#! /bin/bash
22
3+ DIRECTORY_NAME_DATA_CACHE=/data/cache
4+ DIRECTORY_NAME_DATA_SYSTEM_CACHE=/data/system-cache
5+
36current_user_id=$( id -u)
47echo " Current user id is [$current_user_id ]"
58
@@ -22,29 +25,35 @@ CMD_LINE="/usr/bin/librespot"
2225DEFAULT_UID=1000
2326DEFAULT_GID=1000
2427
25- if [[ $current_user_id -eq 0 ]]; then
26- if [[ " ${BACKEND} " == " pulseaudio" && -z " ${PUID} " ]]; then
27- PUID=$DEFAULT_UID ;
28- echo " Setting default value for PUID: [" $PUID " ]"
29- if [[ -z " ${PGID} " ]]; then
30- PGID=$DEFAULT_GID ;
31- echo " Also setting default value for PGID: [" $PGID " ]"
32- fi
33- fi
28+ echo " BACKEND=[${BACKEND} ]"
3429
35- if [[ " ${BACKEND} " == " pulseaudio" && -z " ${PGID} " ]]; then
36- PGID=$DEFAULT_GID ;
37- echo " Setting default value for PGID: [" $PGID " ]"
30+ if [[ $current_user_id -eq 0 ]]; then
31+ if [[ " ${BACKEND} " == " pulseaudio" ]]; then
32+ echo " Backend is [${BACKEND} ], user mode is enforced"
33+ if [[ -z " ${PUID} " ]]; then
34+ PUID=$DEFAULT_UID ;
35+ echo " Setting default value for PUID: [" $PUID " ]"
36+ if [[ -z " ${PGID} " ]]; then
37+ PGID=$DEFAULT_GID ;
38+ echo " Also setting default value for PGID: [" $PGID " ]"
39+ fi
40+ fi
41+ if [[ -z " ${PGID} " ]]; then
42+ PGID=$DEFAULT_GID ;
43+ echo " Setting default value for PGID: [" $PGID " ]"
44+ fi
3845 fi
39-
4046 USER_NAME=librespot-user
4147 GROUP_NAME=librespot-group
42-
4348 HOME_DIR=/home/$USER_NAME
44-
4549 # handle user mode
46- if [[ -n " ${PUID} " && -n " ${PUID} " ]]; then
47- echo " Ensuring user with uid:[$PUID ] gid:[$PGID ] exists ..." ;
50+ if [[ -n " ${PUID} " ]]; then
51+ if [[ -z " ${PGID} " ]]; then
52+ echo " PUID is set to [${PUID} ] but PGID is empty, using PUID"
53+ PGID=${PUID}
54+ fi
55+ echo " User mode with PUID=[${PUID} ] PGID=[${PGID} ] AUDIO_GID=[${AUDIO_GID} ]"
56+ echo " Ensuring user with uid:[${PUID} ] gid:[${PGID} ] exists ..." ;
4857 # ## create group if it does not exist
4958 if [ ! $( getent group $PGID ) ]; then
5059 echo " Group with gid [$PGID ] does not exist, creating..."
@@ -70,37 +79,45 @@ if [[ $current_user_id -eq 0 ]]; then
7079 mkdir -p $HOME_DIR
7180 echo " . done."
7281 fi
73- chown -R $PUID :$PGID $HOME_DIR
74- if [ -z " ${AUDIO_GID} " ]; then
75- echo " WARNING: AUDIO_GID is mandatory for user mode and alsa backend"
82+ echo " Setting ownership of [${HOME_DIR} ] to [$USER_NAME :$GROUP_NAME ] ..."
83+ chown -R $USER_NAME :$GROUP_NAME $HOME_DIR
84+ echo " Done."
85+ if [[ $PUID -ne 0 ]]; then
86+ if [ -z " ${AUDIO_GID} " ]; then
87+ echo " WARNING: AUDIO_GID is mandatory for user mode and alsa backend"
88+ echo " WARNING: Ignore the previous warning if you set PGID to the " " audio" " group"
89+ else
90+ if [ $( getent group $AUDIO_GID ) ]; then
91+ echo " Alsa Mode - Group with gid $AUDIO_GID already exists"
92+ else
93+ echo " Alsa Mode - Creating group with gid $AUDIO_GID "
94+ groupadd -g $AUDIO_GID sq-audio
95+ fi
96+ echo " Alsa Mode - Adding $USER_NAME [$PUID ] to gid [$AUDIO_GID ]"
97+ AUDIO_GRP=$( getent group $AUDIO_GID | cut -d: -f1)
98+ echo " gid $AUDIO_GID -> group $AUDIO_GRP "
99+ usermod -a -G $AUDIO_GRP $USER_NAME
100+ echo " Alsa Mode - Successfully created user $USER_NAME :$GROUP_NAME [$PUID :$PGID ])" ;
101+ fi
76102 else
77- if [ $( getent group $AUDIO_GID ) ]; then
78- echo " Alsa Mode - Group with gid $AUDIO_GID already exists"
79- else
80- echo " Alsa Mode - Creating group with gid $AUDIO_GID "
81- groupadd -g $AUDIO_GID sq-audio
82- fi
83- echo " Alsa Mode - Adding $USER_NAME [$PUID ] to gid [$AUDIO_GID ]"
84- AUDIO_GRP=$( getent group $AUDIO_GID | cut -d: -f1)
85- echo " gid $AUDIO_GID -> group $AUDIO_GRP "
86- usermod -a -G $AUDIO_GRP $USER_NAME
87- echo " Alsa Mode - Successfully created user $USER_NAME :$GROUP_NAME [$PUID :$PGID ])" ;
103+ echo " PUID specifies the root user, not adding to the " " audio" " group"
88104 fi
89105 # set ownership on volumes
90- chown -R $PUID :$PGID /data/cache
91- chown -R $PUID :$PGID /data/system-cache
92- chown -R $PUID :$PGID /user/config
106+ echo " Setting ownership of volumes to [$USER_NAME :$GROUP_NAME ] ..."
107+ chown -R $USER_NAME :$GROUP_NAME $DIRECTORY_NAME_DATA_CACHE
108+ chown -R $USER_NAME :$GROUP_NAME $DIRECTORY_NAME_DATA_SYSTEM_CACHE
109+ echo " Done."
110+ fi
111+ # preparing for pulseaudio
112+ if [[ " ${BACKEND} " == " pulseaudio" ]]; then
113+ PULSE_CLIENT_CONF=" /etc/pulse/client.conf"
114+ echo " cat /app/assets/pulse-client-template.conf"
115+ cat /app/assets/pulse-client-template.conf
116+ echo " Creating pulseaudio configuration file $PULSE_CLIENT_CONF ..."
117+ cp /app/assets/pulse-client-template.conf $PULSE_CLIENT_CONF
118+ sed -i ' s/PUID/' " $PUID " ' /g' $PULSE_CLIENT_CONF
119+ cat $PULSE_CLIENT_CONF
93120 fi
94-
95- PULSE_CLIENT_CONF=" /etc/pulse/client.conf"
96-
97- echo " cat /app/assets/pulse-client-template.conf"
98- cat /app/assets/pulse-client-template.conf
99-
100- echo " Creating pulseaudio configuration file $PULSE_CLIENT_CONF ..."
101- cp /app/assets/pulse-client-template.conf $PULSE_CLIENT_CONF
102- sed -i ' s/PUID/' " $PUID " ' /g' $PULSE_CLIENT_CONF
103- cat $PULSE_CLIENT_CONF
104121fi
105122
106123if [ -n " $SPOTIFY_USERNAME " ]; then
@@ -140,11 +157,25 @@ if [ -n "$FORMAT" ]; then
140157fi
141158
142159if [ " ${ENABLE_CACHE^^} " = " Y" ]; then
143- CMD_LINE=" $CMD_LINE --cache /data/cache"
160+ if [ -w " $DIRECTORY_NAME_DATA_CACHE " ]; then
161+ echo " Directory [$DIRECTORY_NAME_DATA_CACHE ] is writable"
162+ CMD_LINE=" $CMD_LINE --cache $DIRECTORY_NAME_DATA_CACHE "
163+ else
164+ echo " Directory [$DIRECTORY_NAME_DATA_CACHE ] is not writable, creating in /tmp ..."
165+ mkdir -p /tmp/cache
166+ CMD_LINE=" $CMD_LINE --cache /tmp/cache"
167+ fi
144168fi
145169
146170if [ " ${ENABLE_SYSTEM_CACHE^^} " = " Y" ]; then
147- CMD_LINE=" $CMD_LINE --system-cache /data/system-cache"
171+ if [ -w " $DIRECTORY_NAME_DATA_SYSTEM_CACHE " ]; then
172+ echo " Directory [$DIRECTORY_NAME_DATA_SYSTEM_CACHE ] is writable"
173+ CMD_LINE=" $CMD_LINE --system-cache $DIRECTORY_NAME_DATA_SYSTEM_CACHE "
174+ else
175+ echo " Directory [$DIRECTORY_NAME_DATA_SYSTEM_CACHE ] is not writable, creating in /tmp ..."
176+ mkdir -p /tmp/system-cache
177+ CMD_LINE=" $CMD_LINE --system-cache /tmp/system-cache"
178+ fi
148179fi
149180
150181if [ -n " $CACHE_SIZE_LIMIT " ]; then
@@ -267,14 +298,14 @@ if [[ -z "${LOG_COMMAND_LINE}" || "${LOG_COMMAND_LINE^^}" = "Y" ]]; then
267298fi
268299
269300if [[ $current_user_id -eq 0 ]]; then
270- if [[ " ${BACKEND} " = " pulseaudio" || -n " ${PUID} " ]]; then
301+ if [[ " ${BACKEND} " == " pulseaudio" || -n " ${PUID} " ]]; then
271302 echo " Running in user mode ..."
272- su - $USER_NAME -c " $CMD_LINE " ;
303+ su - $USER_NAME -c " $CMD_LINE "
273304 else
274305 echo " Running as root ..."
275306 eval $CMD_LINE ;
276307 fi
277308else
278309 echo " Running as uid: [$current_user_id ] ..."
279- eval $CMD_LINE ;
310+ eval $CMD_LINE
280311fi
0 commit comments