9dbd17f7b70f867999f0375b9a5d459a6368561d
[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
19 PGM_NAME_LOWER=$(echo $PGM_NAME | tr '[:upper:]' '[:lower:]')
20
21 #### Global Variables ####
22 HAS_XDG="T"
23
24 ########################
25 # Function Definitions
26 ########################
27
28 VaildateYesNoQuestion ()
29 {
30         # $1 = Question Text
31         
32         local INPUT_OK="n"
33         local USER_INPUT=""
34
35         until test "y" = $INPUT_OK;
36         do
37                 echo ""
38                 read -p "$1 [y/n]: " USER_INPUT
39                 echo ""
40
41                 if [ ! -z $USER_INPUT ]; 
42                 then            
43                         if [ "Y" = $USER_INPUT -o "y" = $USER_INPUT -o "n" = $USER_INPUT -o "N" = $USER_INPUT ];
44                         then
45                                 INPUT_OK="y"
46                         fi
47                 fi
48         done
49         
50         echo $USER_INPUT | tr '[:upper:]' '[:lower:]'
51         
52 }
53
54 SystemInstall ()
55 {
56
57         # Determine which software install app to use and then install requested package
58         # $1 = Package Name
59
60         if which yum > /dev/null;
61         then
62                 sudo yum -y install $1
63                 rtrn=$?
64
65                 if [ $rtrn -ne 0 ];
66                 then
67                         echo ""
68                         echo "!!! ERROR !!! yum install failed for an unknown reason."
69                         echo "Please install package $1 after this installer completes."
70                         echo ""
71                 fi
72
73         elif which apt-get > /dev/null;
74         then
75                 sudo apt-get -y install $1
76                 rtrn=$?
77
78                 if [ $rtrn -ne 0 ];
79                 then
80                         echo ""
81                         echo "!!! ERROR !!! apt-get install failed for an unknown reason."
82                         echo "Please install package $1 after this installer completes."
83                         echo ""
84                 fi
85
86         else
87                 echo ""
88                 echo "!!! ERROR !!! - Not able to detect which software install tool to use (yum or apt-get)."
89                 echo "Please install package $1 using the system software install tool."
90                 echo ""
91                 rtrn=1
92         fi
93         
94         return $rtrn
95
96 }
97
98 ########################################################################
99 #                                 Main
100 ########################################################################
101
102 echo ""
103 echo "Welcome to the ${PGM_NAME} installer"
104 echo ""
105
106 ##############
107 # Check sudo
108 ##############
109
110 if ! sudo date;
111 then
112         echo ""
113         echo "!!! ERROR !!!"
114         echo ""
115         echo "Either you don't know the root password or the user is not allowed to sudo"
116         echo "Please correct this and run the installer again (hint: use visudo to edit sudoers file)"
117         echo ""
118         read -p "Press ENTER to exit installer:" BLAH
119         exit 1
120 fi
121
122 ############################
123 # Determine processor type
124 ############################
125
126 case `uname -m` in
127         i[3456789]86|x86|i86pc)
128                 echo "Architecture is x86"
129                 ARCH='x86'
130                 ;;
131         x86_64|amd64|AMD64)
132                 echo "Architecture is x86_64"
133                 ARCH='x86_64'
134                 ;;
135         *)
136                 echo ""
137                 echo "!!! ERROR !!! - Unknown architecture `uname -m`"
138                 echo ""
139                 read -p "Press ENTER to exit installer:" BLAH
140                 exit 1
141                 ;;
142 esac
143
144 # untar the correct bundle for us to install
145 echo "Unpacking bundle for $ARCH"
146 tar -xjf ${PGM_NAME}_${ARCH}-*.tar.bz2
147 BUNDLE_DIR=$(basename `find -maxdepth 1 -type d -name "${PGM_NAME}_${ARCH}-*"`)
148
149
150 #######################
151 # Check for xdg utils
152 #######################
153
154 XDG_MENU_VER=$(xdg-desktop-menu --version 2> /dev/null)
155 if [ -z "$XDG_MENU_VER" ];
156 then
157         echo "System does not have xdg-desktop-menu installed"
158         HAS_XDG="F"
159 fi
160
161 XDG_ICON_VER=$(xdg-icon-resource --version 2> /dev/null)
162 if [ -z "$XDG_ICON_VER" ];
163 then
164         echo "System does not have xdg-icon-resource installed"
165         HAS_XDG="F"
166 fi
167
168 #################################################
169 # Check if system libs are OK (libc, etc)
170 #################################################
171
172 echo ""
173 echo "Checking system libs to see if they are compatible with ${PGM_NAME}."
174 echo ""
175
176 LIB_ERROR="F"
177 LD_PATH=`pwd`/${BUNDLE_DIR}/lib
178
179 # check the main App
180 LDD_RESULT=$(LD_LIBRARY_PATH=${LD_PATH} ldd ${BUNDLE_DIR}/bin/${PGM_NAME_LOWER}-* 2>&1 > /dev/null)
181
182 if [ -n "$LDD_RESULT" ];
183 then
184         echo "$LDD_RESULT"
185         LIB_ERROR="T"
186 fi
187         
188 # check the libs
189 LIB_FILES=$(find ${BUNDLE_DIR}/lib -name "*.so")
190
191 for path in $LIB_FILES
192 do
193         LDD_RESULT=$(LD_LIBRARY_PATH=${LD_PATH} ldd $path 2>&1 > /dev/null)
194         if [ -n "$LDD_RESULT" ];
195         then
196                 echo "$LDD_RESULT"
197                 LIB_ERROR="T"
198         fi
199 done
200
201 if test "T" = $LIB_ERROR;
202 then
203         echo ""
204         echo "!!! ERROR !!! - Missing library detected!"
205         echo "This system does not have the correct libs to run ${PGM_NAME}."
206         echo "Installation will not complete. Please use a compatible distro."
207         echo ""
208         read -p "Press ENTER to exit installer:" BLAH
209         exit 1
210 fi
211
212 ################################
213 # Setup derived variables
214 ################################
215 PGM_VERSION=$(echo ${BUNDLE_DIR} | awk 'BEGIN { FS = "-" } ; { print $2 }' | awk 'BEGIN { FS = "_"} ; { print $1}')
216 PGM_BUILD=$(echo ${BUNDLE_DIR} | awk 'BEGIN { FS = "-" } ; { print $2 }' | awk 'BEGIN { FS = "_"} ; { print $2}')
217 PGM_FULL_NAME="${PGM_NAME}-${PGM_VERSION}_${PGM_BUILD}"
218
219 ICON_NAME="${PGM_VENDOR}-${PGM_NAME}_${PGM_VERSION}"                    #no dash since it seperates vendor from program
220 MENU_FILE="${PGM_VENDOR}-${PGM_NAME}_${PGM_VERSION}.desktop"    #no dash since it seperates vendor from program
221 DESKTOP_LINK_FILE="${PGM_NAME}_${PGM_VERSION}.desktop"
222
223 PGM_EXEC_PATH="${INSTALL_DEST_BASE}/${PGM_FULL_NAME}/bin/${PGM_EXEC_FILE}"
224 ICON_PATH="${INSTALL_DEST_BASE}/${PGM_FULL_NAME}/etc/icons"
225 MENU_FILE_PATH="${INSTALL_DEST_BASE}/${PGM_FULL_NAME}/share"
226
227         
228
229 ################################
230 # Install bundle and Menu/Link
231 ################################
232
233 if [ ! -d ${INSTALL_DEST_BASE} ];
234 then
235         echo ""
236         echo "!!! ERROR !!! - Installation location ${INSTALL_DEST_BASE} does not exist!"
237         echo "Installation will not complete."
238         echo ""
239         read -p "Press ENTER to exit installer:" BLAH
240         exit 1
241 fi
242
243 # uninstall any older versions
244 UNINSTALLERS=$(find ${INSTALL_DEST_BASE} -maxdepth 1 -type f -name "${PGM_NAME}*.uninstall.sh")
245 if [ ! -z "$UNINSTALLERS" ];
246 then
247         for i in $UNINSTALLERS; do
248                 echo ""
249                 echo "Found existing ${PGM_NAME} installation."
250                 
251                 ANSWER=$(VaildateYesNoQuestion "Do you want to run the uninstaller ${i} ?")
252
253                 if test "y" = $ANSWER;
254                 then
255                         echo ""
256                         echo "Running uninstaller $i"
257                         
258                         ${i}
259                 fi
260         done
261 fi
262
263
264 # install 
265
266 echo ""
267 echo "Installing ${PGM_NAME} ${PGM_VERSION} built from ${PGM_BUILD} in ${INSTALL_DEST_BASE}"
268 echo ""
269
270 # Copy the new version in the install directory
271 sudo mkdir ${INSTALL_DEST_BASE}/${PGM_FULL_NAME} 
272 sudo cp -Rf     ${BUNDLE_DIR}/* ${INSTALL_DEST_BASE}/${PGM_FULL_NAME}/
273
274 # write the desktop/menu file
275 echo "[Desktop Entry]" > /tmp/${MENU_FILE}
276 echo "Encoding=UTF-8" >> /tmp/${MENU_FILE}
277 echo "Version=1.0" >> /tmp/${MENU_FILE}
278 echo "Type=Application" >> /tmp/${MENU_FILE}
279 echo "Terminal=false" >> /tmp/${MENU_FILE}
280 echo "Exec=${PGM_EXEC_PATH}" >> /tmp/${MENU_FILE}
281 echo "Name=${PGM_NAME}-${PGM_VERSION}" >> /tmp/${MENU_FILE}
282 echo "Icon=${ICON_NAME}" >> /tmp/${MENU_FILE}
283 echo "Comment=Digital Audio Workstation" >> /tmp/${MENU_FILE}
284 echo "Categories=AudioVideo;Audio;Recorder;" >> /tmp/${MENU_FILE}
285
286 chmod ugo+rx /tmp/${MENU_FILE}
287 sudo mv /tmp/${MENU_FILE} ${MENU_FILE_PATH}/.
288
289 # install the Menu, Link, and Icon(s)
290 if [ "T" = ${HAS_XDG} ];
291 then
292         echo "Adding ${PGM_NAME} to the applications menu"
293         sudo xdg-icon-resource install --context apps --size 16 ${ICON_PATH}/${PGM_NAME_LOWER}_icon_16px.png ${ICON_NAME}
294         sudo xdg-icon-resource install --context apps --size 22 ${ICON_PATH}/${PGM_NAME_LOWER}_icon_22px.png ${ICON_NAME}
295         sudo xdg-icon-resource install --context apps --size 32 ${ICON_PATH}/${PGM_NAME_LOWER}_icon_32px.png ${ICON_NAME}
296         sudo xdg-icon-resource install --context apps --size 48 ${ICON_PATH}/${PGM_NAME_LOWER}_icon_48px.png ${ICON_NAME}
297
298         if [ -e ${ICON_PATH}/${PGM_NAME_LOWER}_icon.svg -a -d /usr/share/icons/hicolor/scalable/apps ]; 
299         then
300                 sudo cp -f ${ICON_PATH}/${PGM_NAME_LOWER}_icon.svg  /usr/share/icons/hicolor/scalable/apps/${ICON_NAME}.svg
301         fi
302
303         sudo xdg-desktop-menu install ${MENU_FILE_PATH}/${MENU_FILE}
304
305         echo ""
306         echo "Creating a desktop link for ${PGM_NAME}"
307         cp ${MENU_FILE_PATH}/${MENU_FILE} ~/Desktop/${DESKTOP_LINK_FILE}
308         chmod ugo+rx ~/Desktop/${DESKTOP_LINK_FILE}
309 else
310         echo ""
311         echo "Creating a desktop link for ${PGM_NAME}"
312         cp ${MENU_FILE_PATH}/${MENU_FILE} ~/Desktop/${DESKTOP_LINK_FILE}
313         chmod ugo+rx ~/Desktop/${DESKTOP_LINK_FILE}
314 fi
315
316 echo ""
317 echo "Copying uninstall script to ${INSTALL_DEST_BASE}"
318 echo ""
319
320 sudo cp -f ${BUNDLE_DIR}/bin/*.uninstall.sh ${INSTALL_DEST_BASE}/.
321
322 ###########################
323 # Check Jack and qjackctl
324 ###########################
325
326 echo ""
327 echo "Checking to see if Jack is installed"
328 echo ""
329
330 if ! which jackd > /dev/null;
331 then
332         echo ""
333         echo "The program Jack is missing from this system. Jack is a required component of $PGM_NAME."
334         echo ""
335
336         ANSWER=$(VaildateYesNoQuestion "Install jack using system software repository?")
337
338         if test "y" = $ANSWER;
339         then
340                 echo "Attempting to install Jack"
341                 SystemInstall "jackd"
342
343                 if [ $? -ne 0 ];
344                 then
345                         echo ""
346                         read -p "Press ENTER to continue:" BLAH
347                 fi
348         fi
349 else
350         echo "Jack OK"
351 fi
352
353
354 if ! which qjackctl > /dev/null;
355 then
356         echo ""
357         echo "The program QjackCtl is missing from this system. QjackCtl is an extremely useful tool"
358         echo "for any system that runs JACK applications like Ardour. We recommend that you install it."
359         echo ""
360
361         ANSWER=$(VaildateYesNoQuestion "Install QjackCtl using system software repository?")
362
363         if test "y" = $ANSWER;
364         then
365                 echo "Attempting to install QjackCtl"
366                 SystemInstall "qjackctl"
367
368                 if [ $? -ne 0 ];
369                 then
370                         echo ""
371                         read -p "Press ENTER to continue:" BLAH
372                 fi
373         fi
374 fi
375
376
377 ########################
378 # Run Sanity Check
379 ########################
380
381 USER_GROUP_ADJUSTED="f"
382
383 if ! ./${BUNDLE_DIR}/bin/sanityCheck -a > /dev/null;
384 then
385         echo ""
386         echo "System failed the quick sanity check... Looking for the cause"
387
388         if ! ./${BUNDLE_DIR}/bin/sanityCheck -rt > /dev/null;
389         then
390                 echo ""
391                 echo "System does not allow realtime for the current user... Looking for a solution"            
392                 
393                 if ./${BUNDLE_DIR}/bin/sanityCheck -hasaudiogroup > /dev/null;
394                 then
395                         if ./${BUNDLE_DIR}/bin/sanityCheck -memberaudiogroup > /dev/null 2>&1;
396                         then
397                                 ## This is an odd case. We have an audio group and are a member.
398                                 echo ""
399                                 echo "!!! WARNING !!! - The current user can not execute realtime processes."
400                                 echo "This will adversely affect audio latency."
401                                 echo "This system has an audio group and the user is a member. If jack was"
402                                 echo "just installed, a simple log out/in may fix this."
403                                 echo ""
404                                 echo "For best results, please correct this on your system."
405                                 echo "(Hint: check /etc/security/limits.conf or /etc/security/limits.d/)" 
406                                 echo ""
407                                 read -p "Press ENTER to continue:" BLAH
408                         else
409                                 # Not a member of an audio group. Try to fix it.
410                                 
411                                 if ./${BUNDLE_DIR}/bin/sanityCheck -hasgroup audio > /dev/null && find /etc/security -type f -name "*.conf" | xargs grep -q "^@audio.*rtprio";
412                                 then
413                                         # add user to audio group
414                                         echo ""
415                                         echo "Adding user `whoami` to the audio group."
416                                         echo "This should allow you to run realtime tasks. Please re-login for this change to take affect."
417                                         echo ""
418                                         read -p "Press ENTER to continue:" BLAH
419
420                                         user=`whoami`
421                                         if sudo usermod -a -G audio $user;
422                                         then
423                                                 USER_GROUP_ADJUSTED="t"
424                                         else
425                                                 echo ""
426                                                 echo "!!! ERROR !!! - Not able to add user to the audio group (usermod failed)!"
427                                                 echo ""
428                                                 echo "Please add yourself to the audio group and re-login"
429                                                 echo ""
430                                                 read -p "Press ENTER to continue:" BLAH
431                                         fi
432
433                                 elif ./${BUNDLE_DIR}/bin/sanityCheck -hasgroup jackuser > /dev/null && find /etc/security -type f -name "*.conf" | xargs grep -q "^@jackuser.*rtprio";
434                                 then
435                                         # add user to jackuser group
436                                         echo ""
437                                         echo "Adding user `whoami` to the jackuser group."
438                                         echo "This should allow you to run realtime tasks. Please re-login for this change to take affect."
439                                         echo ""
440                                         read -p "Press ENTER to continue:" BLAH
441
442                                         user=`whoami`
443                                         if sudo usermod -a -G jackuser $user;
444                                         then
445                                                 USER_GROUP_ADJUSTED="t"
446                                         else
447                                                 echo ""
448                                                 echo "!!! ERROR !!! - Not able to add user to the jackuser group."
449                                                 echo ""
450                                                 echo "Please add yourself to the audio group and re-login"
451                                                 echo ""
452                                                 read -p "Press ENTER to continue:" BLAH
453                                         fi
454                                         
455
456                                 fi
457                         fi
458                 else
459                         # No audio group found on this system!
460                         echo ""
461                         echo "!!! WARNING !!! - The system does not seem to have an audio group (audio or jackuser)."
462                         echo ""
463                         echo "We will not attempt to fix this. Please configure your system to allow"
464                         echo "non-root users to execute realtime tasks."
465                         echo ""
466                         read -p "Press ENTER to continue:" BLAH
467                 fi
468         fi
469
470         if ! ./${BUNDLE_DIR}/bin/sanityCheck -freqscaling > /dev/null;
471         then
472                 echo ""
473                 echo "!!! WARNING !!! - Your system seems to use frequency scaling."
474                 echo "This can have a serious impact on audio latency. You have two choices:"
475                 echo "(1) turn it off, e.g. by chosing the 'performance' governor."
476                 echo "(2) Use the HPET clocksource by passing \"-c h\" to JACK"
477                 echo "(this second option only works on relatively recent computers)"
478                 echo ""
479                 read -p "Press ENTER to continue:" BLAH
480         fi
481
482         if [ "f" = $USER_GROUP_ADJUSTED ];
483         then
484                 if ! ./${BUNDLE_DIR}/bin/sanityCheck -memlock > /dev/null;
485                 then
486                         echo ""
487                         echo "!!! WARNING !!! - You are not allowed to lock memory."
488                         echo ""
489                         echo "We will not attempt to fix this. Please configure your system to allow"
490                         echo "non-root users to execute lock memory."
491                         echo ""
492                         read -p "Press ENTER to continue:" BLAH
493                 fi
494         fi
495 fi
496
497
498 ########################
499 # Install Complete
500 ########################
501
502 echo ""
503 echo "Cleaning up"
504 rm -rf ${BUNDLE_DIR}/
505
506 echo ""
507 echo "!!! Install Complete !!!"
508
509 if [ "t" = $USER_GROUP_ADJUSTED ];
510 then
511         echo "You will need to logout and then login again for all changes to be complete"
512 fi
513
514 echo ""
515 read -p "Press ENTER to close this window:" BLAH
516
517