installJRMC 64 KB

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