installJRMC 55 KB

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