installJRMC 58 KB

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