installJRMC 55 KB

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