installJRMC 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060
  1. #!/usr/bin/env bash
  2. # @file installJRMC
  3. # @brief Installs JRiver Media Center and associated services
  4. # @description See installJRMC --help or print_help() below for usage
  5. # Copyright (c) 2021-2025 Bryan C. Roessler
  6. # This software is released under the Apache License.
  7. # https://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # TODO (v2)
  10. # * Interactive (ncurses) mode
  11. # * Additional containerization (createrepo and rpmbuild)
  12. #
  13. # BUGS
  14. # * No createrepo on Mint
  15. #
  16. # NOTES
  17. # * Be careful with tabs in heredocs
  18. # * Avoid execute() for stdout
  19. #
  20. # Allow indirection to match service names to their functions
  21. # shellcheck disable=SC2317
  22. shopt -s extglob
  23. declare -g SCRIPT_VERSION="1.34.6-dev"
  24. declare -g MC_VERSION="34.0.48" # do find all replace (hardcoded fallback)
  25. declare -g MC_REPO="bookworm" # should match the MC_VERSION
  26. declare -g BOARD_ID="89.0" # MC34 board ID for automatic version detection
  27. declare -gi SELF_UPDATE_SWITCH=1 # 0 to disable installJRMC self-update
  28. declare -g SCRIPT_URL="https://git.bryanroessler.com/bryan/installJRMC/raw/master/installJRMC" # self-update URL
  29. # declare -g SCRIPT_URL="https://raw.githubusercontent.com/cryobry/installJRMC/refs/heads/master/installJRMC" # backup URL
  30. declare -gi DEBUG=${DEBUG:-0} # set default debug and allow DEBUG env override (default: disabled)
  31. # @description Print help text
  32. print_help() {
  33. debug "${FUNCNAME[0]}()"
  34. cat <<-EOF
  35. SEE:
  36. README.md for more information
  37. USAGE:
  38. installJRMC [[OPTION] [VALUE]]...
  39. installJRMC defaults to --install=repo on platforms with a JRiver repository and --install=local on all others.
  40. Specifying --build, --createrepo, --service, or --uninstall disables the default install method.
  41. OPTIONS
  42. --install, -i repo|local
  43. repo: Install MC from repository, updates are handled by the system package manager
  44. local: Build and install MC locally from official source package
  45. --build[=suse|fedora|centos|mandriva]
  46. Build RPM from source DEB but do not install
  47. Optionally, specify a target distro for cross-building (ex. --build=suse, note the '=')
  48. --compat
  49. Build/install MC locally without minimum dependency version requirements
  50. --mcversion VERSION
  51. Specify the MC version, ex. "$MC_VERSION" or "${MC_VERSION%%.*}" (default: latest release)
  52. --arch VERSION
  53. Specify the target MC architecture, ex. "amd64", "arm64", etc (default: host)
  54. --mcrepo REPO
  55. Specify the MC repository, ex. "bullseye", "bookworm", "noble", etc (default: auto)
  56. --outputdir PATH
  57. Generate reusable installJRMC output in this PATH (default: ./output)
  58. --restorefile MJR_FILE
  59. Restore file location for automatic license registration
  60. --betapass PASSWORD
  61. Enter beta team password for access to beta builds
  62. --service, -s SERVICE
  63. See SERVICES section below for a list of possible services to install
  64. --service-type user|system
  65. Starts services at boot (system) or at user login (user) (default: per service, see SERVICES)
  66. --container, -c CONTAINER (TODO: Under construction)
  67. See CONTAINERS section below for a list of possible services to install
  68. --createrepo[=suse|fedora|centos|mandriva]
  69. Build rpm, copy to webroot, and run createrepo.
  70. Use in conjunction with --build=TARGET for crossbuilding repos
  71. Optionally, specify a target distro for non-native repo (ex. --createrepo=fedora, note the '=')
  72. --createrepo-webroot PATH
  73. Specify the webroot directory to install the repo (default: /var/www/jriver)
  74. --createrepo-user USER
  75. Specify the web server user if it differs from \$USER
  76. --no-update
  77. Disable automatic installJRMC self-update
  78. --uninstall, -u
  79. Uninstall JRiver MC, remove services, containers, and firewall rules (does not remove library files)
  80. --yes, -y, --auto
  81. Assume yes response to questions
  82. --version, -v
  83. Print installJRMC version and exit
  84. --debug, -d
  85. Print debug output
  86. --help, -h
  87. Print help dialog and exit
  88. SERVICES
  89. jriver-mediaserver (default --service-type=user)
  90. Enable and start a mediaserver systemd service (requires an existing X server)
  91. jriver-mediacenter (user)
  92. Enable and start a mediacenter systemd service (requires an existing X server)
  93. jriver-x11vnc (user)
  94. Enable and start x11vnc for the local desktop (requires an existing X server)
  95. Usually combined with jriver-mediaserver or jriver-mediacenter services
  96. --vncpass and --display are optional (see below)
  97. jriver-xvnc (system)
  98. Enable and start a new Xvnc session running JRiver Media Center
  99. --vncpass PASSWORD
  100. Set the vnc password for x11vnc/Xvnc access. If no password is set, installJRMC
  101. will either use existing password stored in \$HOME/.vnc/jrmc_passwd or else no password
  102. --display DISPLAY
  103. Display to use for x11vnc/Xvnc (default: The current display (x11vnc) or the
  104. current display incremented by 1 (Xvnc))
  105. jriver-createrepo (system)
  106. Install hourly service to build latest MC RPM and run createrepo
  107. EOF
  108. }
  109. # @description Parses user input and sets sensible defaults
  110. # @arg $@ User input
  111. parse_input() {
  112. debug "${FUNCNAME[0]}()" "$@"
  113. declare -gi BUILD_SWITCH REPO_INSTALL_SWITCH LOCAL_INSTALL_SWITCH \
  114. CONTAINER_INSTALL_SWITCH CREATEREPO_SWITCH SNAP_INSTALL_SWITCH \
  115. APPIMAGE_INSTALL_SWITCH COMPAT_SWITCH UNINSTALL_SWITCH YES_SWITCH DEBUG=0
  116. declare -g USER_MC_VERSION USER_MC_MVERSION USER_MC_RELEASE USER_MC_REPO USER_ARCH MJR_FILE \
  117. BETAPASS SERVICE_TYPE VNCPASS USER_DISPLAY BUILD_TARGET CREATEREPO_TARGET
  118. local long_opts short_opts input
  119. long_opts="install:,build::,outputdir:,mcversion:,arch:,mcrepo:,compat,"
  120. long_opts+="restorefile:,betapass:,"
  121. long_opts+="service-type:,service:,services:,"
  122. long_opts+="version,debug,verbose,help,uninstall,yes,auto,no-update,"
  123. long_opts+="createrepo::,createrepo-webroot:,createrepo-user:,"
  124. long_opts+="vncpass:,display:,container:"
  125. short_opts="+i:b::s:c:uyvdh"
  126. if input=$(getopt -o $short_opts -l $long_opts -- "$@"); then
  127. eval set -- "$input"
  128. while true; do
  129. case $1 in
  130. --install|-i) shift;
  131. case $1 in
  132. local|rpm|deb) BUILD_SWITCH=1 LOCAL_INSTALL_SWITCH=1 ;;
  133. repo|remote) REPO_INSTALL_SWITCH=1 ;;
  134. container) CONTAINER_INSTALL_SWITCH=1 ;;
  135. snap) SNAP_INSTALL_SWITCH=1 ;;
  136. appimage) APPIMAGE_INSTALL_SWITCH=1 ;;
  137. *) err "Invalid --install option passed"; exit 1 ;;
  138. esac
  139. ;;
  140. --build|-b) BUILD_SWITCH=1; shift; BUILD_TARGET="$1" ;;
  141. --outputdir) shift; OUTPUT_DIR="$1" ;;
  142. --mcversion) shift;
  143. if [[ $1 =~ ^([0-9]+)(\.[0-9]+\.[0-9]+)?(-([0-9]+))?$ ]]; then
  144. # Major version is required
  145. USER_MC_MVERSION="${BASH_REMATCH[1]}"
  146. # Set default release to 1 if not provided
  147. USER_MC_RELEASE="${BASH_REMATCH[4]:-1}"
  148. # If we get the full version, use it
  149. [[ -n ${BASH_REMATCH[2]} ]] && USER_MC_VERSION="${BASH_REMATCH[1]}${BASH_REMATCH[2]}"
  150. # Set major version defaults
  151. case "$USER_MC_MVERSION" in
  152. 34) MC_VERSION="${USER_MC_VERSION:-$MC_VERSION}" MC_REPO="bookworm" BOARD_ID="89.0" ;;
  153. 33) MC_VERSION="${USER_MC_VERSION:-33.0.72}" MC_REPO="bullseye" BOARD_ID="86.0" ;;
  154. 32) MC_VERSION="${USER_MC_VERSION:-32.0.58}" MC_REPO="bullseye" BOARD_ID="83.0" ;;
  155. 31) MC_VERSION="${USER_MC_VERSION:-31.0.83}" MC_REPO="bullseye" BOARD_ID="80.0" ;;
  156. 30) MC_VERSION="${USER_MC_VERSION:-30.0.96}" MC_REPO="buster" BOARD_ID="76.0" ;;
  157. 29) MC_VERSION="${USER_MC_VERSION:-29.0.91}" MC_REPO="buster" BOARD_ID="74.0" ;;
  158. 28) MC_VERSION="${USER_MC_VERSION:-28.0.110}" MC_REPO="buster" BOARD_ID="71.0" ;;
  159. 27) MC_VERSION="${USER_MC_VERSION:-27.0.88}" MC_REPO="buster" BOARD_ID="67.0" ;;
  160. 26) MC_VERSION="${USER_MC_VERSION:-26.0.107}" MC_REPO="jessie" BOARD_ID="64.0" ;;
  161. 25) MC_VERSION="${USER_MC_VERSION:-25.0.114}" MC_REPO="jessie" BOARD_ID="62.0" ;;
  162. 24) MC_VERSION="${USER_MC_VERSION:-24.0.78}" MC_REPO="jessie" BOARD_ID="58.0" ;;
  163. 23) MC_VERSION="${USER_MC_VERSION:-23.0.104}" MC_REPO="jessie" BOARD_ID="54.0" ;;
  164. 22) MC_VERSION="${USER_MC_VERSION:-22.0.102}" MC_REPO="jessie" BOARD_ID="51.0" ;;
  165. 21) MC_VERSION="${USER_MC_VERSION:-21.0.90}" MC_REPO="jessie" BOARD_ID="44.0" ;;
  166. 20) MC_VERSION="${USER_MC_VERSION:-20.0.131}" MC_REPO="jessie" BOARD_ID="35.0";;
  167. *) err "Bad --mcversion"; print_help; exit 1 ;;
  168. esac
  169. else
  170. err "Bad --mcversion"; print_help; exit 1
  171. fi
  172. ;;
  173. --arch) shift; USER_ARCH="$1" ;;
  174. --mcrepo) shift; USER_MC_REPO="$1" ;;
  175. --restorefile) shift; MJR_FILE="$1"; [[ -f $MJR_FILE ]] || err "Specified license $MJR_FILE missing." ;;
  176. --betapass) shift; BETAPASS="$1" ;;
  177. --service-type) shift; SERVICE_TYPE="$1" ;;
  178. --service|-s|--services) shift; SERVICES+=("$1") ;;
  179. --createrepo) shift; CREATEREPO_TARGET="$1"; BUILD_TARGET="$1"
  180. BUILD_SWITCH=1; CREATEREPO_SWITCH=1 ;;
  181. --createrepo-webroot) shift; CREATEREPO_WEBROOT="$1" ;;
  182. --createrepo-user) shift; CREATEREPO_USER="$1" ;;
  183. --vncpass) shift; VNCPASS="$1" ;;
  184. --display) shift; USER_DISPLAY="$1" ;;
  185. --compat) COMPAT_SWITCH=1; BUILD_SWITCH=1 ;;
  186. --no-update) SELF_UPDATE_SWITCH=0 ;;
  187. --container|-c) shift; CONTAINERS+=("$1") ;;
  188. --yes|-y|--auto) YES_SWITCH=1 ;;
  189. --version|-v) echo "Version: $SCRIPT_VERSION"; exit 0 ;;
  190. --debug|-d|--verbose) DEBUG=1 ;;
  191. --help|-h) print_help; exit 0 ;;
  192. --uninstall|-u) UNINSTALL_SWITCH=1 ;;
  193. --) shift; break ;;
  194. esac
  195. shift
  196. done
  197. else
  198. err "Incorrect option provided, see installJRMC --help"; exit 1
  199. fi
  200. # Set some default conditions
  201. if ! ((UNINSTALL_SWITCH || BUILD_SWITCH || CREATEREPO_SWITCH || LOCAL_INSTALL_SWITCH
  202. || CONTAINER_INSTALL_SWITCH || SNAP_INSTALL_SWITCH || APPIMAGE_INSTALL_SWITCH)) &&
  203. [[ ${#SERVICES[@]} -eq 0 && ${#CONTAINERS[@]} -eq 0 ]]; then
  204. debug "Defaulting to --install=repo"
  205. REPO_INSTALL_SWITCH=1
  206. fi
  207. if [[ -n $BETA_PASS ]] && ((REPO_INSTALL_SWITCH)); then
  208. echo "Warning: not all repositories have beta channels"
  209. echo "If the MC package is unavailable, try using --mcrepo to select another repository"
  210. fi
  211. }
  212. # @description Perform OS detection and generate OS-specific functions
  213. # @see parse_input
  214. init() {
  215. debug "${FUNCNAME[0]}()" "$@"
  216. declare -g USER
  217. declare -g SCRIPT_PATH; SCRIPT_PATH=$(readlink -f "${BASH_SOURCE[0]}")
  218. declare -g SCRIPT_DIR; SCRIPT_DIR=$(readlink -f "$(dirname "${BASH_SOURCE[0]}")")
  219. declare -g OUTPUT_DIR="$SCRIPT_DIR/output"
  220. declare -g CREATEREPO_WEBROOT="/var/www/jriver"
  221. declare -g CREATEREPO_USER="$USER" # can be root
  222. declare -g ID VERSION_ID ARCH MC_ARCH NAME
  223. declare -g MC_MVERSION MC_RELEASE MC_PKG MC_RPM MC_ROOT
  224. declare -ga PKG_INSTALL PKG_REMOVE PKG_UPDATE PKG_QUERY
  225. declare -ga SERVICES CONTAINERS
  226. parse_input "$@"
  227. # Try to save users from themselves
  228. if [[ $EUID -eq 0 ]]; then
  229. err "Running as root but attempting to continue"
  230. ask_ok "Continue as root user (not recommended)?" || exit 1
  231. elif [[ -n $SUDO_USER ]]; then
  232. err "Sudo detected, installJRMC should not be run with sudo but attempting to continue"
  233. ask_ok "Continue as user $SUDO_USER (unsupported and may result in permission issues)?" || exit 1
  234. USER="${SUDO_USER:-$USER}"
  235. fi
  236. # Run the self-updater if enabled
  237. ((SELF_UPDATE_SWITCH)) && update "$@"
  238. fix_permissions "$HOME/.jriver" "$USER"
  239. # Get host information
  240. [[ -f /etc/os-release ]] && source /etc/os-release
  241. # Detect host architecture and translate to MC convention
  242. if ARCH=$(uname -m); then
  243. case $ARCH in
  244. x86_64) MC_ARCH="amd64" ;;
  245. aarch64) MC_ARCH="arm64" ;;
  246. *) MC_ARCH="$ARCH" ;;
  247. esac
  248. else
  249. ARCH="x86_64"
  250. MC_ARCH="amd64"
  251. err "Failed to detect host arch, using default: $ARCH"
  252. fi
  253. echo "Host: $ID $VERSION_ID $ARCH"
  254. # Parse user-provided architecture, allow either convention
  255. if [[ -n $USER_ARCH ]]; then
  256. case $USER_ARCH in
  257. x86_64|amd64) ARCH="x86_64"; MC_ARCH="amd64" ;;
  258. aarch64|arm64) ARCH="aarch64"; MC_ARCH="arm64" ;;
  259. *) ARCH="$USER_ARCH" ;;
  260. esac
  261. fi
  262. # Normalize ID and set host-specific vars
  263. case $ID in
  264. debian|fedora|centos) ;;
  265. rhel|almalinux) ID="centos" ;;
  266. linuxmint|neon|zorin|*ubuntu*) ID="ubuntu" ;;
  267. raspbian) ID="debian" ;;
  268. *mandriva*) ID="mandriva"
  269. if ((REPO_INSTALL_SWITCH)); then
  270. debug "Automatically using --install=local for Mandriva"
  271. REPO_INSTALL_SWITCH=0
  272. BUILD_SWITCH=1
  273. LOCAL_INSTALL_SWITCH=1
  274. fi ;;
  275. manjaro|arch|cachyos) ID="arch"
  276. if ((REPO_INSTALL_SWITCH)); then
  277. debug "Automatically using --install=local for Arch"
  278. REPO_INSTALL_SWITCH=0
  279. BUILD_SWITCH=1
  280. LOCAL_INSTALL_SWITCH=1
  281. fi ;;
  282. *suse*) ID="suse"
  283. if ((REPO_INSTALL_SWITCH)); then
  284. debug "Automatically using --install=local for SUSE"
  285. REPO_INSTALL_SWITCH=0
  286. BUILD_SWITCH=1
  287. LOCAL_INSTALL_SWITCH=1
  288. fi ;;
  289. *) err "Auto-detecting distro, this is unreliable and --compat may be required"
  290. for cmd in dnf yum apt-get pacman; do
  291. if command -v "$cmd" &>/dev/null; then
  292. case "$cmd" in
  293. dnf) ID="fedora" ;;
  294. yum) ID="centos"; COMPAT_SWITCH=1 ;;
  295. apt-get) ID="ubuntu" ;;
  296. pacman) ID="arch" ;;
  297. esac
  298. break
  299. fi
  300. done
  301. if [[ -z $ID ]]; then
  302. err "OS detection failed!"
  303. if ask_ok "Continue with manual installation?"; then
  304. debug "Automatically using --install=local for unknown distro"
  305. ID="unknown"
  306. REPO_INSTALL_SWITCH=0
  307. BUILD_SWITCH=1
  308. LOCAL_INSTALL_SWITCH=1
  309. else
  310. exit 1
  311. fi
  312. fi ;;
  313. esac
  314. # Set distro-specific package manager commands for normalized IDs
  315. case $ID in
  316. fedora|centos|mandriva)
  317. local rpm_mgr
  318. rpm_mgr=$(command -v dnf &>/dev/null && echo "dnf" || echo "yum")
  319. PKG_INSTALL=(sudo "$rpm_mgr" install -y)
  320. PKG_REMOVE=(sudo "$rpm_mgr" remove -y)
  321. PKG_UPDATE=(sudo "$rpm_mgr" makecache)
  322. PKG_QUERY=(rpm -q)
  323. PKG_INSTALL_LOCAL() { install_mc_rpm; }
  324. ;;
  325. debian|ubuntu)
  326. PKG_INSTALL=(sudo apt-get -f install --install-recommends -y -q0)
  327. PKG_REMOVE=(sudo apt-get remove --auto-remove -y -q0)
  328. PKG_UPDATE=(sudo apt-get update -y -q0)
  329. PKG_QUERY=(dpkg -s)
  330. PKG_INSTALL_LOCAL() { install_mc_deb "$@"; }
  331. ;;
  332. suse)
  333. PKG_INSTALL=(sudo zypper --gpg-auto-import-keys --non-interactive --quiet install --force --force-resolution --replacefiles --no-confirm)
  334. PKG_REMOVE=(sudo zypper --non-interactive --quiet remove --clean-deps)
  335. PKG_UPDATE=(sudo zypper --non-interactive --quiet refresh jriver)
  336. PKG_QUERY=(rpm -q)
  337. PKG_INSTALL_LOCAL() { install_mc_rpm; }
  338. ;;
  339. arch)
  340. PKG_INSTALL=(sudo pacman -Sy --noconfirm)
  341. PKG_REMOVE=(sudo pacman -Rs --noconfirm)
  342. PKG_UPDATE=(sudo pacman -Syy)
  343. PKG_QUERY=(sudo pacman -Qs)
  344. PKG_INSTALL_LOCAL() { install_mc_arch; }
  345. ;;
  346. unknown)
  347. PKG_INSTALL=(:)
  348. PKG_REMOVE=(:)
  349. PKG_UPDATE=(:)
  350. PKG_QUERY=(:)
  351. PKG_INSTALL_LOCAL() { install_mc_generic; }
  352. ;;
  353. esac
  354. # Set default targets
  355. BUILD_TARGET="${BUILD_TARGET:-$ID}"
  356. CREATEREPO_TARGET="${CREATEREPO_TARGET:-$ID}"
  357. # Repo selection
  358. # Match repo to the host on MC31+ unless overriden by user
  359. if [[ $ID =~ debian|ubuntu && "${USER_MC_MVERSION:-${MC_VERSION%%.*}}" -ge 31 ]]; then
  360. MC_REPO=${UBUNTU_CODENAME:-${VERSION_CODENAME:-$MC_REPO}}
  361. fi
  362. MC_REPO="${USER_MC_REPO:-$MC_REPO}" # allow user override
  363. echo "MC source -> target: $MC_REPO $MC_ARCH -> $BUILD_TARGET $ARCH"
  364. # Retrieves the latest MC version number if we need it
  365. if ((BUILD_SWITCH || LOCAL_INSTALL_SWITCH || CREATEREPO_SWITCH)); then
  366. get_latest_mc_version
  367. fi
  368. # Set MC version variables
  369. MC_VERSION="${USER_MC_VERSION:-$MC_VERSION}"
  370. MC_RELEASE="${USER_MC_RELEASE:-1}"
  371. MC_MVERSION="${USER_MC_MVERSION:-${MC_VERSION%%.*}}"
  372. MC_PKG="mediacenter$MC_MVERSION"
  373. MC_RPM="$OUTPUT_DIR/RPMS/$ARCH/mediacenter$MC_MVERSION-$MC_VERSION-$MC_RELEASE.$ARCH.rpm"
  374. MC_ROOT="/usr/lib/jriver/Media Center $MC_MVERSION"
  375. # Generate explicit package name
  376. if [[ -n $USER_MC_VERSION ]]; then
  377. # Append explicit package version when user provides --mcversion
  378. case $ID in
  379. fedora|centos|suse|mandriva) MC_PKG+="-$MC_VERSION" ;;
  380. debian|ubuntu) MC_PKG+="=$MC_VERSION" ;;
  381. esac
  382. fi
  383. }
  384. # @description Determines the latest JRiver MC version using several methods
  385. get_latest_mc_version() {
  386. debug "${FUNCNAME[0]}()"
  387. local mc_version_source
  388. # User --mcversion
  389. if [[ -n $USER_MC_VERSION ]]; then
  390. mc_version_source="user input"
  391. # Containerized package manager
  392. elif create_mc_apt_container &&
  393. MC_VERSION=$(sudo buildah run "$CNT" -- apt-cache policy "mediacenter${USER_MC_MVERSION:-${MC_VERSION%%.*}}" | awk '/Candidate:/ {sub(/-.*/, "", $2); print $2}' | sort -V | tail -n1) &&
  394. execute sudo buildah rm "$CNT" &&
  395. [[ $MC_VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  396. mc_version_source="containerized package manager"
  397. # Fallback to webscrape
  398. elif MC_VERSION=$(download "https://yabb.jriver.com/interact/index.php/board,$BOARD_ID.html" "-" | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+' | head -n 1) &&
  399. [[ $MC_VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  400. mc_version_source="webscrape"
  401. # Fallback to hardcoded value
  402. else
  403. mc_version_source="hardcoded"
  404. echo "Warning! Using hardcoded version number"
  405. fi
  406. echo "Selected MC version $MC_VERSION from the $MC_REPO repo (via $mc_version_source)"
  407. }
  408. # @description Installs a package using the system package manager
  409. # @arg $1 array One or more package names
  410. # @option --no-install-check Do not check if package is already installed
  411. # @option --no-gpg-check Disable GPG checks for RPM based distros
  412. # @option --allow-downgrades Useful for installing specific MC versions
  413. # @option --silent | -s Do not print errors (useful for optional packages)
  414. install_package() {
  415. debug "${FUNCNAME[0]}()" "$@"
  416. local -a pkg_array install_flags
  417. local -A pkg_aliases
  418. local input pkg _pkg
  419. local -i no_install_check=0 allow_downgrades=0 silent=0 refresh=0 no_gpg_check=0 reinstall=0
  420. local long_opts="no-install-check,allow-downgrades,no-gpg-check,refresh,reinstall,silent"
  421. local -a pkg_install=("${PKG_INSTALL[@]}")
  422. input=$(getopt -o +s -l "$long_opts" -- "$@") || { err "Incorrect options provided"; exit 1; }
  423. eval set -- "$input"
  424. while true; do
  425. case $1 in
  426. --no-install-check) no_install_check=1 ;;
  427. --allow-downgrades) allow_downgrades=1 ;;
  428. --no-gpg-check) no_gpg_check=1 ;;
  429. --refresh) refresh=1 ;;
  430. --reinstall) reinstall=1 ;;
  431. --silent|-s) silent=1 ;;
  432. --) shift; break ;;
  433. esac
  434. shift
  435. done
  436. # Define distribution-specific package aliases
  437. case $ID in
  438. debian|ubuntu) pkg_aliases=(
  439. [rpm-build]="rpm"
  440. [createrepo_c]="createrepo"
  441. [tigervnc-server]="tigervnc-standalone-server"
  442. ) ;;
  443. suse) pkg_aliases=(
  444. [buildah]="buildah fuse-overlayfs"
  445. ) ;;
  446. mandriva) pkg_aliases=(
  447. [dpkg]="dpkg gnutar"
  448. ) ;;
  449. esac
  450. # Filter out already installed packages to create pkg_array
  451. for pkg in "$@"; do
  452. # Use alias if present, otherwise just pkg itself
  453. pkg_names=("$pkg")
  454. if [[ -v pkg_aliases[$pkg] ]]; then
  455. debug "Aliasing $pkg to ${pkg_aliases[$pkg]}"
  456. IFS=' ' read -ra pkg_names <<< "${pkg_aliases[$pkg]}"
  457. fi
  458. for p in "${pkg_names[@]}"; do
  459. if (( no_install_check )) ||
  460. ! { command -v "$p" &>/dev/null || "${PKG_QUERY[@]}" "$p" &>/dev/null; }; then
  461. pkg_array+=("$p")
  462. else
  463. debug "$p is already installed, skipping installation"
  464. fi
  465. done
  466. done
  467. # Generate installation flags based on the distribution
  468. case $ID in
  469. debian|ubuntu)
  470. ((allow_downgrades)) && install_flags+=(--allow-downgrades)
  471. ((reinstall)) && install_flags+=(--reinstall) ;;
  472. fedora|centos|mandriva)
  473. ((allow_downgrades)) && install_flags+=(--allowerasing)
  474. ((no_gpg_check)) && install_flags+=(--nogpgcheck)
  475. ((refresh)) && install_flags+=(--refresh)
  476. # Only add reinstall flag for mediacenter package
  477. if ((reinstall)) && [[ ${#pkg_array[@]} -eq 1 ]] && "${PKG_QUERY[@]}" "mediacenter$MC_MVERSION" &>/dev/null; then
  478. pkg_install=("${pkg_install[@]/install/reinstall}")
  479. fi
  480. ;;
  481. suse)
  482. ((no_gpg_check)) && install_flags+=(--allow-unsigned-rpm) ;;
  483. esac
  484. # Install packages
  485. if [[ ${#pkg_array[@]} -gt 0 ]]; then
  486. if ! execute "${pkg_install[@]}" "${install_flags[@]}" "${pkg_array[@]}"; then
  487. ((silent)) || err "Failed to install ${pkg_array[*]}"
  488. return 1
  489. fi
  490. fi
  491. return 0
  492. }
  493. # @description install host-specific external repos
  494. install_external_repos() {
  495. debug "${FUNCNAME[0]}()"
  496. case $ID in
  497. ubuntu)
  498. if ! grep -E '^deb|^Components' /etc/apt/sources.list /etc/apt/sources.list.d/* | grep -q universe; then
  499. echo "Adding universe repository"
  500. if ! execute sudo add-apt-repository -y universe; then
  501. err "Adding universe repository failed"
  502. fi
  503. fi
  504. ;;
  505. centos)
  506. if ! command -v dpkg &>/dev/null; then
  507. echo "Adding EPEL repository"
  508. if ! install_package epel-release; then
  509. # If epel-release is not available, install it manually
  510. install_package --no-install-check \
  511. "https://dl.fedoraproject.org/pub/epel/epel-release-latest-${VERSION_ID%%.*}.noarch.rpm"
  512. fi
  513. fi
  514. if ! "${PKG_QUERY[@]}" rpmfusion-free-release &>/dev/null; then
  515. echo "Installing the RPMFusion repository"
  516. install_package --no-install-check \
  517. "https://download1.rpmfusion.org/free/el/rpmfusion-free-release-${VERSION_ID%%.*}.noarch.rpm"
  518. fi
  519. # Install mesa-va-drivers-freeworld separately from the RPM using dnf swap
  520. # install_mesa_freeworld # no longer provided by RPMFusion for EL9, etc.
  521. ;;
  522. fedora)
  523. if ! "${PKG_QUERY[@]}" rpmfusion-free-release &>/dev/null; then
  524. echo "Installing the RPMFusion repository"
  525. install_package --no-install-check \
  526. "https://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-$VERSION_ID.noarch.rpm"
  527. fi
  528. ;;
  529. suse) : # TODO may be needed if X11_XOrg is made unavailable in default repos
  530. # if ! zypper repos | grep -q "X11_XOrg"; then
  531. # echo "Installing the X11 repository"
  532. # execute sudo zypper --non-interactive --quiet addrepo \
  533. # "https://download.opensuse.org/repositories/X11:/XOrg/${NAME// /_}/X11:XOrg.repo"
  534. # execute sudo zypper --non-interactive --quiet refresh
  535. # fi
  536. ;;
  537. mandriva)
  538. local branch
  539. branch=$(grep ^PRETTY_NAME= /etc/os-release | tr -d '"' | rev | cut -d' ' -f1 | rev | tr '[:upper:]' '[:lower:]')
  540. execute sudo dnf config-manager --set-enabled "${branch}-${ARCH}-extra"
  541. ;;
  542. esac
  543. }
  544. # @description Installs mesa-va-drivers-freeworld on Fedora
  545. install_mesa_freeworld() {
  546. debug "${FUNCNAME[0]}()"
  547. local pkg freeworld_pkg
  548. case $ID in
  549. fedora)
  550. for pkg in mesa-va-drivers mesa-vdpau-drivers mesa-vulkan-drivers; do
  551. freeworld_pkg="${pkg}-freeworld"
  552. if ! "${PKG_QUERY[@]}" "$freeworld_pkg" &>/dev/null; then
  553. if "${PKG_QUERY[@]}" "$pkg" &>/dev/null; then
  554. if ! execute sudo dnf swap -y "$pkg" "$freeworld_pkg"; then
  555. err "Package swap failed for $pkg!"
  556. fi
  557. else
  558. "${PKG_INSTALL[@]}" "$freeworld_pkg"
  559. fi
  560. fi
  561. done
  562. ;;
  563. esac
  564. }
  565. # @description Installs JRiver Media Center from a remote repository
  566. install_mc_repo() {
  567. debug "${FUNCNAME[0]}()"
  568. local repo_file repo_text channel
  569. case $ID in
  570. fedora|centos)
  571. repo_file="/etc/yum.repos.d/jriver.repo"
  572. read -r -d '' repo_text <<-EOF
  573. [jriver]
  574. name=JRiver Media Center by BryanC
  575. baseurl=https://repos.bryanroessler.com/jriver
  576. gpgcheck=0
  577. EOF
  578. ;;
  579. debian|ubuntu)
  580. [[ -n $BETAPASS ]] && channel="beta" || channel="latest"
  581. local major_version="${VERSION_ID%%.*}"
  582. local keyfile="/usr/share/keyrings/jriver-com-archive-keyring.gpg"
  583. if [[ ($ID == "ubuntu" && $major_version -ge 24) ||
  584. ($ID == "debian" && (-z $major_version || $major_version -ge 12)) ]]; then
  585. if [[ $channel == "beta" ]]; then
  586. repo_file="/etc/apt/sources.list.d/jriver-beta.sources"
  587. else
  588. repo_file="/etc/apt/sources.list.d/jriver.sources"
  589. fi
  590. # Remove deprecated repo files
  591. old_repo_files=(
  592. "/etc/apt/sources.list.d/jriver.list"
  593. "/etc/apt/sources.list.d/jriver-beta.list"
  594. "/etc/apt/sources.list.d/jriver_beta.list"
  595. )
  596. for f in "${old_repo_files[@]}"; do
  597. [[ -f $f ]] && execute sudo rm -f "$f"
  598. done
  599. read -r -d '' repo_text <<-EOF
  600. Types: deb
  601. URIs: https://dist.jriver.com/$channel/mediacenter/
  602. Signed-By: $keyfile
  603. Suites: $MC_REPO
  604. Components: main
  605. Architectures: amd64 armhf arm64
  606. EOF
  607. else
  608. if [[ $channel == "beta" ]]; then
  609. execute sudo rm -f "/etc/apt/sources.list.d/jriver_beta.list"
  610. repo_file="/etc/apt/sources.list.d/jriver-beta.list"
  611. else
  612. repo_file="/etc/apt/sources.list.d/jriver.list"
  613. fi
  614. repo_text="deb [signed-by=$keyfile arch=amd64,armhf,arm64] https://dist.jriver.com/$channel/mediacenter/ $MC_REPO main"
  615. fi
  616. echo "Installing JRiver Media Center GPG key"
  617. download "https://dist.jriver.com/mediacenter@jriver.com.gpg.key" "-" |
  618. gpg --dearmor | sudo tee "$keyfile" &>/dev/null
  619. ;;
  620. *)
  621. err "An MC repository for $ID is not yet available"
  622. err "Use --install=local to install MC on $ID"
  623. return 1
  624. ;;
  625. esac
  626. # Remove existing repository file if it exists
  627. [[ -f $repo_file ]] && execute sudo rm -f "$repo_file"
  628. echo "Adding MC repository file: $repo_file"
  629. debug "$repo_text"
  630. sudo tee "$repo_file" &>/dev/null <<< "$repo_text"
  631. # Add older repository for libwebkit2gtk-4.0-37, etc, on newer Debian/Ubuntu
  632. if add_temp_repo; then
  633. trap 'execute sudo rm -f "$TEMP_REPO_FILE"' EXIT
  634. else
  635. err "Failed to add temporary repository"
  636. return 1
  637. fi
  638. execute "${PKG_UPDATE[@]}" || { err "Package update failed!"; return 1; }
  639. echo "Installing $MC_PKG package"
  640. if ! install_package \
  641. --no-install-check \
  642. --allow-downgrades \
  643. --no-gpg-check \
  644. --reinstall \
  645. "$MC_PKG"; then
  646. err "Package install failed!"
  647. return 1
  648. fi
  649. # Unset the trap and remove temporary legacy repository
  650. trap - EXIT
  651. [[ -f $TEMP_REPO_FILE ]] && execute sudo rm -f "$TEMP_REPO_FILE"
  652. return 0
  653. }
  654. # @description Acquires the source DEB package from JRiver
  655. acquire_deb() {
  656. debug "${FUNCNAME[0]}()"
  657. declare -g MC_DEB MC_SOURCE
  658. local fname mnt
  659. [[ -d $OUTPUT_DIR/SOURCES ]] || execute mkdir -p "$OUTPUT_DIR/SOURCES"
  660. # Usually JRiver excludes the release number from the filename
  661. # but in some cases (test builds) it may be included
  662. if [[ $MC_RELEASE -gt 1 ]]; then
  663. fname="MediaCenter-$MC_VERSION-$MC_RELEASE-$MC_ARCH.deb"
  664. else
  665. fname="MediaCenter-$MC_VERSION-$MC_ARCH.deb"
  666. fi
  667. MC_DEB="$OUTPUT_DIR/SOURCES/$fname"
  668. MC_SOURCE="https://files.jriver-cdn.com/mediacenter/channels/v$MC_MVERSION/latest/$fname"
  669. # If deb file already exists, skip download
  670. if [[ -f $MC_DEB ]]; then
  671. if [[ $(stat -c%s "$MC_DEB") -lt 10000000 ]]; then
  672. echo "Removing existing DEB under 10MB: $MC_DEB"
  673. execute rm -f "$MC_DEB"
  674. else
  675. echo "Using existing DEB: $MC_DEB"
  676. return 0
  677. fi
  678. fi
  679. # Download the deb file using the containerized package manager
  680. if ! { create_mc_apt_container "apt-get download --allow-unauthenticated mediacenter$MC_MVERSION &>/dev/null" &&
  681. mnt="$(sudo buildah mount "$CNT")" &&
  682. execute sudo find "$mnt" -maxdepth 1 -type f -name "*.deb" -exec cp {} "$MC_DEB" \; &&
  683. [[ -f $MC_DEB ]] &&
  684. execute sudo buildah umount "$CNT" &&
  685. execute sudo buildah rm "$CNT"; }; then
  686. debug "Failed to download DEB using containerized package manager"
  687. echo "Using legacy download method"
  688. # Define the repository search order
  689. local -a repos
  690. [[ -n $BETAPASS ]] && repos=("https://files.jriver-cdn.com/mediacenter/channels/v$MC_MVERSION/beta/$BETAPASS/$fname")
  691. repos+=(
  692. "https://files.jriver-cdn.com/mediacenter/channels/v$MC_MVERSION/latest/$fname"
  693. "https://files.jriver-cdn.com/mediacenter/test/$fname")
  694. # Loop through the repositories and attempt to download
  695. for repo in "${repos[@]}"; do
  696. echo -n "$repo --> "
  697. if download "$repo" "$MC_DEB"; then
  698. echo "Found!"
  699. MC_SOURCE="$repo"
  700. break
  701. fi
  702. echo "Not found"
  703. done
  704. fi
  705. # Return if the download was successful
  706. [[ -f $MC_DEB ]]
  707. }
  708. # @description Translates upstream DEB dependencies for several distros
  709. translate_packages() {
  710. debug "${FUNCNAME[0]}()" "$*"
  711. local deb_file="$1"
  712. declare -n requires_arr="$2"
  713. declare -n recommends_arr="$3"
  714. local -i i
  715. # Load deb dependencies into array
  716. IFS=',' read -ra requires_arr <<< "$(dpkg-deb -f "$deb_file" Depends)"
  717. IFS=',' read -ra recommends_arr <<< "$(dpkg-deb -f "$deb_file" Recommends)"
  718. # Clean up formatting
  719. requires_arr=("${requires_arr[@]%%|*}")
  720. requires_arr=("${requires_arr[@]/?:/}")
  721. requires_arr=("${requires_arr[@]# }")
  722. requires_arr=("${requires_arr[@]% }")
  723. requires_arr=("${requires_arr[@]//\(/}")
  724. requires_arr=("${requires_arr[@]//)/}")
  725. recommends_arr=("${recommends_arr[@]%%|*}")
  726. recommends_arr=("${recommends_arr[@]/?:/}")
  727. recommends_arr=("${recommends_arr[@]# }")
  728. recommends_arr=("${recommends_arr[@]% }")
  729. recommends_arr=("${recommends_arr[@]//\(/}")
  730. recommends_arr=("${recommends_arr[@]//)/}")
  731. # Translate package names
  732. case $BUILD_TARGET in
  733. fedora|centos)
  734. requires_arr=("${requires_arr[@]/libc6/glibc}")
  735. requires_arr=("${requires_arr[@]/libasound2/alsa-lib}")
  736. requires_arr=("${requires_arr[@]/libuuid1/libuuid}")
  737. requires_arr=("${requires_arr[@]/libx11-6/libX11}")
  738. requires_arr=("${requires_arr[@]/libxext6/libXext}")
  739. requires_arr=("${requires_arr[@]/libxcb1*/libxcb}") # TODO Remove minimum version for MC31 (*)
  740. requires_arr=("${requires_arr[@]/libxau6/libXau}")
  741. requires_arr=("${requires_arr[@]/libxdmcp6/libXdmcp}")
  742. requires_arr=("${requires_arr[@]/libstdc++6/libstdc++}")
  743. requires_arr=("${requires_arr[@]/libgtk-3-0/gtk3}")
  744. requires_arr=("${requires_arr[@]/libegl1/mesa-libEGL}")
  745. requires_arr=("${requires_arr[@]/libgl1/mesa-libGL}")
  746. requires_arr=("${requires_arr[@]/libgles2/libglvnd-gles}")
  747. requires_arr=("${requires_arr[@]/libgbm1/mesa-libgbm}")
  748. requires_arr=("${requires_arr[@]/libegl-mesa0/mesa-libEGL}")
  749. requires_arr=("${requires_arr[@]/libvulkan1/vulkan-loader}")
  750. requires_arr=("${requires_arr[@]/libpango1.0-0/pango}")
  751. requires_arr=("${requires_arr[@]/libpango-1.0-0/pango}")
  752. requires_arr=("${requires_arr[@]/libpangoft2-1.0-0/pango}")
  753. requires_arr=("${requires_arr[@]/libpangox-1.0-0/pango}")
  754. requires_arr=("${requires_arr[@]/libpangoxft-1.0-0/pango}")
  755. requires_arr=("${requires_arr[@]/libnss3/nss}")
  756. requires_arr=("${requires_arr[@]/libnspr4/nspr}")
  757. requires_arr=("${requires_arr[@]/libgomp1/libgomp}")
  758. requires_arr=("${requires_arr[@]/libfribidi0/fribidi}")
  759. requires_arr=("${requires_arr[@]/libfontconfig1/fontconfig}")
  760. requires_arr=("${requires_arr[@]/libfreetype6/freetype}")
  761. requires_arr=("${requires_arr[@]/libharfbuzz0b/harfbuzz}")
  762. requires_arr=("${requires_arr[@]/libva2/libva}")
  763. requires_arr=("${requires_arr[@]/libva-drm2/libva}")
  764. requires_arr=("${requires_arr[@]/libepoxy0/libepoxy}")
  765. requires_arr=("${requires_arr[@]/liblcms2-2/lcms2}")
  766. requires_arr=("${requires_arr[@]/libwebkit2gtk-4.0*/webkit2gtk4.0}")
  767. requires_arr=("${requires_arr[@]/libwebkit2gtk-4.1*/webkit2gtk4.1}")
  768. requires_arr=("${requires_arr[@]/libsdbus-c++1/sdbus-cpp}")
  769. recommends_arr=("${recommends_arr[@]/fdkaac/fdk-aac-free}")
  770. recommends_arr+=("mesa-va-drivers-freeworld|mesa-va-drivers")
  771. recommends_arr+=("mesa-vulkan-drivers-freeworld|mesa-vulkan-drivers")
  772. recommends_arr+=("mesa-vdpau-driver-freeworld|mesa-vdpau-driver")
  773. ;;
  774. suse)
  775. requires_arr=("${requires_arr[@]/libc6/glibc}")
  776. requires_arr=("${requires_arr[@]/libasound2/alsa-lib}")
  777. requires_arr=("${requires_arr[@]/libx11-6/libX11-6}")
  778. requires_arr=("${requires_arr[@]/libxext6/libXext6}")
  779. requires_arr=("${requires_arr[@]/libxdmcp6/libXdmcp6}")
  780. requires_arr=("${requires_arr[@]/libgtk-3-0/gtk3}")
  781. requires_arr=("${requires_arr[@]/libgl1/Mesa-libGL1}")
  782. requires_arr=("${requires_arr[@]/libpango-1.0-0/pango}")
  783. requires_arr=("${requires_arr[@]/libpangoft2-1.0-0/pango}")
  784. requires_arr=("${requires_arr[@]/libpangox-1.0-0/pango}")
  785. requires_arr=("${requires_arr[@]/libpangoxft-1.0-0/pango}")
  786. requires_arr=("${requires_arr[@]/libnss3/mozilla-nss}")
  787. requires_arr=("${requires_arr[@]/libnspr4/mozilla-nspr}")
  788. requires_arr=("${requires_arr[@]/libfribidi0/fribidi}")
  789. requires_arr=("${requires_arr[@]/libfontconfig1/fontconfig}")
  790. requires_arr=("${requires_arr[@]/libharfbuzz0b/libharfbuzz0}")
  791. requires_arr=("${requires_arr[@]/libwebkit2gtk-4.0*/libwebkit2gtk-4_0-37}")
  792. requires_arr=("${requires_arr[@]/libwebkit2gtk-4.1*/libwebkit2gtk-4_1-0}")
  793. for i in "${!requires_arr[@]}"; do
  794. [[ ${requires_arr[$i]} == "mesa-vulkan-drivers" ]] && unset -v 'requires_arr[i]'
  795. [[ ${requires_arr[$i]} == "libsdbus-c++1" ]] && unset -v 'requires_arr[i]'
  796. done
  797. recommends_arr+=(libvulkan1)
  798. recommends_arr+=(libvulkan_intel)
  799. recommends_arr+=(libvulkan_radeon)
  800. recommends_arr+=(libvulkan_nouveau)
  801. ;;
  802. mandriva)
  803. requires_arr=("${requires_arr[@]/libc6/glibc}")
  804. requires_arr=("${requires_arr[@]/libasound2/lib64asound2}")
  805. requires_arr=("${requires_arr[@]/libuuid1/lib64uuid1}")
  806. requires_arr=("${requires_arr[@]/libx11-6/lib64x11_6}")
  807. requires_arr=("${requires_arr[@]/libxext6/lib64xext6}")
  808. requires_arr=("${requires_arr[@]/libxcb1/lib64xcb1}")
  809. requires_arr=("${requires_arr[@]/libxdmcp6/lib64xdmcp6}")
  810. requires_arr=("${requires_arr[@]/libstdc++6/lib64stdc++6}")
  811. requires_arr=("${requires_arr[@]/libgtk-3-0/lib64gtk3_0}")
  812. requires_arr=("${requires_arr[@]/libgl1/lib64GL1}")
  813. requires_arr=("${requires_arr[@]/libgles2/lib64GLESv2_2}")
  814. requires_arr=("${requires_arr[@]/libegl-mesa0/lib64EGL_mesa0}")
  815. requires_arr=("${requires_arr[@]/libpango1.0-0/lib64pango1.0_0}")
  816. requires_arr=("${requires_arr[@]/libpango-1.0-0/lib64pango1.0_0}")
  817. requires_arr=("${requires_arr[@]/libpangoft2-1.0-0/lib64pangoft2_1.0_0}")
  818. requires_arr=("${requires_arr[@]/libpango-cairo-1.0-0/lib64pangocairo1.0_0}")
  819. requires_arr=("${requires_arr[@]/libpangoxft-1.0-0/lib64pangoxft1.0_0}")
  820. requires_arr=("${requires_arr[@]/libnss3/lib64nss3}")
  821. requires_arr=("${requires_arr[@]/libnspr4/lib64nspr4}")
  822. requires_arr=("${requires_arr[@]/libgomp1/lib64gomp1}")
  823. requires_arr=("${requires_arr[@]/libfribidi0/lib64fribidi0}")
  824. requires_arr=("${requires_arr[@]/libfontconfig1/lib64fontconfig}")
  825. requires_arr=("${requires_arr[@]/libfreetype6/lib64freetype6}")
  826. requires_arr=("${requires_arr[@]/libharfbuzz0b/lib64harfbuzz}")
  827. requires_arr=("${requires_arr[@]/libgbm1/lib64gbm1}")
  828. requires_arr=("${requires_arr[@]/libva2/lib64va2}")
  829. requires_arr=("${requires_arr[@]/libva-drm2/lib64va-drm2}")
  830. requires_arr=("${requires_arr[@]/libvulkan1/lib64vulkan1}")
  831. requires_arr=("${requires_arr[@]/mesa-vulkan-drivers/lib64dri-drivers}")
  832. requires_arr=("${requires_arr[@]/vulkan-icd/vulkan-loader}")
  833. requires_arr=("${requires_arr[@]/libwebkit2gtk-4.1-0/lib64webkit2gtk4.1}")
  834. recommends_arr=("${recommends_arr[@]/musepack-tools/mppenc}")
  835. for i in "${!recommends_arr[@]}"; do
  836. [[ ${recommends_arr[$i]} == "fdkaac" ]] && unset -v 'recommends_arr[i]'
  837. done
  838. ;;
  839. arch)
  840. # Set these manually for Arch since they are quite different
  841. requires_arr=('alsa-lib' 'ca-certificates' 'gtk3' 'gcc-libs' 'libx11' 'libxext'
  842. 'libxcb' 'libxau' 'libxdmcp' 'util-linux' 'mesa-libgl' 'webkit2gtk')
  843. recommends_arr=('mesa-libgl' 'nvidia-libgl' 'nvidia-utils' 'vulkan-intel'
  844. 'vulkan-radeon' 'vorbis-tools' 'musepack-tools')
  845. ;;
  846. *)
  847. echo "Skipping package translations for $ID"
  848. esac
  849. # Convert array to newline delim'd string (for heredoc)
  850. printf -v requires_arr "Requires: %s\n" "${requires_arr[@]}"
  851. printf -v recommends_arr "Recommends: %s\n" "${recommends_arr[@]}"
  852. # Strip last newline
  853. requires_arr="${requires_arr%?}"
  854. recommends_arr="${recommends_arr%?}"
  855. if ((COMPAT_SWITCH)); then
  856. # Strip minimum versions
  857. requires_arr=$(echo "$requires_arr" | awk -F" " 'NF == 4 {print $1 " " $2} NF != 4 {print $0}')
  858. fi
  859. }
  860. # @description Creates a SPEC file and builds the RPM from the source DEB using rpmbuild
  861. build_rpm() {
  862. debug "${FUNCNAME[0]}()"
  863. declare -n requires_arr="$1"
  864. declare -n recommends_arr="$2"
  865. local i rpmbuild_cmd stub
  866. local spec_file="$OUTPUT_DIR/SPECS/mediacenter$MC_MVERSION-$MC_VERSION-$MC_RELEASE-$BUILD_TARGET-$ARCH.spec"
  867. # skip rebuilding the rpm if it already exists
  868. debug "Checking for existing MC RPM: $MC_RPM"
  869. if [[ -f $MC_RPM && -f $spec_file ]]; then
  870. echo "Skipping build step: .spec and ouput RPM already exist"
  871. debug "RPM .spec file: $spec_file"
  872. debug "RPM: $MC_RPM"
  873. echo "Remove either to force rebuild"
  874. return 0
  875. fi
  876. # Exclude MC stub executable <= MC31
  877. if [[ $MC_MVERSION -le 31 ]]; then
  878. stub=""
  879. else
  880. stub="%{_bindir}/mc$MC_MVERSION"
  881. fi
  882. # Create spec file
  883. cat <<-EOF > "$spec_file"
  884. Name: mediacenter$MC_MVERSION
  885. Version: $MC_VERSION
  886. Release: $MC_RELEASE
  887. Summary: JRiver Media Center
  888. Group: Applications/Media
  889. Source0: $MC_SOURCE
  890. %define _rpmfilename %%{ARCH}/%%{NAME}-%%{version}-%%{release}.%%{ARCH}.rpm
  891. AutoReq: 0
  892. $requires_arr
  893. $recommends_arr
  894. Conflicts: MediaCenter
  895. Provides: mediacenter$MC_MVERSION
  896. License: Copyright 1998-$(date +%Y), JRiver, Inc. All rights reserved. Protected by U.S. patents #7076468 and #7062468
  897. URL: https://www.jriver.com/
  898. %define __provides_exclude_from ^%{_libdir}/jriver/.*/.*\\.so.*$
  899. %description
  900. Media Center is more than a world class player.
  901. %global __os_install_post %{nil}
  902. %prep
  903. %build
  904. %install
  905. dpkg -x %{S:0} %{buildroot}
  906. %post -p /sbin/ldconfig
  907. %postun -p /sbin/ldconfig
  908. %files
  909. %{_bindir}/mediacenter$MC_MVERSION
  910. $stub
  911. %{_libdir}/jriver
  912. %{_datadir}
  913. %exclude %{_datadir}/applications/media_center_packageinstaller_$MC_MVERSION.desktop
  914. /etc/security/limits.d/*
  915. EOF
  916. # Run rpmbuild
  917. echo "Building $MC_RPM, this may take some time"
  918. rpmbuild_cmd=(
  919. rpmbuild
  920. --define="_topdir $OUTPUT_DIR"
  921. --define="_libdir /usr/lib"
  922. --target="$ARCH"
  923. -bb
  924. "$spec_file"
  925. )
  926. execute "${rpmbuild_cmd[@]}" && [[ -f $MC_RPM ]]
  927. }
  928. # @description Creates the Arch PKGBUILD file for Media Center
  929. build_pkgbuild() {
  930. debug "${FUNCNAME[0]}()"
  931. declare -n requires_arr="$1"
  932. declare -n recommends_arr="$2"
  933. local pkgbuild_file="$OUTPUT_DIR/PKGBUILD/mediacenter.pkgbuild"
  934. [[ -d $OUTPUT_DIR/PKGBUILD ]] || execute mkdir -p "$OUTPUT_DIR/PKGBUILD"
  935. # The requires and recommends arrays are in RPM .spec format
  936. # Make them simple arrays for the PKGBUILD
  937. # Clean up requires_arr
  938. local cleaned_requires_arr=()
  939. for elem in "${requires_arr[@]}"; do
  940. elem="${elem#Requires: }"
  941. elem="${elem//$'\n'/ }"
  942. cleaned_requires_arr+=("$elem")
  943. done
  944. requires_arr=("${cleaned_requires_arr[@]}")
  945. # Clean up recommends_arr
  946. local cleaned_recommends_arr=()
  947. for elem in "${recommends_arr[@]}"; do
  948. elem="${elem#Recommends: }"
  949. elem="${elem//$'\n'/ }"
  950. cleaned_recommends_arr+=("$elem")
  951. done
  952. recommends_arr=("${cleaned_recommends_arr[@]}")
  953. # Create PKGBUILD file
  954. cat <<-EOF > "$pkgbuild_file"
  955. pkgname=mediacenter$MC_MVERSION
  956. pkgver=$MC_VERSION
  957. pkgrel=$MC_RELEASE
  958. pkgdesc="JRiver Media Center"
  959. arch=("$ARCH")
  960. url="https://www.jriver.com/"
  961. license=("custom")
  962. depends=(${requires_arr[@]})
  963. optdepends=(${recommends_arr[@]})
  964. source=("$MC_SOURCE")
  965. package() {
  966. cd "\$srcdir"
  967. bsdtar xf data.tar.xz -C "\$pkgdir"
  968. }
  969. EOF
  970. }
  971. # @description Installs Media Center via DEB package w/ optional compatability fixes
  972. install_mc_deb() {
  973. debug "${FUNCNAME[0]}()" "$@"
  974. if ((COMPAT_SWITCH)); then
  975. local extract_dir; extract_dir="$(mktemp -d)"
  976. pushd "$extract_dir" &>/dev/null || return
  977. command -v ar &>/dev/null || { install_package binutils || return 1; }
  978. execute ar x "$MC_DEB"
  979. execute tar xJf "control.tar.xz"
  980. # Remove minimum version specifiers from control file
  981. execute sed -i 's/ ([^)]*)//g' control
  982. # Remove libwebkit2gtk and their fantastic package versioning strategy
  983. execute sed -E -i 's/,[[:space:]]*libwebkit2gtk[^,]*(,|\?)?//g' control
  984. # TODO workaround for legacy ZorinOS
  985. if [[ $ID == "ubuntu" && ${VERSION_ID%.*} -le 16 ]] &&
  986. grep -q zorin /etc/os-release; then
  987. execute sed -i 's/libva2/libva1/g' control
  988. fi
  989. execute tar -cJf "control.tar.xz" "control" "postinst"
  990. declare -g MC_DEB="${MC_DEB/.deb/.compat.deb}"
  991. execute ar rcs "$MC_DEB" "debian-binary" "control.tar.xz" "data.tar.xz"
  992. popd &>/dev/null || return
  993. execute rm -rf "$extract_dir"
  994. fi
  995. # Add older repository for libwebkit2gtk-4.0-37, etc, on newer Debian/Ubuntu
  996. if add_temp_repo; then
  997. trap 'execute sudo rm -f "$TEMP_REPO_FILE"' EXIT
  998. else
  999. err "Failed to add temporary repository"
  1000. return 1
  1001. fi
  1002. execute "${PKG_UPDATE[@]}" || { err "Package update failed!"; return 1; }
  1003. # Copy the DEB to a temporary file so _apt can read it
  1004. debug "Creating temporary deb file owned by _apt"
  1005. local temp_deb
  1006. temp_deb=$(mktemp --suffix=.deb)
  1007. execute sudo cp "$MC_DEB" "$temp_deb"
  1008. id _apt &>/dev/null && execute sudo chown _apt "$temp_deb"
  1009. # Use --reinstall to make sure local package is installed over repo package
  1010. if ! install_package \
  1011. --no-install-check \
  1012. --no-gpg-check \
  1013. --allow-downgrades \
  1014. --reinstall \
  1015. "$temp_deb"; then
  1016. err "Local MC DEB installation failed"
  1017. if ask_ok "Remove source DEB and retry"; then
  1018. execute sudo rm -f "$MC_DEB" "$temp_deb"
  1019. exec "$SCRIPT_PATH" "$@" "--no-update"
  1020. fi
  1021. fi
  1022. # Unset the trap and remove temporary legacy repository
  1023. trap - EXIT
  1024. [[ -f $TEMP_REPO_FILE ]] && execute sudo rm -f "$TEMP_REPO_FILE"
  1025. execute sudo rm -f "$temp_deb"
  1026. return 0
  1027. }
  1028. # @description Installs MC via RPM package
  1029. install_mc_rpm() {
  1030. debug "${FUNCNAME[0]}()"
  1031. install_package --no-install-check --no-gpg-check --allow-downgrades --reinstall "$MC_RPM"
  1032. }
  1033. # @description Installs Media Center generically for unsupported OSes
  1034. install_mc_generic() {
  1035. debug "${FUNCNAME[0]}()"
  1036. local extract_dir
  1037. local -a raw_files
  1038. echo "Using generic installation method"
  1039. extract_dir="$(mktemp -d)"
  1040. pushd "$extract_dir" &>/dev/null || return
  1041. execute ar x "$MC_DEB"
  1042. execute tar xJf "control.tar.xz"
  1043. echo "You must install the following dependencies manually:"
  1044. grep -i "Depends:" control
  1045. readarray -t raw_files < <(tar xJvf data.tar.xz)
  1046. # Output to log file
  1047. for f in "${raw_files[@]/#./}"; do
  1048. echo "$f" >> "$SCRIPT_DIR/.uninstall"
  1049. done
  1050. # Manually install files
  1051. for f in "${raw_files[@]}"; do
  1052. execute sudo cp -a "$f" "${f/#./}"
  1053. done
  1054. popd &>/dev/null || return
  1055. execute rm -rf "$extract_dir"
  1056. return 0
  1057. }
  1058. # @description Installs MC via PKGBUILD
  1059. install_mc_arch() {
  1060. debug "${FUNCNAME[0]}()"
  1061. pushd "$OUTPUT_DIR/PKGBUILD" &>/dev/null || return
  1062. if ! execute makepkg \
  1063. --install \
  1064. --syncdeps \
  1065. --clean \
  1066. --cleanbuild \
  1067. --skipinteg \
  1068. --force \
  1069. --noconfirm \
  1070. -p mediacenter.pkgbuild; then
  1071. err "makepkg failed"; exit 1
  1072. fi
  1073. popd &>/dev/null || return
  1074. }
  1075. # @description Copy the RPM to createrepo-webroot and run createrepo as the createrepo-user
  1076. run_createrepo() {
  1077. debug "${FUNCNAME[0]}()"
  1078. install_package createrepo_c
  1079. # Ensure the webroot exists
  1080. if [[ ! -d $CREATEREPO_WEBROOT ]]; then
  1081. if ! execute sudo -u "$CREATEREPO_USER" mkdir -p "$CREATEREPO_WEBROOT"; then
  1082. if ! (execute sudo mkdir -p "$CREATEREPO_WEBROOT" ||
  1083. execute sudo chown -R "$CREATEREPO_USER:$CREATEREPO_USER" "$CREATEREPO_WEBROOT"); then
  1084. err "Could not create the createrepo-webroot path!"
  1085. err "Make sure that the webroot $CREATEREPO_WEBROOT is writable by user $CREATEREPO_USER"
  1086. err "Or change the repo ownership with --createrepo-user"
  1087. return 1
  1088. fi
  1089. fi
  1090. fi
  1091. # Copy built RPMs to webroot
  1092. if ! execute sudo cp -nf "$MC_RPM" "$CREATEREPO_WEBROOT" ||
  1093. ! execute sudo chown -R "$CREATEREPO_USER:$CREATEREPO_USER" "$CREATEREPO_WEBROOT"; then
  1094. err "Could not copy $MC_RPM to $CREATEREPO_WEBROOT"
  1095. return 1
  1096. fi
  1097. # Run createrepo
  1098. local -a cr_opts=(-q "$CREATEREPO_WEBROOT")
  1099. # [[ -d "$CREATEREPO_WEBROOT/repodata" ]] && cr_opts+=(--update) # TODO temporarily disabled for legacy createrepo
  1100. if ! execute sudo -u "$CREATEREPO_USER" createrepo "${cr_opts[@]}"; then
  1101. if ! (execute sudo createrepo "${cr_opts[@]}" && execute sudo chown -R "$CREATEREPO_USER:$CREATEREPO_USER" "$CREATEREPO_WEBROOT"); then
  1102. err "createrepo failed"
  1103. return 1
  1104. fi
  1105. fi
  1106. }
  1107. # @description Symlink certificates if they do not exist in default location
  1108. link_ssl_certs() {
  1109. debug "${FUNCNAME[0]}()"
  1110. local target_cert f
  1111. local mc_cert_link="$MC_ROOT/ca-certificates.crt"
  1112. local -a source_certs=(
  1113. "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem"
  1114. "/var/lib/ca-certificates/ca-bundle.pem"
  1115. "$MC_ROOT/local-ca-certificates.crt")
  1116. target_cert=$(readlink -f "$mc_cert_link")
  1117. [[ -f $target_cert ]] && return 0
  1118. for f in "${source_certs[@]}"; do
  1119. if [[ -f $f ]]; then
  1120. if execute sudo ln -fs "$f" "$mc_cert_link"; then
  1121. debug "Symlinked $mc_cert_link to $f"
  1122. return 0
  1123. fi
  1124. fi
  1125. done
  1126. err "Certificate symlinking failed"; return 1
  1127. }
  1128. # @description Restore the mjr license file from MJR_FILE or other common locations
  1129. restore_license() {
  1130. debug "${FUNCNAME[0]}()"
  1131. local newest f
  1132. local -a mjrfiles
  1133. # Glob mjr files from common directories
  1134. shopt -s nullglob
  1135. mjrfiles=(
  1136. "$SCRIPT_DIR"/*.mjr
  1137. "$OUTPUT_DIR"/*.mjr
  1138. "$HOME"/[dD]ownloads/*.mjr
  1139. "$HOME"/[dD]ocuments/*.mjr
  1140. )
  1141. shopt -u nullglob
  1142. if [[ ${#mjrfiles[@]} -gt 0 ]]; then
  1143. debug "mjrfiles=(${mjrfiles[*]})"
  1144. # Sort globbed files by time, newest first
  1145. newest=${mjrfiles[0]}
  1146. for f in "${mjrfiles[@]}"; do
  1147. if [[ -f $f && $f -nt $newest ]]; then
  1148. newest=$f
  1149. fi
  1150. done
  1151. debug "Latest mjrfile: $newest"
  1152. for f in "$MJR_FILE" "$newest"; do
  1153. if [[ -f $f ]]; then
  1154. if execute "mediacenter$MC_MVERSION" "/RestoreFromFile" "$f"; then
  1155. echo "Restored license from $f"
  1156. return 0
  1157. else
  1158. err "Failed to restore license from $f"
  1159. fi
  1160. fi
  1161. done
  1162. fi
  1163. }
  1164. # @description Opens ports using the system firewall tool
  1165. # @arg $1 string Service name
  1166. # @arg $2 array List of ports in firewall-cmd format
  1167. open_firewall() {
  1168. debug "${FUNCNAME[0]}()" "$@"
  1169. local service="$1"
  1170. shift
  1171. local -a f_ports=("$@") # for firewall-cmd
  1172. local u_ports="$*"
  1173. u_ports="${u_ports// /|}" # concatenate
  1174. u_ports="${u_ports//-/\:}" # for ufw
  1175. local port
  1176. if command -v firewall-cmd &>/dev/null; then
  1177. if ! sudo firewall-cmd --get-services | grep -q "$service"; then
  1178. execute sudo firewall-cmd --permanent "--new-service=$service"
  1179. execute sudo firewall-cmd --permanent "--service=$service" "--set-description=$service installed by installJRMC"
  1180. execute sudo firewall-cmd --permanent "--service=$service" "--set-short=$service"
  1181. for port in "${f_ports[@]}"; do
  1182. execute sudo firewall-cmd --permanent "--service=$service" "--add-port=$port"
  1183. done
  1184. execute sudo firewall-cmd --add-service "$service" --permanent
  1185. execute sudo firewall-cmd --reload
  1186. fi
  1187. elif command -v ufw &>/dev/null; then
  1188. sudo bash -c "cat <<-EOF > /etc/ufw/applications.d/$service
  1189. [$service]
  1190. title=$service
  1191. description=$service installed by installJRMC
  1192. ports=$u_ports
  1193. EOF"
  1194. execute sudo ufw app update "$service"
  1195. execute sudo ufw allow "$service"
  1196. else
  1197. return 1
  1198. fi
  1199. }
  1200. # @description Create the xvnc or x11vnc password file
  1201. # @arg $1 string Service type (xvnc, x11vnc)
  1202. set_vnc_pass() {
  1203. debug "${FUNCNAME[0]}()"
  1204. local vncpassfile="$HOME/.vnc/jrmc_passwd"
  1205. [[ -d ${vncpassfile%/*} ]] || execute mkdir -p "${vncpassfile%/*}"
  1206. if [[ -f $vncpassfile ]]; then
  1207. if [[ ! -v VNCPASS ]]; then
  1208. err "Refusing to overwrite existing $vncpassfile with an empty password"
  1209. err "Remove existing $vncpassfile or use --vncpass ''"
  1210. return 1
  1211. else
  1212. execute rm -f "$vncpassfile"
  1213. fi
  1214. fi
  1215. if [[ -v VNCPASS ]]; then
  1216. if [[ $1 == "xvnc" ]]; then
  1217. echo "$VNCPASS" | vncpasswd -f > "$vncpassfile"
  1218. elif [[ $1 == "x11vnc" ]]; then
  1219. execute x11vnc -storepasswd "$VNCPASS" "$vncpassfile"
  1220. fi
  1221. return
  1222. else
  1223. declare -gi NOVNCAUTH=1
  1224. fi
  1225. }
  1226. # @description Set display and port variables
  1227. set_display_vars() {
  1228. debug "${FUNCNAME[0]}()"
  1229. declare -g THIS_DISPLAY THIS_DISPLAY_NUM NEXT_DISPLAY
  1230. # Check USER_DISPLAY, else environment DISPLAY, else set to :0
  1231. THIS_DISPLAY="${USER_DISPLAY:-${DISPLAY:-:0}}"
  1232. THIS_DISPLAY_NUM="${THIS_DISPLAY#*:}" # strip prefix
  1233. THIS_DISPLAY_NUM="${THIS_DISPLAY_NUM%%.*}" # strip suffix
  1234. # Increment each time we run this
  1235. if ((NEXT_DISPLAY_NUM)); then
  1236. declare -g NEXT_DISPLAY_NUM=$((NEXT_DISPLAY_NUM + 1))
  1237. else
  1238. declare -g NEXT_DISPLAY_NUM=$((THIS_DISPLAY_NUM + 1))
  1239. fi
  1240. NEXT_DISPLAY=":$NEXT_DISPLAY_NUM"
  1241. }
  1242. # @description Create associated service variables based on service name
  1243. # @arg $1 string Service name
  1244. set_service_vars() {
  1245. debug "${FUNCNAME[0]}()" "$@"
  1246. declare -g SERVICE_NAME SERVICE_FNAME TIMER_NAME TIMER_FNAME
  1247. declare -g USER_STRING GRAPHICAL_TARGET
  1248. declare -ga RELOAD ENABLE DISABLE IS_ENABLED IS_ACTIVE
  1249. local -a systemctl_prefix
  1250. local service_name="$1"
  1251. local service_type="${SERVICE_TYPE:-${2:-system}}"
  1252. local service_dir="/usr/lib/systemd/$service_type"
  1253. if [[ $USER == "root" && $service_type == "user" ]]; then
  1254. err "Trying to install user service as root"
  1255. err "Use --service-type service and/or execute installJRMC as non-root user"
  1256. return 1
  1257. fi
  1258. if [[ $service_type == "system" ]]; then
  1259. systemctl_prefix=(sudo systemctl)
  1260. GRAPHICAL_TARGET="graphical.target"
  1261. elif [[ $service_type == "user" ]]; then
  1262. systemctl_prefix=(systemctl --user)
  1263. GRAPHICAL_TARGET="default.target"
  1264. fi
  1265. # systemctl commands
  1266. RELOAD=("${systemctl_prefix[@]}" daemon-reload)
  1267. ENABLE=("${systemctl_prefix[@]}" enable --now)
  1268. DISABLE=("${systemctl_prefix[@]}" disable --now)
  1269. IS_ENABLED=("${systemctl_prefix[@]}" is-enabled --quiet)
  1270. IS_ACTIVE=("${systemctl_prefix[@]}" is-active --quiet)
  1271. [[ -d $service_dir ]] || execute sudo mkdir -p "$service_dir"
  1272. if [[ $service_type == "system" && $USER != "root" ]]; then
  1273. SERVICE_FNAME="$service_dir/$service_name@.service"
  1274. TIMER_FNAME="$service_dir/$service_name@.timer"
  1275. SERVICE_NAME="$service_name@$USER.service"
  1276. TIMER_NAME="$service_name@$USER.timer"
  1277. USER_STRING="User=%I"
  1278. else
  1279. SERVICE_NAME="$service_name.service"
  1280. TIMER_NAME="$service_name.timer"
  1281. SERVICE_FNAME="$service_dir/$SERVICE_NAME"
  1282. TIMER_FNAME="$service_dir/${TIMER_NAME}"
  1283. USER_STRING=""
  1284. fi
  1285. }
  1286. # @section Services
  1287. # @description Starts and enables (at startup) a JRiver Media Center service
  1288. # @arg $1 string Passes arguments as startup options to /usr/bin/mediacenter??
  1289. service_jriver-mediacenter() {
  1290. debug "${FUNCNAME[0]}()"
  1291. set_service_vars "${FUNCNAME[0]##*_}" "user"
  1292. sudo bash -c "cat <<-EOF > $SERVICE_FNAME
  1293. [Unit]
  1294. Description=JRiver Media Center $MC_MVERSION
  1295. After=$GRAPHICAL_TARGET
  1296. [Service]
  1297. Type=simple
  1298. $USER_STRING
  1299. ExecStart=/usr/bin/mediacenter$MC_MVERSION $*
  1300. KillMode=none
  1301. ExecStop=/usr/bin/mc$MC_MVERSION /MCC 20007
  1302. Restart=always
  1303. RestartSec=10
  1304. TimeoutStopSec=30
  1305. [Install]
  1306. WantedBy=$GRAPHICAL_TARGET
  1307. EOF"
  1308. open_firewall "jriver-mediacenter" "52100-52200/tcp" "1900/udp"
  1309. "${RELOAD[@]}" &&
  1310. "${ENABLE[@]}" "$SERVICE_NAME"
  1311. }
  1312. # @description Starts and enables (at startup) a JRiver Media Server service
  1313. service_jriver-mediaserver() {
  1314. debug "${FUNCNAME[0]}()"
  1315. set_service_vars "${FUNCNAME[0]##*_}" "user"
  1316. service_jriver-mediacenter "/MediaServer"
  1317. }
  1318. # @description Starts and enables (at startup) JRiver Media Center in a new Xvnc session
  1319. # TODO https://github.com/TigerVNC/tigervnc/blob/master/unix/vncserver/HOWTO.md
  1320. service_jriver-xvnc() {
  1321. debug "${FUNCNAME[0]}()"
  1322. local -a start_cmd
  1323. set_service_vars "${FUNCNAME[0]##*_}" "system"
  1324. set_display_vars
  1325. declare -g PORT=$((NEXT_DISPLAY_NUM + 5900))
  1326. install_package tigervnc-server
  1327. set_vnc_pass xvnc
  1328. start_cmd=(
  1329. /usr/bin/vncserver "$NEXT_DISPLAY"
  1330. -geometry 1440x900
  1331. -alwaysshared
  1332. -autokill
  1333. -xstartup "/usr/bin/mediacenter$MC_MVERSION"
  1334. )
  1335. if ((NOVNCAUTH)); then
  1336. start_cmd+=(
  1337. -name "jriver$NEXT_DISPLAY"
  1338. -SecurityTypes None)
  1339. else
  1340. start_cmd+=(
  1341. -rfbauth "$HOME/.vnc/jrmc_passwd")
  1342. fi
  1343. sudo bash -c "cat <<-EOF > $SERVICE_FNAME
  1344. [Unit]
  1345. Description=Remote desktop service (VNC)
  1346. After=multi-user.target
  1347. [Service]
  1348. Type=forking
  1349. $USER_STRING
  1350. ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill $NEXT_DISPLAY &>/dev/null || :'
  1351. ExecStart=${start_cmd[*]}
  1352. ExecStop=/usr/bin/vncserver -kill $NEXT_DISPLAY
  1353. Restart=always
  1354. [Install]
  1355. WantedBy=multi-user.target
  1356. EOF"
  1357. "${RELOAD[@]}"
  1358. if ! "${ENABLE[@]}" "$SERVICE_NAME"; then
  1359. err "vncserver failed to start on DISPLAY $NEXT_DISPLAY"
  1360. # Allow to increment 10 times before breaking
  1361. max=$((THIS_DISPLAY_NUM + 10))
  1362. while [[ $NEXT_DISPLAY_NUM -lt $max ]]; do
  1363. echo "Incrementing DISPLAY and retrying"
  1364. service_jriver-xvnc && return
  1365. done
  1366. return 1
  1367. else
  1368. echo "Xvnc running on localhost:$PORT"
  1369. open_firewall "jriver-xvnc" "$PORT/tcp"
  1370. open_firewall "jriver-mediacenter" "52100-52200/tcp" "1900/udp"
  1371. return 0
  1372. fi
  1373. }
  1374. # @description Starts and enables (at startup) x11vnc screen sharing for the local desktop
  1375. service_jriver-x11vnc() {
  1376. debug "${FUNCNAME[0]}()"
  1377. local -a start_cmd
  1378. set_service_vars "${FUNCNAME[0]##*_}" "user"
  1379. set_display_vars
  1380. declare -g PORT=$((THIS_DISPLAY_NUM + 5900))
  1381. install_package x11vnc
  1382. set_vnc_pass x11vnc
  1383. # If .Xauthority file is missing, generate a dummy for x11vnc -auth guess
  1384. if [[ ! -f "$HOME/.Xauthority" ]]; then
  1385. [[ $XDG_SESSION_TYPE == "wayland" ]] &&
  1386. ask_ok "Unsupported Wayland session detected for x11vnc, continue?" || return 1
  1387. debug "Generating $HOME/.Xauthority"
  1388. execute touch "$HOME/.Xauthority"
  1389. execute chmod 644 "$HOME/.Xauthority"
  1390. xauth generate "$DISPLAY" . trusted
  1391. xauth add "$HOST$DISPLAY" . "$(xxd -l 16 -p /dev/urandom)"
  1392. fi
  1393. start_cmd=(
  1394. /usr/bin/x11vnc
  1395. -display "$DISPLAY"
  1396. -noscr
  1397. -auth guess
  1398. -forever
  1399. -bg
  1400. )
  1401. if ((NOVNCAUTH)); then
  1402. start_cmd+=(-nopw)
  1403. else
  1404. start_cmd+=(-rfbauth "$HOME/.vnc/jrmc_passwd")
  1405. fi
  1406. sudo bash -c "cat <<-EOF > $SERVICE_FNAME
  1407. [Unit]
  1408. Description=x11vnc
  1409. After=$GRAPHICAL_TARGET
  1410. [Service]
  1411. $USER_STRING
  1412. Type=forking
  1413. Environment=DISPLAY=$DISPLAY
  1414. ExecStart=${start_cmd[*]}
  1415. Restart=always
  1416. RestartSec=10
  1417. [Install]
  1418. WantedBy=$GRAPHICAL_TARGET
  1419. EOF"
  1420. open_firewall "jriver-x11vnc" "$PORT/tcp"
  1421. "${RELOAD[@]}" &&
  1422. "${ENABLE[@]}" "$SERVICE_NAME" &&
  1423. echo "x11vnc running on localhost:$PORT"
  1424. }
  1425. # @description Starts and enables (at startup) an hourly service to build the latest version of
  1426. # JRiver Media Center RPM from the source DEB and create/update an RPM repository
  1427. service_jriver-createrepo() {
  1428. debug "${FUNCNAME[0]}()"
  1429. if [[ $CREATEREPO_USER != "$USER" ]]; then
  1430. USER="root" set_service_vars "${FUNCNAME[0]##*_}" "system"
  1431. else
  1432. set_service_vars "${FUNCNAME[0]##*_}" "system"
  1433. fi
  1434. sudo bash -c "cat <<-EOF > $SERVICE_FNAME
  1435. [Unit]
  1436. Description=Builds JRiver Media Center RPM, moves it to the repo dir, and runs createrepo
  1437. [Service]
  1438. $USER_STRING
  1439. ExecStart=$SCRIPT_DIR/installJRMC --outputdir=$OUTPUT_DIR --createrepo=$CREATEREPO_TARGET \
  1440. --createrepo-webroot=$CREATEREPO_WEBROOT --createrepo-user=$CREATEREPO_USER --mcrepo=$MC_REPO --yes --no-update
  1441. [Install]
  1442. WantedBy=multi-user.target
  1443. EOF"
  1444. sudo bash -c "cat <<-EOF > $TIMER_FNAME
  1445. [Unit]
  1446. Description=Run JRiver MC rpmbuild hourly
  1447. [Timer]
  1448. OnCalendar=hourly
  1449. Persistent=true
  1450. [Install]
  1451. WantedBy=timers.target
  1452. EOF"
  1453. "${RELOAD[@]}" &&
  1454. "${ENABLE[@]}" "$TIMER_NAME"
  1455. }
  1456. # @description Detects if MC is installed on btrfs and disables CoW
  1457. fix_dotjriver_permissions() {
  1458. debug "${FUNCNAME[0]}()"
  1459. # Ensure the user owns their .jriver directory
  1460. if [[ -d "$HOME/.jriver" ]]; then
  1461. local owner
  1462. owner=$(stat -c '%U' "$HOME/.jriver")
  1463. if [[ "$owner" != "$USER" ]]; then
  1464. ask_ok "$USER does not currently own $HOME/.jriver, attempt fix?" &&
  1465. execute sudo chown -R "$USER:$USER" "$HOME/.jriver"
  1466. fi
  1467. fi
  1468. }
  1469. # @description Completely uninstalls MC, services, and firewall rules
  1470. uninstall() {
  1471. debug "${FUNCNAME[0]}()"
  1472. local service type unit f
  1473. if ! ask_ok "Do you really want to uninstall JRiver Media Center?"; then
  1474. echo "Uninstall cancelled"
  1475. return 1
  1476. fi
  1477. echo "Stopping and removing all Media Center services"
  1478. for service in $(compgen -A "function" "service"); do
  1479. service="${service##service_}"
  1480. for type in user system; do
  1481. set_service_vars "$service" "$type";
  1482. for unit in "$SERVICE_NAME" "$TIMER_NAME"; do
  1483. if "${IS_ACTIVE[@]}" "$unit" ||
  1484. "${IS_ENABLED[@]}" "$unit" &>/dev/null; then
  1485. "${DISABLE[@]}" "$unit"
  1486. fi
  1487. done
  1488. for f in "$SERVICE_FNAME" "$TIMER_FNAME"; do
  1489. [[ -f $f ]] && execute sudo rm -f "$f"
  1490. done
  1491. "${RELOAD[@]}"
  1492. done
  1493. done
  1494. # Remove the repository files
  1495. for file in "/etc/yum.repos.d/jriver.repo" /etc/apt/sources.list.d/{jriver,mediacenter}*.{list,sources}; do
  1496. if [[ -e $file ]]; then
  1497. echo "Removing repository file: $file"
  1498. execute sudo rm -f "$file"
  1499. fi
  1500. done
  1501. [[ $ID == "suse" ]] && execute sudo zypper --non-interactive removerepo jriver
  1502. echo "Removing firewall rules"
  1503. for service in jriver-mediacenter jriver-xvnc jriver-x11vnc; do
  1504. if command -v firewall-cmd &>/dev/null; then
  1505. execute sudo firewall-cmd --permanent --remove-service=$service
  1506. execute sudo firewall-cmd --permanent --delete-service=$service
  1507. execute sudo firewall-cmd --reload
  1508. elif command -v ufw &>/dev/null; then
  1509. execute sudo ufw delete allow $service
  1510. [[ -f /etc/ufw/applications.d/$service ]] &&
  1511. execute sudo rm -f /etc/ufw/applications.d/$service
  1512. fi
  1513. done
  1514. echo "Uninstalling the JRiver Media Center package"
  1515. if "${PKG_REMOVE[@]}" "${MC_PKG%%=*}"; then # strip version specifier
  1516. echo "Successfully uninstalled the ${MC_PKG%%=*} package"
  1517. elif [[ $? -eq 100 ]]; then
  1518. err "JRiver Media Center package '${MC_PKG%%=*}' is not present and was not uninstalled"
  1519. else
  1520. err "Could not remove Media Center package"
  1521. fi
  1522. local keyfile="/usr/share/keyrings/jriver-com-archive-keyring.gpg"
  1523. if [[ -f $keyfile ]]; then
  1524. echo "Removing the JRiver Media Center GPG key"
  1525. execute sudo rm -f "$keyfile"
  1526. fi
  1527. if [[ -f $SCRIPT_DIR/.uninstall ]]; then
  1528. echo "Removing files from .uninstall log"
  1529. while read -r p; do
  1530. [[ -d $p ]] && execute sudo rm -rf "$p"
  1531. done < "$SCRIPT_DIR/.uninstall"
  1532. mv "$SCRIPT_DIR/.uninstall" "$SCRIPT_DIR/.uninstall.bk"
  1533. fi
  1534. if [[ -d $OUTPUT_DIR ]]; then
  1535. if ask_ok "Remove installJRMC output directory $OUTPUT_DIR?"; then
  1536. execute sudo rm -rf "$OUTPUT_DIR"
  1537. fi
  1538. fi
  1539. if [[ -d $MC_ROOT ]]; then
  1540. if ask_ok "Remove MC installation directory $MC_ROOT?"; then
  1541. execute sudo rm -rf "$MC_ROOT"
  1542. fi
  1543. fi
  1544. if [[ -d $HOME/.jriver ]]; then
  1545. if ask_ok "Backup and reset your MC library?"; then
  1546. execute mv "$HOME/.jriver" "$HOME/.jriver.bk"
  1547. echo "Your MC library has been reset and backed up to $HOME/.jriver.bk"
  1548. echo "To restore your MC library: mv $HOME/.jriver.bk $HOME/.jriver"
  1549. return
  1550. fi
  1551. echo "To reset and backup your MC library: mv $HOME/.jriver $HOME/.jriver.bk"
  1552. echo "To remove your MC library: rm -rf $HOME/.jriver"
  1553. fi
  1554. }
  1555. # @description Checks for installJRMC update and re-executes, if necessary
  1556. update() {
  1557. debug "${FUNCNAME[0]}()" "$@"
  1558. debug "Checking for installJRMC update"
  1559. # Extract and normalize version from a script
  1560. extract_version() {
  1561. local version_line
  1562. version_line=$(grep -m 1 'SCRIPT_VERSION=' "$1")
  1563. version_line=${version_line#*=}
  1564. version_line=${version_line#\"}
  1565. version_line=${version_line%-dev\"}
  1566. version_line=${version_line%\"}
  1567. echo "$version_line"
  1568. }
  1569. # Compare semantic version strings
  1570. version_greater() {
  1571. [[ "$(echo -e "$1\n$2" | sort -V | head -n 1)" != "$1" ]]
  1572. }
  1573. # Check if we're in a git directory and if it's the installJRMC repository
  1574. if git -C "$SCRIPT_DIR" rev-parse --is-inside-work-tree &>/dev/null &&
  1575. [[ "$(git -C "$SCRIPT_DIR" config --get remote.origin.url)" =~ installJRMC|installjrmc ]]; then
  1576. # Get the current commit hash
  1577. local before_pull_hash
  1578. before_pull_hash=$(git -C "$SCRIPT_DIR" rev-parse HEAD)
  1579. # Stash local changes before pull
  1580. execute git -C "$SCRIPT_DIR" stash push --quiet
  1581. # Pull latest changes
  1582. execute git -C "$SCRIPT_DIR" pull --quiet
  1583. # Restore local changes
  1584. execute git -C "$SCRIPT_DIR" stash pop --quiet
  1585. debug "Current commit hash: $before_pull_hash"
  1586. debug "New commit hash: $(git -C "$SCRIPT_DIR" rev-parse HEAD)"
  1587. # If the commit hash has changed, an update occurred
  1588. if [[ "$before_pull_hash" != $(git -C "$SCRIPT_DIR" rev-parse HEAD) ]]; then
  1589. echo "Detected installJRMC update, restarting"
  1590. exec "$SCRIPT_PATH" "$@" "--no-update"
  1591. fi
  1592. else
  1593. debug "Not in the installJRMC repository, checking for installJRMC update via webscrape."
  1594. local tmp
  1595. tmp=$(mktemp) || { err "Failed to create temporary file."; return 1; }
  1596. # Acquire the latest version of the script
  1597. if ! download "$SCRIPT_URL" "$tmp"; then
  1598. err "Failed to download the latest script."
  1599. execute rm -f "$tmp"
  1600. return 1
  1601. fi
  1602. # Extract the latest version number
  1603. local remote_version
  1604. remote_version=$(extract_version "$tmp")
  1605. if [[ -z "$remote_version" ]]; then
  1606. err "Failed to extract version from the downloaded script."
  1607. execute rm -f "$tmp"
  1608. return 1
  1609. fi
  1610. # Compare versions and update if the remote version is greater
  1611. if version_greater "$remote_version" "$SCRIPT_VERSION"; then
  1612. execute mv "$tmp" "$SCRIPT_PATH" || { err "Failed to replace the script"; execute rm -f "$tmp"; return 1; }
  1613. execute chmod +x "$SCRIPT_PATH" || { err "Failed to make the script executable"; return 1; }
  1614. execute rm -f "$tmp"
  1615. echo "Detected installJRMC update, restarting"
  1616. exec "$SCRIPT_PATH" "$@" "--no-update"
  1617. else
  1618. debug "Current installJRMC $SCRIPT_VERSION is the latest version"
  1619. execute rm -f "$tmp"
  1620. return 0
  1621. fi
  1622. fi
  1623. }
  1624. # @description installJRMC main function
  1625. main() {
  1626. debug "${FUNCNAME[0]}()" "$@" # prints function name and arguments
  1627. echo "Starting installJRMC $SCRIPT_VERSION"
  1628. if ((DEBUG)); then
  1629. echo "Debugging on"
  1630. else
  1631. echo "To enable debugging output, use --debug or -d"
  1632. fi
  1633. # Parse input, set default/host variables, and MC version
  1634. init "$@"
  1635. ((UNINSTALL_SWITCH)) && uninstall
  1636. # Exit now if only --uninstall is passed
  1637. if ! (( BUILD_SWITCH || CREATEREPO_SWITCH || REPO_INSTALL_SWITCH || LOCAL_INSTALL_SWITCH ||
  1638. CONTAINER_INSTALL_SWITCH || SNAP_INSTALL_SWITCH || APPIMAGE_INSTALL_SWITCH )) &&
  1639. [[ ${#SERVICES[@]} -eq 0 && ${#CONTAINERS[@]} -eq 0 ]]; then
  1640. exit 0
  1641. fi
  1642. install_external_repos
  1643. if ((REPO_INSTALL_SWITCH)); then
  1644. echo "Installing JRiver Media Center from remote repository"
  1645. if install_mc_repo; then
  1646. echo "JRiver Media Center installed successfully from remote repository"
  1647. install_mesa_freeworld
  1648. link_ssl_certs
  1649. restore_license
  1650. open_firewall "jriver-mediacenter" "52100-52200/tcp" "1900/udp"
  1651. else
  1652. err "JRiver Media Center installation from remote repository failed"
  1653. return 1
  1654. fi
  1655. fi
  1656. if ((BUILD_SWITCH)); then
  1657. acquire_deb || { err "Could not download Media Center DEB package"; return 1; }
  1658. # Convert the source DEB dependencies to various distro-specific package naming
  1659. install_package dpkg
  1660. translate_packages "$MC_DEB" requires recommends
  1661. if [[ $BUILD_TARGET =~ centos|fedora|suse|mandriva || $CREATEREPO_TARGET =~ centos|fedora|suse|mandriva ]]; then
  1662. install_package rpm-build
  1663. [[ -d $OUTPUT_DIR/SPECS ]] || execute mkdir -p "$OUTPUT_DIR/SPECS"
  1664. if build_rpm requires recommends; then
  1665. echo "RPM package built successfully"
  1666. else
  1667. err "Failed to build RPM package"
  1668. # On build failure, remove the source DEB in case it is corrupted
  1669. if [[ -f $MC_DEB ]]; then
  1670. echo "Removing source DEB"
  1671. if ! execute rm -f "$MC_DEB"; then
  1672. execute sudo rm -f "$MC_DEB"
  1673. fi
  1674. fi
  1675. return 1
  1676. fi
  1677. elif [[ $BUILD_TARGET =~ arch ]]; then
  1678. if build_pkgbuild requires recommends; then
  1679. echo "Successfully generated Arch PKGBUILD"
  1680. fi
  1681. fi
  1682. fi
  1683. if ((LOCAL_INSTALL_SWITCH)); then
  1684. echo "Installing JRiver Media Center from local package"
  1685. if PKG_INSTALL_LOCAL "$@"; then
  1686. echo "JRiver Media Center installed successfully from local package"
  1687. else
  1688. err "JRiver Media Center local package installation failed"
  1689. return 1
  1690. fi
  1691. install_mesa_freeworld
  1692. link_ssl_certs
  1693. restore_license
  1694. open_firewall "jriver-mediacenter" "52100-52200/tcp" "1900/udp"
  1695. fi
  1696. if ((CREATEREPO_SWITCH)); then
  1697. if run_createrepo; then
  1698. echo "Successfully updated repo"
  1699. else
  1700. err "Repo creation failed"
  1701. fi
  1702. fi
  1703. if [[ ${#SERVICES[@]} -gt 0 ]]; then
  1704. declare service
  1705. for service in "${SERVICES[@]}"; do
  1706. if ! "service_$service"; then
  1707. if [[ $? -eq 127 ]]; then
  1708. err "Service $service does not exist, check service name"
  1709. else
  1710. err "Failed to create $service service"
  1711. fi
  1712. else
  1713. echo "Started and enabled $service service"
  1714. fi
  1715. done
  1716. unset service
  1717. fi
  1718. }
  1719. # @section Helper functions
  1720. debug() { ((DEBUG)) && echo "Debug: $*"; }
  1721. err() { echo "Error: $*" >&2; }
  1722. ask_ok() {
  1723. local response
  1724. ((YES_SWITCH)) && return 0
  1725. read -n 1 -r -p "$* [y/N]: " response
  1726. echo
  1727. [[ ${response,,} == y ]]
  1728. }
  1729. execute() {
  1730. if debug "$*"; then
  1731. "$@"
  1732. else
  1733. "$@" &>/dev/null
  1734. fi
  1735. }
  1736. fix_permissions() {
  1737. local dir="$1"
  1738. local user="$2"
  1739. local owner
  1740. [[ -d "$dir" ]] || return 1
  1741. owner=$(stat -c '%U' "$dir")
  1742. if [[ "$owner" != "$user" ]]; then
  1743. echo "Directory $dir is owned by $owner, not $user"
  1744. if ask_ok "Change ownership of $dir to $user?"; then
  1745. execute sudo chown -R "$user:$user" "$dir"
  1746. fi
  1747. fi
  1748. }
  1749. download() {
  1750. debug "${FUNCNAME[0]}()" "$@"
  1751. local url="$1"
  1752. local output="${2:-}"
  1753. local -a cmd
  1754. if command -v curl &>/dev/null || install_package --silent curl; then
  1755. cmd=(curl --silent --fail --location)
  1756. if [[ -n "$output" ]]; then
  1757. cmd+=(--output "$output")
  1758. else
  1759. cmd+=(--remote-name)
  1760. fi
  1761. elif command -v wget &>/dev/null || install_package --silent wget; then
  1762. cmd=(wget --quiet)
  1763. [[ -n "$output" ]] && cmd+=("--output-document=$output")
  1764. else
  1765. err "Unable to install wget or curl"
  1766. return 1
  1767. fi
  1768. debug "${cmd[@]}" "$url"
  1769. "${cmd[@]}" "$url"
  1770. }
  1771. create_mc_apt_container() {
  1772. debug "${FUNCNAME[0]}()" "$@"
  1773. declare -g CNT
  1774. local -a cmds=("$@")
  1775. { command -v buildah &>/dev/null || install_package buildah &>/dev/null; } &&
  1776. CNT=$(sudo buildah from --quiet alpine:edge) &&
  1777. sudo buildah run "$CNT" -- sh -c '
  1778. apk add --no-cache apt curl gnupg &>/dev/null
  1779. curl -fsSL https://dist.jriver.com/mediacenter@jriver.com.gpg.key | gpg --dearmor -o /usr/share/keyrings/jriver-com-archive-keyring.gpg &>/dev/null
  1780. cat <<-EOF > /etc/apt/sources.list.d/jriver.sources
  1781. Types: deb
  1782. URIs: https://dist.jriver.com/latest/mediacenter/
  1783. Signed-By: /usr/share/keyrings/jriver-com-archive-keyring.gpg
  1784. Suites: '"$MC_REPO"'
  1785. Components: main
  1786. Architectures: '"$MC_ARCH"'
  1787. EOF
  1788. apt-get update &>/dev/null' &&
  1789. # If user passes command strings run them in the container
  1790. for cmd in "${cmds[@]}"; do
  1791. sudo buildah run "$CNT" -- sh -c "$cmd" || { err "$cmd failed"; return 1; }
  1792. done
  1793. }
  1794. add_temp_repo() {
  1795. debug "${FUNCNAME[0]}()"
  1796. local repo_name repo_uri repo_suite repo_key
  1797. if [[ "$ID" == "ubuntu" ]]; then
  1798. local major_version="${VERSION_ID%%.*}"
  1799. local minor_version="${VERSION_ID##*.}"
  1800. minor_version="${minor_version#0}" # strip leading zero
  1801. if [[ $major_version -gt 24 || ( $major_version -eq 24 && minor_version -ge 4 ) ]]; then
  1802. echo "Temporarily adding jammy repository for libwebkit2gtk-4.0-37, etc."
  1803. repo_name="ubuntu-jammy-temp"
  1804. repo_uri="https://archive.ubuntu.com/ubuntu"
  1805. repo_suite="jammy"
  1806. repo_key="/usr/share/keyrings/ubuntu-archive-keyring.gpg"
  1807. else
  1808. return 0
  1809. fi
  1810. elif [[ "$ID" == "debian" ]]; then
  1811. local major_version="${VERSION_ID%%.*}"
  1812. if [[ $major_version -ge 13 ]]; then
  1813. echo "Temporarily adding bookworm repository for libwebkit2gtk-4.0-37, etc."
  1814. repo_name="debian-bookworm-temp"
  1815. repo_uri="https://deb.debian.org/debian"
  1816. repo_suite="bookworm"
  1817. repo_key="/usr/share/keyrings/debian-archive-keyring.gpg"
  1818. else
  1819. return 0
  1820. fi
  1821. else
  1822. # For other distributions, do nothing.
  1823. return 0
  1824. fi
  1825. declare -g TEMP_REPO_FILE="/etc/apt/sources.list.d/${repo_name}.sources"
  1826. echo "Creating temporary repository file $TEMP_REPO_FILE for $repo_suite"
  1827. sudo bash -c "cat <<-EOF > $TEMP_REPO_FILE
  1828. Types: deb
  1829. URIs: $repo_uri
  1830. Suites: $repo_suite
  1831. Components: main
  1832. Architectures: $MC_ARCH
  1833. Signed-By: $repo_key
  1834. EOF"
  1835. }
  1836. # Roughly turn debugging on for pre-init
  1837. # Reset and reparse in parse_input() with getopt
  1838. [[ " $* " =~ ( --debug | -d ) ]] && DEBUG=1
  1839. main "$@"
  1840. exit