installJRMC 53 KB

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