installJRMC 57 KB

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