installJRMC 62 KB

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