installJRMC 59 KB

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