don't use cached sudo password in un/installer
[ardour.git] / tools / linux_packaging / stage2.run
1 #!/bin/sh
2
3 ####################################
4 #
5 #       stage2.run
6 #       Ardour/Mixbus bundle installer
7 #       Todd Naugle
8 #
9 ###################################
10
11
12
13 PGM_NAME="Ardour"
14 PGM_VENDOR="Ardour"
15 PGM_EXEC_FILE="ardour3"
16
17 INSTALL_DEST_BASE="/opt"
18 USER_BIN_DIR="/usr/local/bin"
19
20 PGM_NAME_LOWER=$(echo $PGM_NAME | tr '[:upper:]' '[:lower:]')
21
22 USER_NAME=$(logname)
23
24 #### Global Variables ####
25 HAS_XDG="T"
26
27 ########################
28 # Function Definitions
29 ########################
30
31 VaildateYesNoQuestion ()
32 {
33         # $1 = Question Text
34         
35         local INPUT_OK="n"
36         local USER_INPUT=""
37
38         until test "y" = $INPUT_OK;
39         do
40                 echo ""
41                 read -p "$1 [y/n]: " USER_INPUT
42                 echo ""
43
44                 if [ ! -z $USER_INPUT ]; 
45                 then            
46                         if [ "Y" = $USER_INPUT -o "y" = $USER_INPUT -o "n" = $USER_INPUT -o "N" = $USER_INPUT ];
47                         then
48                                 INPUT_OK="y"
49                         fi
50                 fi
51         done
52         
53         echo $USER_INPUT | tr '[:upper:]' '[:lower:]'
54         
55 }
56
57 SystemInstall ()
58 {
59
60         # Determine which software install app to use and then install requested package
61         # $1 = Package Name
62
63         if which yum > /dev/null;
64         then
65                 ${SUPER} yum -y install $1
66                 rtrn=$?
67
68                 if [ $rtrn -ne 0 ];
69                 then
70                         echo ""
71                         echo "!!! ERROR !!! yum install failed for an unknown reason."
72                         echo "Please install package $1 after this installer completes."
73                         echo ""
74                 fi
75
76         elif which apt-get > /dev/null;
77         then
78                 ${SUPER} apt-get -y install $1
79                 rtrn=$?
80
81                 if [ $rtrn -ne 0 ];
82                 then
83                         echo ""
84                         echo "!!! ERROR !!! apt-get install failed for an unknown reason."
85                         echo "Please install package $1 after this installer completes."
86                         echo ""
87                 fi
88
89         else
90                 echo ""
91                 echo "!!! ERROR !!! - Not able to detect which software install tool to use (yum or apt-get)."
92                 echo "Please install package $1 using the system software install tool."
93                 echo ""
94                 rtrn=1
95         fi
96         
97         return $rtrn
98
99 }
100
101 ########################################################################
102 #                                 Main
103 ########################################################################
104 umask 0022
105
106 # If you double click a script, some systems don't get the PWD correct.
107 # Force it to be correct
108 PKG_PATH=$(dirname "$(readlink -f "$0")")
109 cd "${PKG_PATH}"
110
111 echo ""
112 echo "Welcome to the ${PGM_NAME} installer"
113 echo ""
114 echo "${PGM_NAME} will be installed for user ${USER_NAME} in ${INSTALL_DEST_BASE}"
115 echo ""
116
117 ###############################
118 # Check for install destination
119 ###############################
120
121 if [ ! -d ${INSTALL_DEST_BASE} ];
122 then
123         echo ""
124         echo "!!! ERROR !!! - Installation location ${INSTALL_DEST_BASE} does not exist!"
125         echo "Installation will not complete."
126         echo ""
127         read -p "Press ENTER to exit installer:" BLAH
128         exit 1
129 fi
130
131 #############################
132 # Check for root privileges
133 #############################
134
135 SUPER=""
136 NORM_USER=""
137
138 if [ "$(id -u)" != "0" ]; then
139
140         if ! which sudo > /dev/null;
141         then
142                 echo ""
143                 echo "Sudo installed failed, attempting to install using su"
144                 echo "Please enter root password below"
145                 echo ""
146
147                 if ! su -c "./.stage2.run";
148                 then
149                         echo ""
150                         echo "!!! ERROR !!!"
151                         echo ""
152                         echo "This installer requires root privileges. It is currently not"
153                         echo "running as root AND an attempt to use su failed."
154                         echo ""
155                         echo "Please correct this by installing and configuring sudo or running"
156                         echo "the installer as root (su -c)."
157                         echo ""
158                         read -p "Press ENTER to exit installer:" BLAH
159                         exit 1
160                 fi
161                 exit
162         else
163                 sudo -k # make sudo forget about cached credentials
164         fi
165
166         if ! sudo date;
167         then
168                 echo ""
169                 echo "Attempting to install using su"
170                 echo "Please enter root password below"
171                 echo ""
172
173                 if ! su -c "./.stage2.run";
174                 then
175                         echo ""
176                         echo "!!! ERROR !!!"
177                         echo ""
178                         echo "This installer requires root privileges. It is currently not"
179                         echo "running as root AND an attempt to use both sudo and su failed."
180                         echo ""
181                         echo "Please correct this by installing and configuring sudo or running"
182                         echo "the installer as root (su -c)."
183                         echo ""
184                         read -p "Press ENTER to exit installer:" BLAH
185                         exit 1
186                 fi
187                 exit
188         fi
189         SUPER="sudo"
190         
191         # The quoting reqired for the su sanityCheck method does not work when used without
192         # su. Using sh -c in the normal case gets around that, but is a bit of a hack.
193         NORM_USER="sh -c"
194 else
195         NORM_USER="su -l $USER_NAME -c" 
196 fi
197
198 ############################
199 # Determine processor type
200 ############################
201
202 case `uname -m` in
203         i[3456789]86|x86|i86pc)
204                 echo "Architecture is x86"
205                 ARCH='x86'
206                 ;;
207         x86_64|amd64|AMD64)
208                 echo "Architecture is x86_64"
209                 ARCH='x86_64'
210                 ;;
211         *)
212                 echo ""
213                 echo "!!! ERROR !!! - Unknown architecture `uname -m`"
214                 echo ""
215                 read -p "Press ENTER to exit installer:" BLAH
216                 exit 1
217                 ;;
218 esac
219
220 ####################
221 # Check disk space
222 ####################
223
224 # We have to check the current folder and the INSTALL_DEST_BASE just
225 # in case they are on different devices
226 echo "Checking for required disk space"
227
228 if [ ! -e .${PGM_NAME}_${ARCH}-*.size ]; then
229         echo ""
230         echo "!!! ERROR !!! Can't locate .size file for ${ARCH} bundle."
231         echo "This package is broken or does not support ${ARCH}."
232         echo ""
233         read -p "Press ENTER to exit installer:" BLAH
234         exit 1
235 else
236         REQUIRED_BYTES=$(cat .${PGM_NAME}_${ARCH}-*.size)
237
238         #Installer needs 2x the space since the bundle is unpacked locally and then copied
239         REQUIRED_BYTES=$(($REQUIRED_BYTES + $REQUIRED_BYTES))
240
241         #Check space in current folder
242         FREE_BYTES=$(df -P -B 1 "${PKG_PATH}" | grep / | awk '{print $4}')
243
244         if [ ${FREE_BYTES} -le ${REQUIRED_BYTES} ] ; then
245                 echo ""
246                 echo "!!! ERROR !!! - Insufficient disk space in ${PKG_PATH}"
247                 echo "Install requires ${REQUIRED_BYTES} bytes and"
248                 echo "there is only ${FREE_BYTES} bytes of free space"
249                 echo ""
250                 read -p "Press ENTER to exit installer:" BLAH
251                 exit 1
252         fi
253
254         #Check space in INSTALL_DEST_BASE
255         FREE_BYTES=$(df -P -B 1 ${INSTALL_DEST_BASE} | grep / | awk '{print $4}')
256
257         if [ ${FREE_BYTES} -le ${REQUIRED_BYTES} ] ; then
258                 echo ""
259                 echo "!!! ERROR !!! - Insufficient disk space in ${INSTALL_DEST_BASE}"
260                 echo "Install requires ${REQUIRED_BYTES} bytes and"
261                 echo "there is only ${FREE_BYTES} bytes of free space"
262                 echo ""
263                 read -p "Press ENTER to exit installer:" BLAH
264                 exit 1
265         fi
266 fi
267
268 FILESYSTEM_TYPE=$(df -P -T "${PKG_PATH}" | grep / | awk '{print $2}')
269 echo "Bundle is on ${FILESYSTEM_TYPE} filesystem"
270
271 #####################
272 # Unpack the bundle
273 #####################
274
275 # untar the correct bundle for us to install
276 echo "Unpacking bundle for $ARCH"
277
278 if [ ! -e ${PGM_NAME}_${ARCH}-*.tar.bz2 ]; then
279         echo ""
280         echo "!!! ERROR !!! Can't locate ${ARCH} bundle file."
281         echo ""
282         read -p "Press ENTER to exit installer:" BLAH
283         exit 1
284 fi
285
286 if ! tar -xjf ${PGM_NAME}_${ARCH}-*.tar.bz2; then
287         echo ""
288         echo "!!! ERROR !!! Can't unpack ${ARCH} bundle file."
289         echo ""
290         read -p "Press ENTER to exit installer:" BLAH
291         exit 1
292 else
293         echo "Bundle unpacked"
294 fi
295
296 BUNDLE_DIR=$(basename `find -maxdepth 1 -type d -name "${PGM_NAME}_${ARCH}-*"`)
297
298
299 #######################
300 # Check for xdg utils
301 #######################
302
303 #load the file that contains the translated names of the users directories
304 if [ -e /home/${USER_NAME}/.config/user-dirs.dirs ]; then
305         . /home/${USER_NAME}/.config/user-dirs.dirs
306 fi
307
308 if [ "$(id -u)" != "0" ]; then
309         USER_DESKTOP_DIR=${XDG_DESKTOP_DIR:-$HOME/Desktop}
310 else
311         #running as root with su makes this more difficult
312         DESKTOP_FOLDER=$(echo ${XDG_DESKTOP_DIR:-$HOME/Desktop} | awk -F/ '{print $NF}')
313         USER_DESKTOP_DIR="/home/${USER_NAME}/${DESKTOP_FOLDER}"
314 fi
315
316 XDG_MENU_VER=$(xdg-desktop-menu --version 2> /dev/null)
317 if [ -z "$XDG_MENU_VER" ];
318 then
319         echo "System does not have xdg-desktop-menu installed"
320         HAS_XDG="F"
321 fi
322
323 XDG_ICON_VER=$(xdg-icon-resource --version 2> /dev/null)
324 if [ -z "$XDG_ICON_VER" ];
325 then
326         echo "System does not have xdg-icon-resource installed"
327         HAS_XDG="F"
328 fi
329
330 #################################################
331 # Check if system libs are OK (libc, etc)
332 #################################################
333
334 echo ""
335 echo "Checking system libs to see if they are compatible with ${PGM_NAME}."
336 echo ""
337
338 LIB_ERROR="F"
339 LD_PATH=`pwd`/${BUNDLE_DIR}/lib
340
341 # check the main App
342 LDD_RESULT=$(LD_LIBRARY_PATH=${LD_PATH} ldd ${BUNDLE_DIR}/bin/${PGM_NAME_LOWER}-* 2>&1 > /dev/null)
343
344 if [ -n "$LDD_RESULT" ];
345 then
346         echo "$LDD_RESULT"
347         LIB_ERROR="T"
348 fi
349         
350 # check the libs
351 LIB_FILES=$(find ${BUNDLE_DIR}/lib -name "*.so")
352
353 for path in $LIB_FILES
354 do
355         LDD_RESULT=$(LD_LIBRARY_PATH=${LD_PATH} ldd $path 2>&1 > /dev/null)
356         if [ -n "$LDD_RESULT" ];
357         then
358                 echo "$LDD_RESULT"
359                 LIB_ERROR="T"
360         fi
361 done
362
363 if test "T" = $LIB_ERROR;
364 then
365         echo ""
366         echo "!!! ERROR !!! - Missing library detected!"
367         echo "This system does not have the correct libs to run ${PGM_NAME}."
368         echo "Installation will not complete. Please use a compatible distro."
369         echo ""
370         read -p "Press ENTER to exit installer:" BLAH
371         exit 1
372 fi
373
374 ################################
375 # Setup derived variables
376 ################################
377 PGM_VERSION=$(echo ${BUNDLE_DIR} | cut -d- -f2)
378 PGM_BUILDTYPE=$(echo ${BUNDLE_DIR} | cut -d- -f3)
379
380 if [ -z ${PGM_BUILDTYPE} ];
381 then
382         PGM_FULL_NAME="${PGM_NAME}-${PGM_VERSION}"
383         ICON_NAME="${PGM_VENDOR}-${PGM_NAME}_${PGM_VERSION}"                    #no dash between name and version since dash seperates vendor from program
384         MENU_FILE="${PGM_VENDOR}-${PGM_NAME}_${PGM_VERSION}.desktop"    #no dash between name and version since dash seperates vendor from program
385         DESKTOP_LINK_FILE="${PGM_NAME}_${PGM_VERSION}.desktop"
386 else
387         PGM_FULL_NAME="${PGM_NAME}-${PGM_VERSION}-${PGM_BUILDTYPE}"
388         ICON_NAME="${PGM_VENDOR}-${PGM_NAME}_${PGM_VERSION}_${PGM_BUILDTYPE}"                   #no dash between name and version since dash seperates vendor from program
389         MENU_FILE="${PGM_VENDOR}-${PGM_NAME}_${PGM_VERSION}_${PGM_BUILDTYPE}.desktop"   #no dash between name and version since dash seperates vendor from program
390         DESKTOP_LINK_FILE="${PGM_NAME}_${PGM_VERSION}_${PGM_BUILDTYPE}.desktop"
391 fi
392
393 PGM_EXEC_PATH="${INSTALL_DEST_BASE}/${PGM_FULL_NAME}/bin/${PGM_EXEC_FILE}"
394 ICON_PATH="${INSTALL_DEST_BASE}/${PGM_FULL_NAME}/share/icons"
395 MENU_FILE_PATH="${INSTALL_DEST_BASE}/${PGM_FULL_NAME}/share"
396
397 ################################
398 # Install bundle and Menu/Link
399 ################################
400
401 # uninstall any older versions
402 UNINSTALLERS=$(find ${INSTALL_DEST_BASE} -maxdepth 1 -type f -name "${PGM_NAME}*.uninstall.sh")
403 if [ ! -z "$UNINSTALLERS" ];
404 then
405         for i in $UNINSTALLERS; do
406                 echo ""
407                 echo "Found existing ${PGM_NAME} installation."
408                 
409                 ANSWER=$(VaildateYesNoQuestion "Do you want to run the uninstaller ${i} ?")
410
411                 if test "y" = $ANSWER;
412                 then
413                         echo ""
414                         echo "Running uninstaller $i"
415                         
416                         ${i}
417                         ${SUPER} rm -f ${i}
418                 fi
419         done
420 fi
421
422
423 # install 
424
425 echo ""
426 echo "Installing ${PGM_NAME} ${PGM_VERSION} in ${INSTALL_DEST_BASE}"
427 echo ""
428
429 # Copy the new version in the install directory
430 ${SUPER} mkdir ${INSTALL_DEST_BASE}/${PGM_FULL_NAME} 
431 ${SUPER} cp -Rf ${BUNDLE_DIR}/* ${INSTALL_DEST_BASE}/${PGM_FULL_NAME}/
432
433 # write the desktop/menu file
434 echo "[Desktop Entry]" > /tmp/${MENU_FILE}
435 echo "Encoding=UTF-8" >> /tmp/${MENU_FILE}
436 echo "Version=1.0" >> /tmp/${MENU_FILE}
437 echo "Type=Application" >> /tmp/${MENU_FILE}
438 echo "Terminal=false" >> /tmp/${MENU_FILE}
439 echo "Exec=${PGM_EXEC_PATH}" >> /tmp/${MENU_FILE}
440 if [ -z ${PGM_BUILDTYPE} ];
441 then
442         echo "Name=${PGM_NAME}-${PGM_VERSION}" >> /tmp/${MENU_FILE}
443 else
444         echo "Name=${PGM_NAME}-${PGM_VERSION}-${PGM_BUILDTYPE}" >> /tmp/${MENU_FILE}
445 fi
446 echo "Icon=${ICON_NAME}" >> /tmp/${MENU_FILE}
447 echo "Comment=Digital Audio Workstation" >> /tmp/${MENU_FILE}
448 echo "Categories=AudioVideo;AudioEditing;Audio;Recorder;" >> /tmp/${MENU_FILE}
449
450 chmod ugo+rx /tmp/${MENU_FILE}
451 ${SUPER} mv /tmp/${MENU_FILE} ${MENU_FILE_PATH}/.
452
453 # install the Menu, Link, and Icon(s)
454 if [ "T" = ${HAS_XDG} ];
455 then
456         echo "Adding ${PGM_NAME} to the applications menu"
457         ${SUPER} xdg-icon-resource install --context apps --size 16 ${ICON_PATH}/${PGM_NAME_LOWER}_icon_16px.png ${ICON_NAME}
458         ${SUPER} xdg-icon-resource install --context apps --size 22 ${ICON_PATH}/${PGM_NAME_LOWER}_icon_22px.png ${ICON_NAME}
459         ${SUPER} xdg-icon-resource install --context apps --size 32 ${ICON_PATH}/${PGM_NAME_LOWER}_icon_32px.png ${ICON_NAME}
460         ${SUPER} xdg-icon-resource install --context apps --size 48 ${ICON_PATH}/${PGM_NAME_LOWER}_icon_48px.png ${ICON_NAME}
461         ${SUPER} xdg-icon-resource install --context apps --size 256 ${ICON_PATH}/${PGM_NAME_LOWER}_icon_256px.png ${ICON_NAME}
462
463         if [ -e ${ICON_PATH}/${PGM_NAME_LOWER}_icon.svg -a -d /usr/share/icons/hicolor/scalable/apps ]; 
464         then
465                 ${SUPER} cp -f ${ICON_PATH}/${PGM_NAME_LOWER}_icon.svg  /usr/share/icons/hicolor/scalable/apps/${ICON_NAME}.svg
466         fi
467
468         ${SUPER} xdg-desktop-menu install ${MENU_FILE_PATH}/${MENU_FILE}
469         ${SUPER} xdg-desktop-menu forceupdate --mode system                     # Some systems need an extra kick
470         
471         echo ""
472         echo "Creating a desktop link for ${PGM_NAME} in ${USER_DESKTOP_DIR}"
473         cp ${MENU_FILE_PATH}/${MENU_FILE} ${USER_DESKTOP_DIR}/${DESKTOP_LINK_FILE}
474         chmod ugo+rwx ${USER_DESKTOP_DIR}/${DESKTOP_LINK_FILE}
475 else
476         echo ""
477         echo "Creating a desktop link for ${PGM_NAME} in ${USER_DESKTOP_DIR}"
478         cp ${MENU_FILE_PATH}/${MENU_FILE} ${USER_DESKTOP_DIR}/${DESKTOP_LINK_FILE}
479         chmod ugo+rwx ${USER_DESKTOP_DIR}/${DESKTOP_LINK_FILE}
480 fi
481
482 echo ""
483 echo "Copying uninstall script to ${INSTALL_DEST_BASE}"
484 echo ""
485
486 ${SUPER} cp -f ${BUNDLE_DIR}/bin/*.uninstall.sh ${INSTALL_DEST_BASE}/.
487
488 # Create link to the program in user bin
489
490 echo ""
491 echo "Creating link ${PGM_NAME}3 in ${USER_BIN_DIR}"
492 echo ""
493
494 if [ -d "${USER_BIN_DIR}" ]; then
495         if [ -e "${USER_BIN_DIR}/${PGM_NAME}3" ]; then
496                 ${SUPER} rm -f ${USER_BIN_DIR}/${PGM_NAME}3
497         fi
498
499         cd "${USER_BIN_DIR}"
500         ${SUPER} ln -sf ${PGM_EXEC_PATH} ${PGM_NAME}3 
501         cd "${PKG_PATH}"
502         
503 else
504         echo "Can not create link because ${USER_BIN_DIR} does not exist"
505 fi
506
507 ###########################
508 # Check Jack and qjackctl
509 ###########################
510
511 echo ""
512 echo "Checking to see if Jack is installed"
513 echo ""
514
515 JACK_INSTALLED="f"
516
517 if ! which jackd > /dev/null;
518 then
519         echo ""
520         echo "The program Jack is missing from this system. Jack is a required component of $PGM_NAME."
521         echo ""
522
523         ANSWER=$(VaildateYesNoQuestion "Install jack using system software repository?")
524
525         if test "y" = $ANSWER;
526         then
527                 echo "Attempting to install Jack"
528                 SystemInstall "jackd"
529                 
530                 if [ $? -ne 0 ];
531                 then
532                         echo ""
533                         read -p "Press ENTER to continue:" BLAH
534                 else
535                         JACK_INSTALLED="t"
536                 fi
537         fi
538 else
539         JACK_INSTALLED="t"
540         echo "Jack already present"
541 fi
542
543 # Check to see if Jack is new enough to operate correctly.
544
545 if [ "t" = ${JACK_INSTALLED} ];
546 then
547         JACK_VERSION_OK="f"
548         JACK_VER_STRING=$(jackd --version | grep -e "jackd.*protocol")
549         JACK_NAME=$(echo $JACK_VER_STRING | awk '{print $1}')
550         JACK_VERSION=$(echo $JACK_VER_STRING | awk '{print $3}')
551         JACK_VERSION_MAJOR=$(echo $JACK_VERSION | awk 'BEGIN{FS="."}{print $1}')
552         JACK_VERSION_MIDDLE=$(echo $JACK_VERSION | awk 'BEGIN{FS="."}{print $2}')
553         JACK_VERSION_MINOR=$(echo $JACK_VERSION | awk 'BEGIN{FS="."}{print $3}')
554
555         if [ "jackd" = ${JACK_NAME} ];
556         then
557                 if [ ${JACK_VERSION_MAJOR} -eq 0 ];
558                 then
559                         if [ ${JACK_VERSION_MIDDLE} -eq 121 ];
560                         then
561                                 if [ ${JACK_VERSION_MINOR} -ge 3 ];
562                                 then
563                                         JACK_VERSION_OK="t"
564                                 fi
565                         elif [ ${JACK_VERSION_MIDDLE} -gt 121 ];
566                         then
567                                 JACK_VERSION_OK="t"
568                         fi
569                 elif [ ${JACK_VERSION_MAJOR} -gt 0 ];
570                 then
571                         JACK_VERSION_OK="t"
572                 fi
573         elif [ "jackdmp" = ${JACK_NAME} ];
574         then
575                 if [ ${JACK_VERSION_MAJOR} -eq 1 ];
576                 then
577                         if [ ${JACK_VERSION_MIDDLE} -eq 9 ];
578                         then
579                                 if [ ${JACK_VERSION_MINOR} -ge 8 ];
580                                 then
581                                         JACK_VERSION_OK="t"
582                                 fi
583                         elif [ ${JACK_VERSION_MIDDLE} -gt 9 ];
584                         then
585                                 JACK_VERSION_OK="t"
586                         fi
587                 elif [ ${JACK_VERSION_MAJOR} -gt 1 ];
588                 then
589                         JACK_VERSION_OK="t"
590                 fi
591         fi
592
593         if [ "t" = ${JACK_VERSION_OK} ];
594         then
595                 echo ""
596                 echo "Jack Version Check OK (${JACK_VER_STRING})"
597                 echo ""
598         else
599                 echo ""
600                 echo "!!! WARNING !!! - The version of Jack on this system is too old!"
601                 echo "Using an old version of Jack is not recommended. Please update"
602                 echo "Jack for best results."
603                 echo ""
604                 echo "System Jack version:"
605                 echo "   ${JACK_VER_STRING}"
606                 echo ""
607                 echo "Recommended minimum versions:"
608                 echo "   Jack1 - 0.121.3"
609                 echo "   Jack2 - 1.9.8"
610                 echo ""
611                 read -p "Press ENTER to continue:" BLAH
612         fi
613 fi
614
615 if ! which qjackctl > /dev/null;
616 then
617         echo ""
618         echo "The program QjackCtl is missing from this system."
619         echo "QjackCtl is an extremely useful tool for any system that runs JACK applications like $PGM_NAME."
620         echo "We recommend that you install it."
621         echo ""
622
623         ANSWER=$(VaildateYesNoQuestion "Install QjackCtl using system software repository?")
624
625         if test "y" = $ANSWER;
626         then
627                 echo "Attempting to install QjackCtl"
628                 SystemInstall "qjackctl"
629
630                 if [ $? -ne 0 ];
631                 then
632                         echo ""
633                         read -p "Press ENTER to continue:" BLAH
634                 fi
635         fi
636 fi
637
638
639 ########################
640 # Run Sanity Check
641 ########################
642
643 USER_GROUP_ADJUSTED="f"
644
645 if ! ${NORM_USER} "${INSTALL_DEST_BASE}/${PGM_FULL_NAME}/bin/sanityCheck -a > /dev/null";
646 then
647         echo ""
648         echo "System failed the quick sanity check... Looking for the cause"
649
650         if ! ${NORM_USER} "${INSTALL_DEST_BASE}/${PGM_FULL_NAME}/bin/sanityCheck -rt > /dev/null";
651         then
652                 echo ""
653                 echo "System does not allow realtime for the current user... Looking for a solution"            
654                 
655                 if ${NORM_USER} "${INSTALL_DEST_BASE}/${PGM_FULL_NAME}/bin/sanityCheck -hasaudiogroup > /dev/null";
656                 then
657                         if ${NORM_USER} "${INSTALL_DEST_BASE}/${PGM_FULL_NAME}/bin/sanityCheck -memberaudiogroup > /dev/null 2>&1";
658                         then
659                                 ## This is an odd case. We have an audio group and are a member.
660                                 echo ""
661                                 echo "!!! WARNING !!! - The current user can not execute realtime processes."
662                                 echo "This will adversely affect audio latency."
663                                 echo "This system has an audio group and the user is a member. If jack was"
664                                 echo "just installed, a simple log out/in may fix this."
665                                 echo ""
666                                 echo "For best results, please correct this on your system."
667                                 echo "(Hint: check /etc/security/limits.conf or /etc/security/limits.d/)" 
668                                 echo ""
669                                 read -p "Press ENTER to continue:" BLAH
670                         else
671                                 # Not a member of an audio group. Try to fix it.
672                                 
673                                 if ${NORM_USER} "${INSTALL_DEST_BASE}/${PGM_FULL_NAME}/bin/sanityCheck -hasgroup audio > /dev/null && find /etc/security -type f -name "*.conf" | xargs grep -q "^@audio.*rtprio" ";
674                                 then
675                                         # add user to audio group
676                                         echo ""
677                                         echo "Adding user ${USER_NAME} to the audio group."
678                                         echo "This should allow you to run realtime tasks. Please re-login for this change to take affect."
679                                         echo ""
680                                         read -p "Press ENTER to continue:" BLAH
681
682                                         if ${SUPER} usermod -a -G audio ${USER_NAME};
683                                         then
684                                                 USER_GROUP_ADJUSTED="t"
685                                         else
686                                                 echo ""
687                                                 echo "!!! ERROR !!! - Not able to add user to the audio group (usermod failed)!"
688                                                 echo ""
689                                                 echo "Please add yourself to the audio group and re-login"
690                                                 echo ""
691                                                 read -p "Press ENTER to continue:" BLAH
692                                         fi
693
694                                 elif ${NORM_USER} "${INSTALL_DEST_BASE}/${PGM_FULL_NAME}/bin/sanityCheck -hasgroup jackuser > /dev/null && find /etc/security -type f -name "*.conf" | xargs grep -q "^@jackuser.*rtprio" ";
695                                 then
696                                         # add user to jackuser group
697                                         echo ""
698                                         echo "Adding user ${USER_NAME} to the jackuser group."
699                                         echo "This should allow you to run realtime tasks. Please re-login for this change to take affect."
700                                         echo ""
701                                         read -p "Press ENTER to continue:" BLAH
702
703                                         if ${SUPER} usermod -a -G jackuser ${USER_NAME};
704                                         then
705                                                 USER_GROUP_ADJUSTED="t"
706                                         else
707                                                 echo ""
708                                                 echo "!!! ERROR !!! - Not able to add user to the jackuser group."
709                                                 echo ""
710                                                 echo "Please add yourself to the audio group and re-login"
711                                                 echo ""
712                                                 read -p "Press ENTER to continue:" BLAH
713                                         fi
714                                         
715
716                                 fi
717                         fi
718                 else
719                         # No audio group found on this system!
720                         echo ""
721                         echo "!!! WARNING !!! - The system does not seem to have an audio group (audio or jackuser)."
722                         echo ""
723                         echo "We will not attempt to fix this. Please configure your system to allow"
724                         echo "non-root users to execute realtime tasks."
725                         echo ""
726                         read -p "Press ENTER to continue:" BLAH
727                 fi
728         fi
729
730         if ! ${NORM_USER} "${INSTALL_DEST_BASE}/${PGM_FULL_NAME}/bin/sanityCheck -freqscaling > /dev/null";
731         then
732                 echo ""
733                 echo "!!! WARNING !!! - Your system seems to use frequency scaling."
734                 echo "This can have a serious impact on audio latency."
735                 echo "For best results turn it off, e.g. by chosing the 'performance' governor."
736                 echo ""
737                 read -p "Press ENTER to continue:" BLAH
738         fi
739
740         if [ "f" = $USER_GROUP_ADJUSTED ];
741         then
742                 if ! ${NORM_USER} "${INSTALL_DEST_BASE}/${PGM_FULL_NAME}/bin/sanityCheck -memlock > /dev/null";
743                 then
744                         echo ""
745                         echo "!!! WARNING !!! - You are not allowed to lock memory."
746                         echo ""
747                         echo "We will not attempt to fix this. Please configure your system to allow"
748                         echo "non-root users to execute lock memory."
749                         echo ""
750                         read -p "Press ENTER to continue:" BLAH
751                 fi
752         fi
753 fi
754
755
756 ########################
757 # Install Complete
758 ########################
759
760 echo ""
761 echo "Cleaning up"
762 rm -rf ${BUNDLE_DIR}/
763
764 echo ""
765 echo "!!! Install Complete !!!"
766
767 if [ "t" = $USER_GROUP_ADJUSTED ];
768 then
769         echo "You will need to logout and then login again for all changes to be complete"
770 fi
771
772 echo ""
773 read -p "Press ENTER to exit installer:" BLAH
774
775