installJRMC 58 KB

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