installJRMC 63 KB

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