installJRMC 58 KB

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