installJRMC 70 KB

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