installJRMC 55 KB

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