installJRMC 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638
  1. #!/usr/bin/env bash
  2. # This script will install JRiver Media Center and associated services on most major distros
  3. #
  4. # Copyright (c) 2021-2022 Bryan C. Roessler
  5. # This software is released under the Apache License.
  6. # https://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # See installJRMC --help or printHelp() below
  9. #
  10. # TODO (v2)
  11. # 1. Raspberry Pi OS support
  12. # 2. Interactive installation (ncurses?)
  13. # 3. Additional containerization (createrepo and rpmbuild)
  14. # 4. Tests
  15. #
  16. # BUGS
  17. # 1. No createrepo on Mint
  18. shopt -s extglob
  19. declare -g SCRIPTVERSION="1.0b14"
  20. declare -g OUTPUTDIR="$PWD/output"
  21. declare -g CREATEREPO_WEBROOT="/var/www/jriver"
  22. declare -g USER="${SUDO_USER:-$USER}"
  23. declare -g HOME; HOME=$(getent passwd "$USER" | cut -d: -f6)
  24. printHelp() {
  25. debug "Running: ${FUNCNAME[0]}"
  26. cat <<- 'EOF'
  27. USAGE:
  28. installJRMC [[OPTION] [VALUE]]...
  29. If no options (besides -d) are provided, the script will default to '--install repo'.
  30. OPTIONS
  31. --install, -i repo|local
  32. repo: Install MC from repository, updates are handled by the system package manager
  33. local: Build and install MC package locally
  34. --build[=suse|fedora|centos]
  35. Build RPM from source DEB but do not install
  36. Optionally, specify a target distro for cross-building (ex. --build=suse, note the '=')
  37. --compat
  38. Build/install MC locally without minimum library specifiers
  39. --mcversion VERSION
  40. Specify the MC version, ex. "29.0.18" (Default: latest)
  41. --outputdir PATH
  42. Generate rpmbuild output in this directory (Default: ./output)
  43. --restorefile RESTOREFILE
  44. Restore file location for automatic license registration (Default: skip registration)
  45. --betapass PASSWORD
  46. Enter beta team password for access to beta builds
  47. --service, -s SERVICE
  48. See SERVICES section below for a list of possible services to install
  49. --service-type user|system
  50. Starts services at boot (system) or at user login (user) (Default: boot)
  51. --container, -c CONTAINER (TODO: Under construction)
  52. See CONTAINERS section below for a list of possible services to install
  53. --createrepo[=suse|fedora|centos]
  54. Build rpm, copy to webroot, and run createrepo. Use in conjunction with --build=TARGET for crossbuilding repos
  55. Optionally, specify a target distro for non-native repo (ex. --createrepo=fedora, note the '=')
  56. --createrepo-webroot PATH
  57. Specify the webroot directory to install the repo (Default: /var/www/jriver)
  58. --createrepo-user USER
  59. Specify the web server user if it differs from $USER
  60. --version, -v
  61. Print this script version and exit
  62. --debug, -d
  63. Print debug output
  64. --help, -h
  65. Print help dialog and exit
  66. --uninstall, -u
  67. Uninstall JRiver MC, remove services, and remove firewall rules (does not remove library files)
  68. SERVICES
  69. jriver-mediaserver
  70. Enable and start a mediaserver systemd service (requires an existing X server) (user)
  71. jriver-mediacenter
  72. Enable and start a mediacenter systemd service (requires an existing X server) (user)
  73. jriver-x11vnc
  74. Enable and start x11vnc for the local desktop (requires an existing X server) (user)
  75. Usually combined with jriver-mediaserver or jriver-mediacenter services
  76. --vncpass and --display are optional (see below)
  77. jriver-xvnc
  78. Enable and start a new Xvnc session running JRiver Media Center (system)
  79. --vncpass PASSWORD
  80. Set vnc password for x11vnc/Xvnc access. If no password is set, the script
  81. will either use existing password stored in $HOME/.vnc/jrmc_passwd or use no password
  82. --display DISPLAY
  83. Display to start x11vnc/Xvnc (Default: The current display (x11vnc) or the
  84. current display incremented by 1 (Xvnc))
  85. jriver-createrepo
  86. Install hourly service to build latest MC RPM and run createrepo (system)
  87. CONTAINERS (TODO: Under construction)
  88. mediacenter-xvnc
  89. createrepo
  90. EOF
  91. }
  92. # Helpers
  93. debug() { (( DEBUG )) && [[ $# -gt 0 ]] && echo "Debug: $*"; }
  94. err() { echo "Error: $*" >&2; }
  95. askOk() {
  96. declare response
  97. read -r -p "$* [y/N]: " response
  98. [[ "${response,,}" =~ ^(yes|y)$ ]]
  99. return
  100. }
  101. #######################################
  102. # Parses user input and sets sensible defaults
  103. #######################################
  104. parseInput() {
  105. debug "Running: ${FUNCNAME[0]}"
  106. declare -g BUILD_SWITCH REPO_INSTALL_SWITCH COMPAT_SWITCH LOCAL_INSTALL_SWITCH CREATEREPO_SWITCH UNINSTALL_SWITCH
  107. declare -g OUTPUTDIR RESTOREFILE BETAPASS SERVICE_TYPE VNCPASS USER_DISPLAY CREATEREPO_WEBROOT
  108. declare -ga SERVICES CONTAINERS
  109. declare long_opts short_opts
  110. # Allow some environment variables to override and set sane fallbacks
  111. declare -g TARGET=${TARGET:-$ID}
  112. declare -g CREATEREPO_USER="${CREATEREPO_USER:-$USER}"
  113. if [[ $# -eq 0 ]] || [[ $# -eq 1 && "$1" =~ ^(--debug|-d)$ ]]; then
  114. REPO_INSTALL_SWITCH=1
  115. elif [[ $# -eq 1 && "$1" =~ ^(--compat)$ ]]; then
  116. BUILD_SWITCH=1
  117. LOCAL_INSTALL_SWITCH=1
  118. fi
  119. long_opts="install:,build::,outputdir:,mcversion:,restorefile:,betapass:,"
  120. long_opts+="service-type:,service:,version,debug,help,uninstall,createrepo::,"
  121. long_opts+="createrepo-webroot:,createrepo-user:,vncpass:,display:,container:,tests,compat"
  122. short_opts="+i:vb::dhus:c:"
  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)
  131. BUILD_SWITCH=1
  132. LOCAL_INSTALL_SWITCH=1
  133. ;;
  134. repo)
  135. REPO_INSTALL_SWITCH=1
  136. ;;
  137. esac
  138. ;;
  139. --build|-b)
  140. BUILD_SWITCH=1
  141. shift && TARGET="$1"
  142. ;;
  143. --outputdir)
  144. shift && OUTPUTDIR="$1"
  145. ;;
  146. --mcversion)
  147. shift && MCVERSION="$1"
  148. ;;
  149. --restorefile)
  150. shift && RESTOREFILE="$1"
  151. ;;
  152. --betapass)
  153. shift && BETAPASS="$1"
  154. ;;
  155. --service-type)
  156. shift && SERVICE_TYPE="$1"
  157. ;;
  158. --service|-s)
  159. shift && SERVICES+=("$1")
  160. ;;
  161. --createrepo)
  162. BUILD_SWITCH=1
  163. CREATEREPO_SWITCH=1
  164. shift && TARGET="$1"
  165. ;;
  166. --createrepo-webroot)
  167. shift && CREATEREPO_WEBROOT="$1"
  168. ;;
  169. --createrepo-user)
  170. shift && CREATEREPO_USER="$1"
  171. ;;
  172. --vncpass)
  173. shift && VNCPASS="$1"
  174. ;;
  175. --display)
  176. shift && USER_DISPLAY="$1"
  177. ;;
  178. --compat)
  179. COMPAT_SWITCH=1
  180. ;;
  181. --container|-c)
  182. shift && CONTAINERS+=("$1")
  183. ;;
  184. --version|-v)
  185. echo "Version: $SCRIPTVERSION"
  186. exit 0
  187. ;;
  188. --debug|-d)
  189. echo "Debugging on"
  190. echo "installJRMC version: $SCRIPTVERSION"
  191. DEBUG=1
  192. ;;
  193. --help|-h)
  194. printHelp && exit $?
  195. ;;
  196. --uninstall|-u)
  197. UNINSTALL_SWITCH=1
  198. ;;
  199. --tests)
  200. echo "Running tests, all other options are skipped"
  201. tests
  202. ;;
  203. --)
  204. shift
  205. break
  206. ;;
  207. esac
  208. shift
  209. done
  210. else
  211. err "Incorrect options provided"
  212. printHelp && exit 1
  213. fi
  214. (( DEBUG )) || echo "Use --debug for verbose output"
  215. }
  216. #######################################
  217. # Uses several methods to determine the latest JRiver MC version
  218. # TODO but how to determine build distro `$BASE=buster`?
  219. #######################################
  220. getVersion() {
  221. debug "Running: ${FUNCNAME[0]}"
  222. declare -g MCVERSION VERSION_SOURCE MVERSION MCPKG BASE="buster" # For container method
  223. #declare -g BASE_NEXT="bullseye" # TODO possibly use for fallback to smooth upgrades
  224. declare boardurl="https://yabb.jriver.com/interact/index.php/board,74.0.html" # MC28 (Buster), for fallback webscrape
  225. # User input
  226. if [[ -v MCVERSION && "$MCVERSION" =~ ([0-9]+.[0-9]+.[0-9]+) ]]; then
  227. VERSION_SOURCE="user input"
  228. # Containerized package manager
  229. elif installPackage --silent buildah &&
  230. cnt=$(buildah from debian:$BASE) &>/dev/null &&
  231. buildah run "$cnt" -- bash -c \
  232. "echo 'deb [trusted=no arch=amd64,i386,armhf,arm64] http://dist.jriver.com/latest/mediacenter/ $BASE main' > /etc/apt/sources.list 2>&1" &&
  233. buildah run "$cnt" -- bash -c \
  234. "apt update --allow-insecure-repositories &>/dev/null" &&
  235. MCVERSION=$(buildah run "$cnt" -- apt-cache policy mediacenter?? | grep Candidate | awk '{print $2}' | sort -V | tail -n1) &&
  236. [[ "$MCVERSION" =~ ([0-9]+.[0-9]+.[0-9]+) ]] &&
  237. VERSION_SOURCE="containerized package manager"; then
  238. buildah rm "$cnt" &>/dev/null
  239. # Webscrape
  240. elif installPackage wget && MCVERSION=$(wget -qO- "$boardurl" | grep -o "[0-9][0-9]\.[0-9]\.[0-9]\+" | head -n 1) &&
  241. [[ "$MCVERSION" =~ ([0-9]+.[0-9]+.[0-9]+) ]]; then
  242. VERSION_SOURCE="webscrape"
  243. # Hardcoded
  244. else
  245. MCVERSION="29.0.18"
  246. VERSION_SOURCE="hardcoded version"
  247. err "Warning! Using hardcoded version number"
  248. fi
  249. echo "Using MC version $MCVERSION determined by $VERSION_SOURCE"
  250. [[ "$VERSION_SOURCE" != "user input" ]] && echo "To override, use --mcversion"
  251. # Extract major version number
  252. MVERSION="${MCVERSION%%.*}"
  253. }
  254. #######################################
  255. # Installs a package using the system package manager
  256. # Arguments:
  257. # One or more package names
  258. # Options:
  259. # --skip-check-installed: Do not check if package is already installed
  260. # --nogpgcheck: Disable GPG checks for RPM based distros
  261. # --allow-downgrades: Useful for installing compatability versions on DEB based distros
  262. # --silent, -s: Do not report errors (useful if package is not strictly required and errors are noisy)
  263. #######################################
  264. installPackage() {
  265. debug "Running: ${FUNCNAME[0]}" "$@"
  266. declare -a pkg_array install_flags
  267. declare pkg skip_check_installed silent _return pkg_install_cmd
  268. if _input=$(getopt -o +s -l skip-check-installed,allow-downgrades,nogpgcheck,silent -- "$@"); then
  269. eval set -- "$_input"
  270. while true; do
  271. case "$1" in
  272. --skip-check-installed)
  273. skip_check_installed=1
  274. ;;
  275. --allow-downgrades)
  276. [[ "$ID" =~ ^(debian|ubuntu)$ ]] &&
  277. install_flags+=(--allow-downgrades)
  278. ;;
  279. --nogpgcheck)
  280. if [[ "$ID" =~ ^(fedora|centos)$ ]]; then
  281. install_flags+=(--nogpgcheck)
  282. elif [[ "$ID" == "suse" ]]; then
  283. install_flags+=(--allow-unsigned-rpm)
  284. fi
  285. ;;
  286. --silent|-s)
  287. silent=1
  288. ;;
  289. --)
  290. shift
  291. break
  292. ;;
  293. esac
  294. shift
  295. done
  296. else
  297. err "Incorrect options provided"
  298. exit 1
  299. fi
  300. # Aliases
  301. if [[ "$ID" =~ ^(debian|ubuntu)$ ]]; then
  302. declare -A PKG_ALIASES
  303. PKG_ALIASES["xorg-x11-utils"]="xorg-x11"
  304. PKG_ALIASES["rpm-build"]="rpm"
  305. PKG_ALIASES["createrepo_c"]="createrepo"
  306. PKG_ALIASES["tigervnc-server"]="tigervnc-standalone-server"
  307. fi
  308. for pkg in "$@"; do
  309. if [[ ! -v skip_check_installed && -v PKG_ALIASES[$pkg] ]]; then
  310. pkg=${PKG_ALIASES[$pkg]}
  311. fi
  312. # Check if already installed
  313. if (( skip_check_installed )) || ! (hash $pkg &>/dev/null || pkg_query "$pkg" &>/dev/null); then
  314. pkg_array+=("$pkg")
  315. fi
  316. done
  317. # Install packages from package array
  318. if [[ ${#pkg_array[@]} -ge 1 ]]; then
  319. pkg_install_cmd="pkg_install ${install_flags[*]} ${pkg_array[*]}"
  320. debug "$pkg_install_cmd" || pkg_install_cmd+=" &>/dev/null"
  321. if ! eval "$pkg_install_cmd"; then
  322. (( silent )) || err "Failed to install ${pkg_array[*]}. Attempting to continue"
  323. return 1
  324. fi
  325. fi
  326. return 0
  327. }
  328. #######################################
  329. # Adds the JRiver repository files
  330. #######################################
  331. addRepo() {
  332. debug "Running: ${FUNCNAME[0]}"
  333. if [[ "$ID" == "suse" ]]; then
  334. echo "A SUSE repository is not yet available"
  335. echo "Use --install local to locally install a SUSE RPM"
  336. exit 1
  337. fi
  338. echo "Adding JRiver repository to package manager"
  339. if [[ "$ID" =~ ^(fedora|centos)$ ]]; then
  340. declare sources_dir="/etc/yum.repos.d/"
  341. sudo bash -c "cat <<- EOF > $sources_dir/jriver.repo
  342. [jriver]
  343. name=JRiver Media Center repo by BryanC
  344. baseurl=https://repos.bryanroessler.com/jriver
  345. gpgcheck=0
  346. EOF"
  347. elif [[ "$ID" =~ ^(debian|ubuntu)$ ]]; then
  348. declare sources_dir="/etc/apt/sources.list.d"
  349. [[ ! -d $sources_dir ]] && sudo mkdir -p "$sources_dir"
  350. sudo rm -rf "$sources_dir"/mediacenter*.list
  351. installPackage wget
  352. sudo bash -c "cat <<- EOF > $sources_dir/jriver.list
  353. deb [trusted=yes arch=amd64,i386,armhf,arm64] http://dist.jriver.com/latest/mediacenter/ $BASE main
  354. EOF"
  355. #wget -q "http://dist.jriver.com/mediacenter@jriver.com.gpg.key" -O- | sudo apt-key add - &>/dev/null
  356. wget -qO- "http://dist.jriver.com/mediacenter@jriver.com.gpg.key" | sudo tee /etc/apt/trusted.gpg.d/jriver.asc
  357. elif [[ "$ID" == "suse" ]]; then
  358. sudo zypper addrepo --no-gpgcheck "https://repos.bryanroessler.com/jriver" jriver &>/dev/null
  359. elif [[ "$ID" == "arch" ]]; then
  360. sudo bash -c "cat <<- EOF >
  361. EOF"
  362. fi
  363. }
  364. #######################################
  365. # Installs JRiver Media Center from a repository
  366. #######################################
  367. installMCFromRepo() {
  368. debug "Running: ${FUNCNAME[0]}"
  369. addRepo
  370. # Update package list
  371. pkg_update_cmd="pkg_update"
  372. debug "$pkg_update_cmd" || pkg_update_cmd+=" &>/dev/null"
  373. if ! eval "$pkg_update_cmd"; then
  374. err "Package update failed!"
  375. exit 1
  376. fi
  377. echo "Installing JRiver Media Center using package manager"
  378. pkg_install_cmd="installPackage --skip-check-installed --nogpgcheck $MCPKG"
  379. debug "$pkg_install_cmd" || pkg_install_cmd+=" &>/dev/null"
  380. eval "$pkg_install_cmd"
  381. }
  382. #######################################
  383. # Acquires the source DEB package from JRiver's servers
  384. #######################################
  385. acquireDeb() {
  386. debug "Running: ${FUNCNAME[0]}"
  387. declare -g MCDEB="$OUTPUTDIR/SOURCES/MediaCenter-$MCVERSION-amd64.deb"
  388. # If necessary, create SOURCES dir
  389. [[ ! -d "$OUTPUTDIR/SOURCES" ]] && mkdir -p "$OUTPUTDIR/SOURCES"
  390. # If deb file already exists, skip download
  391. if [[ -f "$MCDEB" ]]; then
  392. echo "Using local source DEB: $MCDEB"
  393. return 0
  394. fi
  395. if [[ -v BETAPASS ]]; then
  396. echo "Checking beta repo for DEB package"
  397. if wget -q -O "$MCDEB" \
  398. "https://files.jriver.com/mediacenter/channels/v$MVERSION/beta/$BETAPASS/MediaCenter-$MCVERSION-amd64.deb"; then
  399. echo "Found!"
  400. fi
  401. elif echo "Checking latest repo for DEB package" && wget -q -O "$MCDEB" \
  402. "https://files.jriver.com/mediacenter/channels/v$MVERSION/latest/MediaCenter-$MCVERSION-amd64.deb"; then
  403. echo "Found!"
  404. elif echo "Checking test repo for DEB package" && wget -q -O "$MCDEB" \
  405. "https://files.jriver.com/mediacenter/test/MediaCenter-$MCVERSION-amd64.deb"; then
  406. echo "Found!"
  407. else
  408. err "Cannot find DEB file"
  409. exit 1
  410. fi
  411. if [[ -f "$MCDEB" ]]; then
  412. echo "Downloaded MC $MCVERSION DEB to $MCDEB"
  413. else
  414. err "Downloaded DEB file missing or corrupted"
  415. exit 1
  416. fi
  417. }
  418. #######################################
  419. # Installs Media Center DEB package and optional compatability fixes
  420. #######################################
  421. installMCDEB() {
  422. debug "Running: ${FUNCNAME[0]}"
  423. declare pkg_install_cmd="installPackage --skip-check-installed --nogpgcheck"
  424. if (( COMPAT_SWITCH )); then
  425. declare extract_dir && extract_dir="$(mktemp -d)"
  426. pushd "$extract_dir" &>/dev/null || return
  427. ar x "$MCDEB"
  428. tar -xJf "control.tar.xz"
  429. # Remove minimum version specifiers from control file
  430. sed -i 's/ ([^)]*)//g' "control"
  431. sed -i 's/([^)]*)//g' "control" # TODO MC DEB package error
  432. [[ "$ID" == "ubuntu" && "${VERSION_ID%.*}" -le 16 ]] &&
  433. sed -i 's/libva2/libva1/g' "control"
  434. tar -cJf "control.tar.xz" "control" "postinst"
  435. declare -g MCDEB="${MCDEB/.deb/.compat.deb}"
  436. ar rcs "$MCDEB" "debian-binary" "control.tar.xz" "data.tar.xz"
  437. popd &>/dev/null || return
  438. rm -rf "$extract_dir"
  439. pkg_install_cmd+=" --allow-downgrades"
  440. fi
  441. pkg_install_cmd+=" $MCDEB"
  442. debug "$pkg_install_cmd" || pkg_install_cmd+=" &>/dev/null"
  443. eval "$pkg_install_cmd"
  444. }
  445. #######################################
  446. # Creates a SPEC file and builds the RPM from the source DEB using rpmbuild
  447. #######################################
  448. buildRPM() {
  449. debug "Running: ${FUNCNAME[0]}"
  450. declare i rpmbuild_cmd
  451. declare -a requires recommends
  452. declare -A dupes
  453. # skip rebuilding the rpm if it already exists
  454. if [[ -f "$MCRPM" ]]; then
  455. echo "$MCRPM already exists. Skipping build step"
  456. return 0
  457. fi
  458. [[ ! -d "$OUTPUTDIR/SPECS" ]] && mkdir -p "$OUTPUTDIR/SPECS"
  459. # Load deb dependencies into array
  460. IFS=',' read -ra requires <<< "$(dpkg-deb -f "$MCDEB" Depends)"
  461. IFS=',' read -ra recommends <<< "$(dpkg-deb -f "$MCDEB" Recommends)"
  462. # Clean up formatting
  463. requires=("${requires[@]%%|*}")
  464. requires=("${requires[@]/?:/}")
  465. requires=("${requires[@]# }")
  466. requires=("${requires[@]% }")
  467. requires=("${requires[@]//\(/}")
  468. requires=("${requires[@]//)/}")
  469. recommends=("${recommends[@]%%|*}")
  470. recommends=("${recommends[@]/?:/}")
  471. recommends=("${recommends[@]# }")
  472. recommends=("${recommends[@]% }")
  473. recommends=("${recommends[@]//\(/}")
  474. recommends=("${recommends[@]//)/}")
  475. # Translate package names
  476. case "$TARGET" in
  477. fedora|centos)
  478. requires=("${requires[@]/libc6/glibc}")
  479. requires=("${requires[@]/libasound2/alsa-lib}")
  480. requires=("${requires[@]/libuuid1/libuuid}")
  481. requires=("${requires[@]/libx11-6/libX11}")
  482. requires=("${requires[@]/libxext6/libXext}")
  483. requires=("${requires[@]/libxcb1/libxcb}")
  484. requires=("${requires[@]/libxdmcp6/libXdmcp}")
  485. requires=("${requires[@]/libstdc++6/libstdc++}")
  486. requires=("${requires[@]/libgtk-3-0/gtk3}")
  487. requires=("${requires[@]/libgl1/mesa-libGL}")
  488. requires=("${requires[@]/libpango-1.0-0/pango}")
  489. requires=("${requires[@]/libpangoft2-1.0-0/pango}")
  490. requires=("${requires[@]/libpangox-1.0-0/pango}")
  491. requires=("${requires[@]/libpangoxft-1.0-0/pango}")
  492. requires=("${requires[@]/libnss3/nss}")
  493. requires=("${requires[@]/libnspr4/nspr}")
  494. requires=("${requires[@]/libgomp1/libgomp}")
  495. requires=("${requires[@]/libfribidi0/fribidi}")
  496. requires=("${requires[@]/libfontconfig1/fontconfig}")
  497. requires=("${requires[@]/libfreetype6/freetype}")
  498. requires=("${requires[@]/libharfbuzz0b/harfbuzz}")
  499. requires=("${requires[@]/libgbm1/mesa-libgbm}")
  500. requires=("${requires[@]/libva2/libva}")
  501. requires=("${requires[@]/libepoxy0/libepoxy}")
  502. requires=("${requires[@]/liblcms2-2/lcms2}")
  503. requires=("${requires[@]/libvulkan1/vulkan-loader}")
  504. requires=("${requires[@]/libepoxy0/libepoxy}")
  505. requires=("${requires[@]/python/python3}")
  506. ;;
  507. suse)
  508. requires=("${requires[@]/libc6/glibc}")
  509. requires=("${requires[@]/libasound2/alsa-lib}")
  510. requires=("${requires[@]/libx11-6/libX11-6}")
  511. requires=("${requires[@]/libxext6/libXext6}")
  512. requires=("${requires[@]/libxdmcp6/libXdmcp6}")
  513. requires=("${requires[@]/libgtk-3-0/gtk3}")
  514. requires=("${requires[@]/libgl1/Mesa-libGL1}")
  515. requires=("${requires[@]/libpango-1.0-0/pango}")
  516. requires=("${requires[@]/libpangoft2-1.0-0/pango}")
  517. requires=("${requires[@]/libpangox-1.0-0/pango}")
  518. requires=("${requires[@]/libpangoxft-1.0-0/pango}")
  519. requires=("${requires[@]/libnss3/mozilla-nss}")
  520. requires=("${requires[@]/libnspr4/mozilla-nspr}")
  521. requires=("${requires[@]/libfribidi0/fribidi}")
  522. requires=("${requires[@]/libfontconfig1/fontconfig}")
  523. requires=("${requires[@]/libfreetype6*/freetype}") # Remove minimum version specifier
  524. requires=("${requires[@]/libharfbuzz0b/libharfbuzz0}")
  525. for i in "${!requires[@]}"; do
  526. [[ "${requires[$i]}" == "mesa-vulkan-drivers" ]] && unset -v 'requires[i]'
  527. done
  528. recommends+=(libvulkan_intel)
  529. recommends+=(libvulkan_radeon)
  530. ;;
  531. esac
  532. # Remove duplicates
  533. for i in "${requires[@]}"; do
  534. if [[ ! -v dupes[${i%% *}] ]]; then
  535. tmp+=("$i")
  536. fi
  537. dupes["${i%% *}"]=1
  538. done
  539. requires=("${tmp[@]}")
  540. # Convert array to newline delim'd string (for heredoc)
  541. printf -v requires "Requires: %s\n" "${requires[@]}"
  542. printf -v recommends "Recommends: %s\n" "${recommends[@]}"
  543. # Strip last newline
  544. requires="${requires%?}"
  545. recommends="${recommends%?}"
  546. if (( COMPAT_SWITCH )); then
  547. # Strip minimum versions
  548. requires=$(echo "$requires" | awk -F" " 'NF == 4 {print $1 " " $2} NF != 4 {print $0}')
  549. fi
  550. # Create spec file
  551. cat <<- EOF > "$OUTPUTDIR/SPECS/mediacenter.spec"
  552. Name: MediaCenter
  553. Version: $MCVERSION
  554. Release: 1
  555. Summary: JRiver Media Center
  556. Group: Applications/Media
  557. Source0: http://files.jriver.com/mediacenter/channels/v$MVERSION/latest/MediaCenter-$MCVERSION-amd64.deb
  558. BuildArch: x86_64
  559. %define _rpmfilename %%{ARCH}/%%{NAME}-%%{version}.%%{ARCH}.rpm
  560. AutoReq: 0
  561. $requires
  562. $recommends
  563. Provides: mediacenter$MVERSION
  564. License: Copyright 1998-2022, JRiver, Inc. All rights reserved. Protected by U.S. patents #7076468 and #7062468
  565. URL: http://www.jriver.com/
  566. %define __provides_exclude_from ^%{_libdir}/jriver/.*/.*\\.so.*$
  567. %description
  568. Media Center is more than a world class player.
  569. %global __os_install_post %{nil}
  570. %prep
  571. %build
  572. %install
  573. dpkg -x %{S:0} %{buildroot}
  574. %post -p /sbin/ldconfig
  575. %postun -p /sbin/ldconfig
  576. %files
  577. %{_bindir}/mediacenter$MVERSION
  578. %{_libdir}/jriver
  579. %{_datadir}
  580. %exclude %{_datadir}/applications/media_center_packageinstaller_$MVERSION.desktop
  581. /etc/security/limits.d/*
  582. EOF
  583. # Run rpmbuild
  584. echo "Building RPM for MC $MCVERSION, this may take awhile"
  585. rpmbuild_cmd="rpmbuild --define=\"%_topdir $OUTPUTDIR\" --define=\"%_libdir /usr/lib\" -bb"
  586. rpmbuild_cmd+=" $OUTPUTDIR/SPECS/mediacenter.spec"
  587. debug "$rpmbuild_cmd" || rpmbuild_cmd+=" &>/dev/null"
  588. if eval "$rpmbuild_cmd" && [[ -f "$MCRPM" ]] ; then
  589. echo "Build successful. The RPM file is located at: $MCRPM"
  590. else
  591. err "Build failed"
  592. # For automation, let's remove the source DEB and reaquire it on next run
  593. # after failure in case it is corrupted or buggy
  594. [[ -f "$MCDEB" ]] && echo "Removing source DEB" && rm -f "$MCDEB"
  595. exit 1
  596. fi
  597. }
  598. installMCArch() {
  599. debug "Running: ${FUNCNAME[0]}"
  600. echo "Arch install under construction"
  601. }
  602. #######################################
  603. # Copy the RPM to createrepo-webroot and runs createrepo as the createrepo-user
  604. #######################################
  605. runCreaterepo() {
  606. debug "Running: ${FUNCNAME[0]}"
  607. declare cr_cmd cr_cp_cmd cr_mkdir_cmd cr_chown_cmd
  608. installPackage createrepo_c
  609. # If the webroot does not exist, create it
  610. if [[ ! -d "$CREATEREPO_WEBROOT" ]]; then
  611. cr_mkdir_cmd="sudo -u $CREATEREPO_USER mkdir -p $CREATEREPO_WEBROOT"
  612. debug "$cr_mkdir_cmd" || cr_mkdir_cmd+=" &>/dev/null"
  613. if ! eval "$cr_mkdir_cmd"; then
  614. cr_mkdir_cmd="sudo mkdir -p $CREATEREPO_WEBROOT"
  615. debug "$cr_mkdir_cmd" || cr_mkdir_cmd+=" &>/dev/null"
  616. cr_chown_cmd="sudo chown -R $CREATEREPO_USER:$CREATEREPO_USER $CREATEREPO_WEBROOT"
  617. debug "$cr_chown_cmd" || cr_chown_cmd+=" &>/dev/null"
  618. if ! ( eval "$cr_mkdir_cmd" && eval "$cr_chown_cmd" ); then
  619. err "Could not create the createrepo-webroot path!"
  620. err "Make sure that the webroot $CREATEREPO_WEBROOT is writeable by user $CREATEREPO_USER"
  621. err "Or change the repo ownership with --createrepo-user"
  622. return 1
  623. fi
  624. fi
  625. fi
  626. # Copy built rpms to webroot
  627. cr_cp_cmd="sudo cp -nf $MCRPM $CREATEREPO_WEBROOT"
  628. cr_chown_cmd="sudo chown -R $CREATEREPO_USER:$CREATEREPO_USER $CREATEREPO_WEBROOT"
  629. debug "$cr_cp_cmd" || cr_cp_cmd+=" &>/dev/null"
  630. debug "$cr_chown_cmd" || cr_cp_cmd+=" &>/dev/null"
  631. if ! ( eval "$cr_cp_cmd" && eval "$cr_chown_cmd" ); then
  632. err "Could not copy $MCRPM to $CREATEREPO_WEBROOT"
  633. return 1
  634. fi
  635. # Run createrepo
  636. cr_cmd="sudo -u $CREATEREPO_USER createrepo -q $CREATEREPO_WEBROOT"
  637. [[ -d "$CREATEREPO_WEBROOT/repodata" ]] && cr_cmd+=" --update"
  638. debug "$cr_cmd" || cr_cmd+=" &>/dev/null"
  639. if ! eval "$cr_cmd"; then
  640. cr_cmd="sudo createrepo -q $CREATEREPO_WEBROOT"
  641. [[ -d "$CREATEREPO_WEBROOT/repodata" ]] && cr_cmd+=" --update"
  642. cr_chown_cmd="sudo chown -R $CREATEREPO_USER:$CREATEREPO_USER $CREATEREPO_WEBROOT"
  643. debug "$cr_cmd" || cr_cmd+=" &>/dev/null"
  644. debug "$cr_chown_cmd" || cr_cp_cmd+=" &>/dev/null"
  645. if ! ( eval "$cr_cmd" && eval "$cr_chown_cmd"); then
  646. err "Createrepo failed"
  647. return 1
  648. fi
  649. fi
  650. echo "Successfully updated repo"
  651. }
  652. #######################################
  653. # Symlink certificates where JRiver Media Center expects them to be on Fedora/CentOS
  654. #######################################
  655. symlinkCerts() {
  656. debug "Running: ${FUNCNAME[0]}"
  657. if [[ ! -f /etc/ssl/certs/ca-certificates.crt &&
  658. -f /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem ]]; then
  659. debug "ln -s /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem /etc/ssl/certs/ca-certificates.crt"
  660. if ! sudo ln -s /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem \
  661. /etc/ssl/certs/ca-certificates.crt; then
  662. err "Symlinking certificates failed"
  663. return 1
  664. fi
  665. fi
  666. }
  667. #######################################
  668. # Restore the mjr license file if it is next to installJRMC or RESTOREFILE is set
  669. #######################################
  670. restoreLicense() {
  671. debug "Running: ${FUNCNAME[0]}"
  672. declare mjrfile
  673. # Allow user to put the mjr file next to installJRMC
  674. if [[ ! -v RESTOREFILE ]]; then
  675. for mjrfile in "$PWD"/*.mjr; do
  676. [[ $mjrfile -nt $RESTOREFILE ]] && RESTOREFILE="$mjrfile"
  677. done
  678. fi
  679. # Restore license
  680. if [[ -f "$RESTOREFILE" ]]; then
  681. if ! "mediacenter$MVERSION" /RestoreFromFile "$RESTOREFILE"; then
  682. err "Automatic license restore failed"
  683. return 1
  684. fi
  685. fi
  686. }
  687. #######################################
  688. # Opens ports using the system firewall tool
  689. # Arguments:
  690. # Pre-defined service to enable
  691. #######################################
  692. openFirewall() {
  693. debug "Running: ${FUNCNAME[0]}" "$@"
  694. # Create OS-specific port rules based on argument (service) name
  695. declare -a f_ports # for firewall-cmd
  696. declare u_ports # for ufw
  697. declare -a n_ports # for nftables
  698. declare port
  699. if [[ "$1" == "jriver-mediacenter" ]]; then
  700. f_ports=(52100-52200/tcp 1900/udp)
  701. u_ports="52100:52200/tcp|1900/udp"
  702. n_ports=("tcp dport 52100-52200 accept" "udp dport 1900 accept")
  703. elif [[ "$1" =~ ^(jriver-x11vnc|jriver-xvnc)$ ]]; then
  704. f_ports=("$PORT"/tcp 1900/udp)
  705. u_ports="$PORT/tcp|1900/udp"
  706. n_ports=("tcp dport $PORT accept" "udp dport 1900 accept")
  707. fi
  708. # Open the ports
  709. if [[ "$ID" =~ ^(fedora|centos|suse)$ ]]; then
  710. hash firewall-cmd 2>/dev/null || installPackage firewalld
  711. if ! firewall_cmd --get-services | grep -q "$1"; then
  712. firewall_cmd --permanent --new-service="$1" &>/dev/null
  713. firewall_cmd --permanent --service="$1" --set-description="$1 installed by installJRMC" &>/dev/null
  714. firewall_cmd --permanent --service="$1" --set-short="$1" &>/dev/null
  715. for port in "${f_ports[@]}"; do
  716. firewall_cmd --permanent --service="$1" --add-port="$port" &>/dev/null
  717. done
  718. firewall_cmd --add-service "$1" --permanent &>/dev/null
  719. firewall_cmd --reload &>/dev/null
  720. fi
  721. elif [[ "$ID" =~ ^(debian|ubuntu)$ ]]; then
  722. # Debian ufw package state is broken on fresh installations
  723. hash ufw 2>/dev/null || installPackage ufw
  724. if [[ ! -f "/etc/ufw/applications.d/$1" ]]; then
  725. sudo bash -c "cat <<- EOF > /etc/ufw/applications.d/$1
  726. [$1]
  727. title=$1
  728. description=$1 installed by installJRMC
  729. ports=$u_ports
  730. EOF"
  731. fi
  732. firewall_cmd app update "$1" &>/dev/null
  733. firewall_cmd allow "$1" &>/dev/null
  734. elif [[ "$ID" == "arch" ]]; then
  735. sysctl -w net.ipv4.ip_forward = 1
  736. sudo nft create table inet "jriver"
  737. sudo nft create chain inet "jriver" "$1" '{ type filter hook input priority 0; policy accept; }'
  738. for port in "${n_ports[@]}"; do
  739. sudo nft add rule inet jriver "$1" handle tcp dport "$port"
  740. done
  741. fi
  742. # shellcheck disable=SC2181 # More concise
  743. if [[ $? -ne 0 ]]; then
  744. err "Firewall ports could not be opened"
  745. return 1
  746. fi
  747. }
  748. #######################################
  749. # Create the xvnc or x11vnc password file
  750. # Arguments:
  751. # Service type (xvnc, x11vnc)
  752. #######################################
  753. setVNCPass() {
  754. debug "Running: ${FUNCNAME[0]}"
  755. declare vncpassfile="$HOME/.vnc/jrmc_passwd"
  756. declare vnc_pass_cmd
  757. [[ ! -d "${vncpassfile%/*}" ]] && mkdir -p "${vncpassfile%/*}"
  758. if [[ -f "$vncpassfile" ]]; then
  759. if [[ ! -v VNCPASS ]]; then
  760. err "Refusing to overwrite existing $vncpassfile with an empty password"
  761. err "Remove existing $vncpassfile or set --vncpass to use an empty password"
  762. exit 1
  763. else
  764. rm -f "$vncpassfile"
  765. fi
  766. fi
  767. if [[ -v VNCPASS ]]; then
  768. if [[ $1 == "xvnc" ]]; then
  769. vnc_pass_cmd="echo $VNCPASS | vncpasswd -f > $vncpassfile"
  770. elif [[ $1 == "x11vnc" ]]; then
  771. vnc_pass_cmd="x11vnc -storepasswd $VNCPASS $vncpassfile"
  772. fi
  773. if ! eval "$vnc_pass_cmd"; then
  774. err "Could not create VNC password file"
  775. return 1
  776. fi
  777. else
  778. declare -g NOVNCAUTH=1
  779. fi
  780. }
  781. #######################################
  782. # Set display and port variables
  783. #######################################
  784. setDisplay() {
  785. debug "Running: ${FUNCNAME[0]}"
  786. declare -g DISPLAY DISPLAYNUM NEXT_DISPLAY NEXT_DISPLAYNUM
  787. # Check USER_DISPLAY, else environment DISPLAY, else set to :0 by default
  788. DISPLAY="${USER_DISPLAY:-${DISPLAY:-0}}"
  789. DISPLAYNUM="${DISPLAY#*:}" # strip prefix
  790. DISPLAYNUM="${DISPLAYNUM%%.*}" # strip suffix
  791. NEXT_DISPLAYNUM=$(( DISPLAYNUM + 1 ))
  792. NEXT_DISPLAY=":$NEXT_DISPLAYNUM"
  793. }
  794. #######################################
  795. # Create associated service variables based on service name
  796. # Arguments
  797. # Pre-defined service name
  798. #######################################
  799. setServiceVars() {
  800. debug "Running: ${FUNCNAME[0]}"
  801. declare -g SERVICE_NAME SERVICE_FNAME TIMER_NAME TIMER_FNAME USER_STRING GRAPHICAL_TARGET
  802. declare -g SERVICE_TYPE="${SERVICE_TYPE:-system}"
  803. declare service_dir="/usr/lib/systemd/$SERVICE_TYPE"
  804. if [[ "$USER" == "root" && "$SERVICE_TYPE" == "user" ]]; then
  805. err "Trying to install user service as root"
  806. err "Use --service-type service and/or execute installJRMC as non-root user"
  807. return 1
  808. fi
  809. if [[ "$SERVICE_TYPE" == "system" ]]; then
  810. systemctl_reload_cmd(){ sudo systemctl daemon-reload; }
  811. systemctl_enable_cmd(){ sudo systemctl enable --now "$@"; }
  812. systemctl_disable_cmd(){ sudo systemctl disable --now "$@"; }
  813. systemctl_is_enabled_cmd(){ sudo systemctl is-enabled -q "$@"; }
  814. systemctl_is_active_cmd(){ sudo systemctl is-active -q "$@"; }
  815. GRAPHICAL_TARGET="graphical.target"
  816. elif [[ "$SERVICE_TYPE" == "user" ]]; then
  817. systemctl_reload_cmd(){ systemctl --user daemon-reload; }
  818. systemctl_enable_cmd(){ systemctl --user enable --now "$@"; }
  819. systemctl_disable_cmd(){ systemctl --user disable --now "$@"; }
  820. systemctl_is_enabled_cmd(){ systemctl --user is-enabled -q "$@"; }
  821. systemctl_is_active(){ sudo systemctl is-active -q "$@"; }
  822. GRAPHICAL_TARGET="default.target"
  823. fi
  824. [[ ! -d "$service_dir" ]] && sudo mkdir -p "$service_dir"
  825. if [[ "$SERVICE_TYPE" == "system" && "$USER" != "root" ]]; then
  826. SERVICE_FNAME="$service_dir/${1}@.service"
  827. TIMER_FNAME="$service_dir/${1}@.timer"
  828. SERVICE_NAME="${1}@$USER.service"
  829. TIMER_NAME="${1}@$USER.timer"
  830. USER_STRING="User=%I"
  831. else
  832. SERVICE_NAME="${1}.service"
  833. TIMER_NAME="${1}.timer"
  834. SERVICE_FNAME="$service_dir/${SERVICE_NAME}"
  835. TIMER_FNAME="$service_dir/${TIMER_NAME}"
  836. USER_STRING=""
  837. fi
  838. }
  839. #######################################
  840. # Starts and enables (at startup) a JRiver Media Center service
  841. # Arguments:
  842. # Passes arguments as startup options to /usr/bin/mediacenter??
  843. #######################################
  844. service_jriver-mediacenter() {
  845. debug "Running: ${FUNCNAME[0]}"
  846. setServiceVars "${FUNCNAME[0]##*_}"
  847. sudo bash -c "cat <<- EOF > $SERVICE_FNAME
  848. [Unit]
  849. Description=JRiver Media Center $MVERSION
  850. After=$GRAPHICAL_TARGET
  851. [Service]
  852. $USER_STRING
  853. Type=simple
  854. Environment=DISPLAY=$DISPLAY
  855. Environment=XAUTHORITY=$XAUTHORITY
  856. ExecStart=/usr/bin/mediacenter$MVERSION $*
  857. Restart=always
  858. RestartSec=10
  859. KillSignal=SIGHUP
  860. TimeoutStopSec=30
  861. [Install]
  862. WantedBy=$GRAPHICAL_TARGET
  863. EOF"
  864. systemctl_reload_cmd &&
  865. systemctl_enable_cmd "$SERVICE_NAME" &&
  866. openFirewall "jriver-mediacenter"
  867. }
  868. #######################################
  869. # Starts and enables (at startup) a JRiver Media Server service
  870. #######################################
  871. service_jriver-mediaserver() {
  872. debug "Running: ${FUNCNAME[0]}"
  873. setServiceVars "${FUNCNAME[0]##*_}"
  874. service_jriver-mediacenter "/MediaServer"
  875. }
  876. #######################################
  877. # Starts and enables (at startup) JRiver Media Center in a new Xvnc session
  878. #######################################
  879. service_jriver-xvnc() {
  880. debug "Running: ${FUNCNAME[0]}"
  881. setServiceVars "${FUNCNAME[0]##*_}"
  882. setDisplay
  883. declare start_cmd
  884. declare -g PORT=$(( NEXT_DISPLAYNUM + 5900 ))
  885. installPackage tigervnc-server
  886. setVNCPass xvnc
  887. if (( NOVNCAUTH )); then
  888. start_cmd="/usr/bin/vncserver $NEXT_DISPLAY -geometry 1440x900 -alwaysshared -name jriver$NEXT_DISPLAY -SecurityTypes None -autokill -xstartup /usr/bin/mediacenter$MVERSION"
  889. else
  890. start_cmd="/usr/bin/vncserver $NEXT_DISPLAY -geometry 1440x900 -alwaysshared -rfbauth $HOME/.vnc/jrmc_passwd -autokill -xstartup /usr/bin/mediacenter$MVERSION"
  891. fi
  892. sudo bash -c "cat <<- EOF > $SERVICE_FNAME
  893. [Unit]
  894. Description=Remote desktop service (VNC)
  895. After=multi-user.target
  896. [Service]
  897. Type=forking
  898. $USER_STRING
  899. ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill $NEXT_DISPLAY &>/dev/null || :'
  900. ExecStart=$start_cmd
  901. ExecStop=/usr/bin/vncserver -kill $NEXT_DISPLAY
  902. Restart=always
  903. [Install]
  904. WantedBy=multi-user.target
  905. EOF"
  906. systemctl_reload_cmd &&
  907. systemctl_enable_cmd "$SERVICE_NAME" &&
  908. echo "Xvnc running on localhost:$PORT" &&
  909. openFirewall "jriver-xvnc" &&
  910. openFirewall "jriver-mediacenter"
  911. }
  912. #######################################
  913. # Starts and enables (at startup) x11vnc screen sharing for the local desktop
  914. #######################################
  915. service_jriver-x11vnc() {
  916. debug "Running: ${FUNCNAME[0]}"
  917. setServiceVars "${FUNCNAME[0]##*_}"
  918. setDisplay
  919. declare start_cmd
  920. declare -g PORT=$(( DISPLAYNUM + 5900 ))
  921. installPackage x11vnc
  922. setVNCPass x11vnc
  923. # Get current desktop resolution
  924. # TODO: may need to break this out into its own function and get smarter at identifying multi-monitors
  925. # _getResolution() {
  926. # debug "Running: ${FUNCNAME[0]}"
  927. # installPackage xorg-x11-utils
  928. # _res=$(xdpyinfo | grep dimensions | awk '{print $2}')
  929. # }
  930. # _getResolution
  931. # If .Xauthority file is missing, generate a dummy for x11vnc -auth guess
  932. if [[ ! -f "$HOME/.Xauthority" ]]; then
  933. [[ "$XDG_SESSION_TYPE" == "wayland" ]] && ask_ok "Unsupported Wayland session detected for x11vnc, continue?" || return 1
  934. touch "$HOME/.Xauthority"
  935. xauth generate "$DISPLAY" . trusted
  936. xauth add "$HOST$DISPLAY" . "$(xxd -l 16 -p /dev/urandom)"
  937. fi
  938. if (( NOVNCAUTH )); then
  939. start_cmd="/usr/bin/x11vnc -display $DISPLAY -noscr -auth guess -forever -bg -nopw"
  940. else
  941. start_cmd="/usr/bin/x11vnc -display $DISPLAY -noscr -auth guess -forever -bg -rfbauth $HOME/.vnc/jrmc_passwd"
  942. fi
  943. sudo bash -c "cat <<-EOF > $SERVICE_FNAME
  944. [Unit]
  945. Description=x11vnc
  946. After=$GRAPHICAL_TARGET
  947. [Service]
  948. $USER_STRING
  949. Type=forking
  950. Environment=DISPLAY=$DISPLAY
  951. ExecStart=$start_cmd
  952. Restart=always
  953. RestartSec=10
  954. [Install]
  955. WantedBy=$GRAPHICAL_TARGET
  956. EOF"
  957. systemctl_reload_cmd &&
  958. systemctl_enable_cmd "$SERVICE_NAME" &&
  959. echo "x11vnc running on localhost:$PORT" &&
  960. openFirewall "jriver-x11vnc"
  961. }
  962. #######################################
  963. # Starts and enables (at startup) an hourly service to build the latest version of JRiver Media
  964. # Center RPM from the source DEB and create/update an RPM repository
  965. #######################################
  966. service_jriver-createrepo() {
  967. debug "Running: ${FUNCNAME[0]}"
  968. if [[ "$CREATEREPO_USER" != "$USER" ]]; then
  969. USER="root" SERVICE_TYPE="system" setServiceVars "${FUNCNAME[0]##*_}"
  970. else
  971. setServiceVars "${FUNCNAME[0]##*_}"
  972. fi
  973. sudo bash -c "cat <<-EOF > $SERVICE_FNAME
  974. [Unit]
  975. Description=Builds JRiver Media Center RPM file, moves it to the repo dir, and runs createrepo
  976. [Service]
  977. $USER_STRING
  978. ExecStart=$PWD/installJRMC --outputdir $OUTPUTDIR --createrepo=$TARGET --createrepo-webroot $CREATEREPO_WEBROOT --createrepo-user $CREATEREPO_USER
  979. [Install]
  980. WantedBy=multi-user.target
  981. EOF"
  982. sudo bash -c "cat <<-EOF > $TIMER_FNAME
  983. [Unit]
  984. Description=Run JRiver MC rpmbuild hourly
  985. [Timer]
  986. OnCalendar=hourly
  987. Persistent=true
  988. [Install]
  989. WantedBy=timers.target
  990. EOF"
  991. systemctl_reload_cmd &&
  992. systemctl_enable_cmd "$TIMER_NAME"
  993. }
  994. #######################################
  995. # CONTAINERS
  996. #######################################
  997. # container_jriver-createrepo() {
  998. # :
  999. # }
  1000. # container_jriver-xvnc() {
  1001. # :
  1002. # }
  1003. # container_jriver-mediacenter() {
  1004. # installPackage buildah podman
  1005. # # Eventually try to switch to Debian
  1006. # # if ! CNT=$(buildah from debian:$BASE); then
  1007. # # echo "Bad base image for container $CNAME, skipping"
  1008. # # continue
  1009. # # fi
  1010. # if ! CNT=$(buildah from jlesage/baseimage-gui:debian-10-v3.5.7); then
  1011. # echo "Bad base image for container $CNAME, skipping"
  1012. # continue
  1013. # fi
  1014. # buildah run "$CNT" add-pkg gnupg2 libxss1 wmctrl xdotool ca-certificates inotify-tools libgbm1 ffmpeg
  1015. # # #BASEIMAGE=jlesage/baseimage-gui:debian-10-v3.5.7
  1016. # # JRIVER_RELEASE=28
  1017. # # JRIVER_TAG=stable
  1018. # # ARCH=amd64
  1019. # # REBUILD_MIN=22
  1020. # # REBUILD_MAX=120
  1021. # # # JRiver Version tag (latest, stable or beta)
  1022. # # ARG jriver_tag
  1023. # # # JRiver Release Version (25, 26 etc.)
  1024. # # ARG jriver_release
  1025. # # # Image Version of the build
  1026. # # ARG image_version
  1027. # # # .deb download URL, if set to "repository" the JRiver repository will be used
  1028. # # ARG deb_url
  1029. # # RUN add-pkg gnupg2 libxss1 wmctrl xdotool ca-certificates inotify-tools libgbm1 ffmpeg
  1030. # # Install JRiver
  1031. # RUN \
  1032. # add-pkg --virtual build-dependencies wget &&
  1033. # # Install from Repository
  1034. # if [ "${deb_url}" = "repository" ]; then \
  1035. # echo "Installing JRiver from repository ${jriver_release}:${jriver_tag}" &&
  1036. # wget -q "http://dist.jriver.com/mediacenter@jriver.com.gpg.key" -O- | apt-key add - &&
  1037. # wget http://dist.jriver.com/${jriver_tag}/mediacenter/mediacenter${jriver_release}.list -O /etc/apt/sources.list.d/mediacenter${jriver_release}.list &&
  1038. # apt update &&
  1039. # add-pkg mediacenter${jriver_release}; \
  1040. # # Install from .deb URL
  1041. # else \
  1042. # echo "Installing JRiver from URL: ${deb_url}" &&
  1043. # wget -q -O "jrivermc.deb" ${deb_url} &&
  1044. # add-pkg "./jrivermc.deb"; \
  1045. # fi &&
  1046. # # Cleanup
  1047. # del-pkg build-dependencies &&
  1048. # rm -rf /tmp/* /tmp/.[!.]*
  1049. # # Add rootfs
  1050. # COPY rootfs/ /
  1051. # VOLUME ["/config"]
  1052. # # Application Icon
  1053. # RUN \
  1054. # APP_ICON_URL=https://gitlab.com/shiomax/jrivermc-docker/raw/master/assets/Application.png &&
  1055. # install_app_icon.sh "$APP_ICON_URL"
  1056. # # Various configuration vars
  1057. # ENV KEEP_APP_RUNNING=1 \
  1058. # DISPLAY_WIDTH=1280 \
  1059. # DISPLAY_HEIGHT=768 \
  1060. # APP_NAME="JRiver MediaCenter ${jriver_release}" \
  1061. # MAXIMIZE_POPUPS=0 \
  1062. # S6_KILL_GRACETIME=8000
  1063. # # Modify startapp.sh and rc.xml with JRiver version
  1064. # RUN sed-patch s/%%MC_VERSION%%/${jriver_release}/g \
  1065. # /startapp.sh &&
  1066. # sed-patch s/%%MC_VERSION%%/${jriver_release}/g \
  1067. # /etc/xdg/openbox/rc.xml
  1068. # EXPOSE 5800 5900 52100 52101 52199 1900/udp
  1069. # # Metadata.
  1070. # LABEL \
  1071. # org.label-schema.name="jrivermc${jriver_release}" \
  1072. # org.label-schema.description="Docker image for JRiver Media Center ${jriver_release}" \
  1073. # org.label-schema.version="${image_version}" \
  1074. # org.label-schema.vcs-url="https://gitlab.com/shiomax/jrivermc-docker" \
  1075. # org.label-schema.schema-version="1.0"
  1076. # installPackage buildah podman
  1077. # cnt=$(buildah from docker.io/jlesage/baseimage-gui:debian-10)
  1078. # podman_create_cmd=(podman create --name "$CNAME")
  1079. # buildah_config_cmd=(buildah config \
  1080. # --author bryanroessler@gmail.com \
  1081. # --label maintainer="$MAINTAINER" \
  1082. # --env TZ="$TZ" \
  1083. # --workingdir /app \
  1084. # --cmd mediacenter"$MVERSION")
  1085. # mkcdirs() {
  1086. # declare dir
  1087. # for dir in "$@"; do
  1088. # if [[ ! -d "$dir" ]]; then
  1089. # if ! mkdir -p "$dir"; then
  1090. # err "Could not create directory $dir, check your permissions"
  1091. # fi
  1092. # fi
  1093. # if ! chcon -t container_file_t -R "$dir"; then
  1094. # err "Could not set container_file_t attribute for $dir, check your permissions"
  1095. # fi
  1096. # done
  1097. # }
  1098. # mkcdirs "$HOME/.jriver"
  1099. # podman_create_cmd+=(-v "$HOME/.jriver:/root/.jriver")
  1100. # podman_create_cmd+=(-v "$DOWNLOAD_ROOT:/downloads:z")
  1101. # podman_create_cmd+=(-v "$MONITOR_ROOT/nzbs:/nzbs")
  1102. # podman_create_cmd+=(-p "${CONTAINER[HOST_PORT]}:${CONTAINER[CONTAINER_PORT]}")
  1103. # brc() { buildah run "$1" "${@:2}" || return 1; }
  1104. # brc add-pkg gnupg2 libxss1 wmctrl xdotool ca-certificates inotify-tools libgbm1
  1105. # brc add-pkg --virtual .build-deps wget
  1106. # brc sh -s <<- EOF
  1107. # wget -q "http://dist.jriver.com/mediacenter@jriver.com.gpg.key" -O- | apt-key add - &>/dev/null
  1108. # EOF
  1109. # brc wget "http://dist.jriver.com/latest/mediacenter/mediacenter$MVERSION.list" -O "/etc/apt/sources.list.d/mediacenter$MVERSION.list"
  1110. # brc apt update -y -q0
  1111. # brc add-pkg "mediacenter$MVERSION"
  1112. # brc del-pkg .build-deps
  1113. # }
  1114. #######################################
  1115. # Perform OS detection and use compatability modes if necessary
  1116. #######################################
  1117. getOS() {
  1118. debug "Running: ${FUNCNAME[0]}"
  1119. declare -g ID MGR
  1120. if [[ -e "/etc/os-release" ]]; then
  1121. source "/etc/os-release"
  1122. else
  1123. err "/etc/os-release not found"
  1124. err "Your OS is unsupported"
  1125. printHelp && exit 1
  1126. fi
  1127. debug "Detected host platform: $ID $VERSION_ID"
  1128. # normalize ID
  1129. case "$ID" in
  1130. fedora|arch|debian|centos)
  1131. ;;
  1132. rhel)
  1133. ID="centos"
  1134. ;;
  1135. linuxmint|neon|*ubuntu*)
  1136. ID="ubuntu"
  1137. ;;
  1138. *suse*)
  1139. ID="suse"
  1140. ;;
  1141. *)
  1142. echo "Autodetecting distro, this may be unreliable and --compat may also be required"
  1143. if hash dnf &>/dev/null; then
  1144. ID="fedora"
  1145. MGR="dnf"
  1146. elif hash yum &>/dev/null; then
  1147. ID="centos"
  1148. MGR="yum"
  1149. COMPAT_SWITCH=1
  1150. elif hash apt &>/dev/null; then
  1151. ID="ubuntu"
  1152. elif hash pacman &>/dev/null; then
  1153. ID="arch"
  1154. else
  1155. err "OS detection failed!"
  1156. exit 1
  1157. fi
  1158. esac
  1159. # Set package manager for RPM distros
  1160. case "$ID" in
  1161. centos|fedora)
  1162. if hash dnf &>/dev/null; then
  1163. MGR="dnf"
  1164. elif hash yum &>/dev/null; then
  1165. MGR="yum"
  1166. fi
  1167. ;;
  1168. esac
  1169. debug "Using host platform: $ID $VERSION_ID"
  1170. }
  1171. #######################################
  1172. # Detects if MC is installed on btrfs and disables CoW
  1173. #######################################
  1174. disableCoW() {
  1175. debug "Running: ${FUNCNAME[0]}"
  1176. declare dir
  1177. declare mc_system_path="/usr/lib/jriver"
  1178. declare mc_user_path="$HOME/.jriver"
  1179. for dir in "$mc_system_path" "$mc_user_path"; do
  1180. ! [[ -d "$dir" ]] && return
  1181. if [[ $(stat -f -c %T "$dir") == "btrfs" ]] &&
  1182. ! lsattr -d "$dir" | cut -f1 -d" " | grep -q C; then
  1183. echo "Disabling CoW for $dir"
  1184. sudo chattr +C "$dir"
  1185. fi
  1186. done
  1187. }
  1188. #######################################
  1189. # Migrate major versions
  1190. #######################################
  1191. migrateLibrary() {
  1192. debug "Running: ${FUNCNAME[0]}"
  1193. declare mc_user_path="$HOME/.jriver"
  1194. declare current_config_path="$mc_user_path/Media Center $MVERSION"
  1195. declare previous_config_path="$mc_user_path/Media Center $(( MVERSION - 1 ))"
  1196. if [[ ! -d "$current_config_path" ]] &&
  1197. [[ -d "$previous_config_path" ]] &&
  1198. mkdir -p "$current_config_path"; then
  1199. echo "Migrating $previous_config_path to $current_config_path"
  1200. cp -a "$previous_config_path" "$current_config_path" &>/dev/null
  1201. fi
  1202. }
  1203. #######################################
  1204. # Completely uninstalls MC, services, and firewall rules
  1205. #######################################
  1206. uninstall() {
  1207. debug "Running: ${FUNCNAME[0]}"
  1208. declare service unit f i
  1209. if ! askOk "Do you really want to uninstall JRiver Media Center"; then
  1210. echo "Uninstall canceled"
  1211. exit 0
  1212. fi
  1213. echo "Stopping and removing all Media Center services"
  1214. for service in $(compgen -A "function" "service"); do
  1215. service="${service##service_}"
  1216. for i in user system; do
  1217. SERVICE_TYPE="$i" setServiceVars "$service";
  1218. for unit in "$SERVICE_NAME" "$TIMER_NAME"; do
  1219. if systemctl_is_active_cmd "$unit" &>/dev/null ||
  1220. systemctl_is_enabled_cmd "$unit" &>/dev/null; then
  1221. debug "Disabling $unit"
  1222. systemctl_disable_cmd "$unit"
  1223. fi
  1224. done
  1225. for f in "$SERVICE_FNAME" "$TIMER_FNAME"; do
  1226. [[ -f "$f" ]] && debug "Removing $f" && sudo rm -f "$f"
  1227. done
  1228. systemctl_reload_cmd
  1229. done
  1230. for f in /etc/systemd/system/jriver-*; do
  1231. sudo rm -f "$f"
  1232. done
  1233. done
  1234. echo "Removing repo files"
  1235. sudo rm -rf \
  1236. "/etc/yum.repos.d/jriver.repo" \
  1237. /etc/apt/sources.list.d/{jriver,mediacenter}*.list # also remove legacy repo files
  1238. if [[ "$ID" == "suse" ]]; then
  1239. sudo zypper rr jriver &>/dev/null
  1240. fi
  1241. echo "Removing firewall rules"
  1242. if hash firewall-cmd 2>/dev/null; then
  1243. if [[ -v debug ]]; then
  1244. debug "firewall_cmd --permanent --remove-service=jriver"
  1245. firewall_cmd --permanent --remove-service=jriver
  1246. debug "firewall_cmd --permanent --delete-service=jriver"
  1247. firewall_cmd --permanent --delete-service=jriver
  1248. debug "firewall_cmd --reload"
  1249. firewall_cmd --reload
  1250. else
  1251. firewall_cmd --permanent --remove-service=jriver &>/dev/null
  1252. firewall_cmd --permanent --delete-service=jriver &>/dev/null
  1253. firewall_cmd --reload &>/dev/null
  1254. fi
  1255. elif hash ufw 2>/dev/null; then
  1256. firewall_cmd="firewall_cmd delete allow jriver"
  1257. debug "$firewall_cmd" || firewall_cmd+=" &>/dev/null"
  1258. eval "$firewall_cmd"
  1259. [[ -f "/etc/ufw/applications.d/jriver" ]] &&
  1260. sudo rm -f /etc/ufw/applications.d/jriver
  1261. elif hash nft 2>/dev/null; then
  1262. sudo nft delete table inet jriver
  1263. fi
  1264. echo "Uninstalling JRiver Media Center package"
  1265. mc_pkg_rm_cmd="pkg_remove $MCPKG"
  1266. debug "$mc_pkg_rm_cmd" || mc_pkg_rm_cmd+=" &>/dev/null"
  1267. if eval "$mc_pkg_rm_cmd"; then
  1268. echo "JRiver Media Center has been completely uninstalled"
  1269. echo "To remove your library files, run: rm -rf $HOME/.jriver"
  1270. elif [[ $? -eq 100 ]]; then
  1271. err "JRiver Media Center package '$MCPKG' is not present"
  1272. err "and was not uninstalled"
  1273. else
  1274. err "Could not remove Media Center package"
  1275. fi
  1276. }
  1277. tests() {
  1278. # To test on Mint/16.04: sudo apt install -y spice-vdagent ca-certificates git; export GIT_SSL_NO_VERIFY=1
  1279. exit $?
  1280. }
  1281. main() {
  1282. debug "Running: ${FUNCNAME[0]}"
  1283. getOS
  1284. # Distro-specific commands
  1285. if [[ "$ID" =~ ^(fedora|centos)$ ]]; then
  1286. pkg_install(){ sudo "$MGR" install -y "$@"; }
  1287. pkg_remove(){ sudo "$MGR" remove -y "$@"; }
  1288. pkg_update(){ sudo "$MGR" makecache; }
  1289. pkg_query(){ rpm -q "$@"; }
  1290. firewall_cmd(){ sudo firewall-cmd "$@"; }
  1291. elif [[ "$ID" =~ ^(debian|ubuntu)$ ]]; then
  1292. pkg_install(){ sudo apt-get install -y -q0 "$@"; }
  1293. pkg_remove(){ sudo apt-get remove --auto-remove -y -q0 "$@"; }
  1294. pkg_update(){ sudo apt-get update -y -q0; }
  1295. pkg_query(){ dpkg -s "$@"; }
  1296. firewall_cmd(){ sudo ufw "$@"; }
  1297. elif [[ "$ID" == "suse" ]]; then
  1298. pkg_install(){ sudo zypper --non-interactive -q install --force --no-confirm "$@"; }
  1299. pkg_remove(){ sudo zypper --non-interactive -q remove --clean-deps "$@"; }
  1300. pkg_update(){ sudo zypper --non-interactive -q refresh jriver; }
  1301. pkg_query(){ rpm -q "$@"; }
  1302. firewall_cmd(){ sudo firewall-cmd "$@"; }
  1303. elif [[ "$ID" == "arch" ]]; then
  1304. pkg_install(){ sudo pacman -Sy --noconfirm "$@"; }
  1305. pkg_remove(){ sudo pacman -Rs --noconfirm "$@"; }
  1306. pkg_update(){ sudo pacman -Syy ; }
  1307. pkg_query(){ sudo pacman -Qs "$@"; }
  1308. firewall_cmd(){ sudo nft -A INPUT "$@"; }
  1309. fi
  1310. parseInput "$@"
  1311. getVersion
  1312. # Set target package name
  1313. if [[ "$ID" =~ ^(fedora|centos|suse)$ ]]; then
  1314. MCPKG="MediaCenter"
  1315. [[ "$VERSION_SOURCE" == "user input" ]] && MCPKG="$MCPKG-$MCVERSION"
  1316. elif [[ "$ID" =~ ^(debian|ubuntu)$ ]]; then
  1317. MCPKG="mediacenter$MVERSION"
  1318. [[ "$VERSION_SOURCE" == "user input" ]] && MCPKG="$MCPKG=$MCVERSION"
  1319. fi
  1320. declare -g MCRPM="$OUTPUTDIR/RPMS/x86_64/MediaCenter-$MCVERSION.x86_64.rpm"
  1321. if (( UNINSTALL_SWITCH )); then
  1322. uninstall
  1323. exit
  1324. fi
  1325. # Some distros need external repos installed for MC libraries
  1326. if [[ "$ID" == "ubuntu" ]]; then
  1327. if ! grep ^deb /etc/apt/sources.list|grep -q universe; then
  1328. echo "Adding universe repository"
  1329. sudo add-apt-repository universe
  1330. fi
  1331. elif [[ "$ID" =~ ^(centos)$ ]] && ! hash dpkg &>/dev/null; then
  1332. echo "Adding EPEL repository"
  1333. installPackage epel-release
  1334. fi
  1335. if (( REPO_INSTALL_SWITCH )); then
  1336. if installMCFromRepo; then
  1337. echo "JRiver Media Center installed successfully from repo"
  1338. symlinkCerts
  1339. migrateLibrary
  1340. restoreLicense
  1341. openFirewall "jriver-mediacenter"
  1342. else
  1343. err "JRiver Media Center installation from repo failed"
  1344. exit 1
  1345. fi
  1346. fi
  1347. if (( BUILD_SWITCH )); then
  1348. installPackage "wget"
  1349. acquireDeb
  1350. if [[ "$TARGET" =~ (centos|fedora|suse) ]]; then
  1351. installPackage "dpkg" "rpm-build"
  1352. buildRPM
  1353. fi
  1354. fi
  1355. if (( LOCAL_INSTALL_SWITCH )); then
  1356. if ([[ "$TARGET" =~ (debian|ubuntu) ]] && installMCDEB) ||
  1357. ([[ "$TARGET" =~ (fedora|centos|suse) ]] &&
  1358. installPackage --skip-check-installed --nogpgcheck "$MCRPM") ||
  1359. ([[ "$TARGET" == "arch" ]] && installMCArch); then
  1360. echo "JRiver Media Center installed successfully from local package"
  1361. else
  1362. err "JRiver Media Center local package installation failed"
  1363. exit 1
  1364. fi
  1365. symlinkCerts
  1366. migrateLibrary
  1367. restoreLicense
  1368. openFirewall "jriver-mediacenter"
  1369. fi
  1370. if (( CREATEREPO_SWITCH )); then
  1371. runCreaterepo
  1372. fi
  1373. if [[ "${#SERVICES[@]}" -gt 0 ]]; then
  1374. declare service
  1375. for service in "${SERVICES[@]}"; do
  1376. if ! "service_$service"; then
  1377. if [[ $? -eq 127 ]]; then
  1378. err "Service $service does not exist, check your service name"
  1379. else
  1380. err "Failed to create service: $service"
  1381. fi
  1382. fi
  1383. done
  1384. fi
  1385. disableCoW
  1386. # for _container in "${CONTAINERS[@]}"; do
  1387. # if ! "_container_$_container"; then
  1388. # if [[ $? -eq 127 ]]; then
  1389. # err "Container $_container does not exist, check your container name"
  1390. # else
  1391. # err "Failed to create container: $_container"
  1392. # fi
  1393. # fi
  1394. # done
  1395. }
  1396. # Quickly turn debugging on (catch for real with getopt in parseInput())
  1397. [[ " $* " =~ ( --debug | -d ) ]] && echo "First Debugging on!" && DEBUG=1
  1398. main "$@"