Install Battle.net quickly and easily, even for a secondary user
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bn3t.sh 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #!/bin/bash
  2. ## Copyright © 2018 Bret Human
  3. ## https://cynicaloptimist.me/
  4. ##
  5. ## Documentation at:
  6. ## https://mage.cynicaloptimist.me/en/bn3t
  7. ## https://psi.cynicaloptimist.me/Caffarius/bn3t/
  8. ##
  9. ## For questions or comments write:
  10. ## info@cynicaloptimist.me
  11. #####################################################################
  12. # bn3t
  13. script_ver="0.2"
  14. page="https://mage.cynicaloptimist.me/en/bn3t"
  15. # author: Bret Human
  16. # date: Dec 6, 2018
  17. #
  18. #####################################################################
  19. #
  20. # A wrapper for wrappers, used to install/manage/own a Battle.net
  21. # installation within Wine in a Linux environment.
  22. #
  23. # Extra handy for running Battle.net under a secondary account for
  24. # added security. Can be used for lots of zany wine needs, like
  25. # spawning a multitude of Wine prefixes and installing Battle.net in
  26. # all of them.
  27. #
  28. # This is the first of two scripts. It is meant to be the sanitizer
  29. # crew and the firing squad for bn3t-run. bn3t-run must be installed.
  30. #
  31. # Install bn3t-run for your desired user with:
  32. # ./bn3t.sh -u username -i
  33. #
  34. # Then you can prep the environment and install Battle.net via:
  35. # ./bn3t.sh -u username -bxs
  36. #
  37. # Or I guess you could manually download the script from:
  38. dl_addr="https://psi.cynicaloptimist.me/Caffarius/bn3t/raw/branch/master/bn3t-run.sh"
  39. #####################################################################
  40. #
  41. # This is free software; you are free to change and redistribute it.
  42. # There is NO WARRANTY of any kind.
  43. #
  44. #####################################################################
  45. runas_user="`whoami`"
  46. app_name="Battle.net"
  47. run_cmd="bn3t-run.sh"
  48. check_wait="3"
  49. usage="bn3t, version ${script_ver}
  50. Usage: ./`basename ${0}` -g (game) -u (user) -p (prefix) -a (arch) -dvksbwtnchxi
  51. -g | Launches the game of your choice
  52. -u | Runs ${app_name} as the user specified
  53. -p | Selects the location of the Wine prefix
  54. -a | Selects the architecture to run as (32 | 64)
  55. -d | Debug messages on (turns verbose on)
  56. -v | Verbose messages on
  57. -k | Kill all processes for a user (not current user or root)
  58. -s | Runs ${app_name} setup
  59. -b | Runs wineboot
  60. -w | Runs winetricks
  61. -m | Runs Twitch Client (for /M/ods - I'm running out of letters)
  62. -t | Attempts to launch to the system tray
  63. -n | No BS mode - doesn't launch an app (for boot/tricks/console safety)
  64. -c | Launch a console session as specified user (can launch Xapps)
  65. -h | Help (this message)
  66. -x | Force upgrade DXVK
  67. -i | Install the bn3t-run script for the specified user - mandantory
  68. See more documentation at: ${page}"
  69. # Catch CTRL+C and cleanup
  70. trap "echo '' && inform '---------------------------------' && inform 'Caught CTRL+C' && check" 2
  71. ## Define our functions
  72. function refresh {
  73. app_dir="/home/${runas_user}/.script/"
  74. app_check="ps -ef | grep ${runas_user} | grep Battle.net.exe | grep -v grep"
  75. }
  76. function debug () {
  77. if [ "${app_debug}" ]; then
  78. echo "-D- ${1}"
  79. fi
  80. }
  81. function inform () {
  82. if [ "${app_verbose}" ]; then
  83. echo "--- ${1}"
  84. fi
  85. }
  86. function clean {
  87. debug "Clean"
  88. # Var
  89. unset script_ver
  90. unset page
  91. unset runas_user
  92. unset app_name
  93. unset run_cmd
  94. unset check_wait
  95. unset usage
  96. unset app_dir
  97. unset app_check
  98. unset app_debug
  99. unset app_verbose
  100. unset launch_opt
  101. unset nobs_mode
  102. unset dl_addr
  103. # Func
  104. unset refresh
  105. unset debug
  106. unset inform
  107. unset check
  108. }
  109. function die () {
  110. debug "die"
  111. echo "Cleaning up..."
  112. if [ "`whoami`" == "${runas_user}" ]; then
  113. debug "Launched as original user, not killing processes"
  114. else
  115. if [ "${runas_user}" == "root" ]; then
  116. debug "Root? Madness. Just removing xhost then..."
  117. inform "`xhost -SI:localuser:"${runas_user}"`"
  118. else
  119. debug "Launched as ${runas_user}, killing processes"
  120. sudo -u "${runas_user}" -- bash -c "kill -9 -1"
  121. inform "`xhost -SI:localuser:"${runas_user}"`"
  122. fi
  123. fi
  124. clean
  125. unset clean
  126. unset fizzle
  127. if [ "$1" ]; then
  128. unset die && exit $1
  129. else
  130. echo "...what?"
  131. unset die && exit 5
  132. fi
  133. exit 5
  134. }
  135. function fizzle () {
  136. debug "fizzle" # https://mtg.gamepedia.com/Fizzle
  137. echo "Exiting..."
  138. clean
  139. unset clean
  140. unset die
  141. if [ "$1" ]; then
  142. unset fizzle && exit $1
  143. else
  144. echo "...what?"
  145. unset fizzle && exit 5
  146. fi
  147. exit 5
  148. }
  149. function check {
  150. debug "check"
  151. i="0"
  152. while [ "${i}" -lt "${check_wait}" ]; do
  153. if [ "$(eval ${app_check})" ]; then
  154. inform "${app_name} is still running. Waiting to see if we need to murder... $[ ${check_wait} - ${i} ]"
  155. sleep 1
  156. else
  157. unset i
  158. die 0
  159. fi
  160. i=$[ $i + 1 ]
  161. done
  162. unset i
  163. }
  164. ## Pick up any arguments provided
  165. # None are required, we'll use default values.
  166. launch_opt=""
  167. while getopts ":g:G:u:U:p:P:a:A:dDvVkKsSbBwWtTnNcChHxXiImM" option; do
  168. case "${option}" in
  169. "k"|"K") die 0;;
  170. "g"|"G") launch_opt="${launch_opt} -g ${OPTARG}";;
  171. "p"|"P") launch_opt="${launch_opt} -p ${OPTARG}";;
  172. "a"|"A") launch_opt="${launch_opt} -a ${OPTARG}";;
  173. "d"|"D")
  174. if [ ! "${app_debug}" ]; then # Let's not do this too much...
  175. app_debug="y"
  176. launch_opt=" -d${launch_opt}"
  177. debug "Debug enabled"
  178. if [ ! "${app_verbose}" ]; then
  179. debug "`realpath "${0}"` version ${script_ver} running as `whoami`"
  180. fi
  181. app_verbose="y"
  182. debug ""
  183. debug "This is free software; you are free to change and redistribute it."
  184. debug "There is NO WARRANTY of any kind."
  185. debug ""
  186. debug "Documentation at: ${page}"
  187. debug ""
  188. fi
  189. ;;
  190. "w"|"W")
  191. inform "Winetricks mode"
  192. launch_opt="${launch_opt} -w"
  193. ;;
  194. "s"|"S")
  195. inform "Setup mode"
  196. nobs_mode="y"
  197. launch_opt="${launch_opt} -s"
  198. ;;
  199. "x"|"X")
  200. inform "Force upgrading dxvk"
  201. launch_opt="${launch_opt} -x"
  202. ;;
  203. "b"|"B")
  204. inform "Wineboot mode"
  205. launch_opt="${launch_opt} -b"
  206. ;;
  207. "m"|"M")
  208. inform "Twitch mode"
  209. launch_opt="${launch_opt} -m"
  210. ;;
  211. "n"|"N")
  212. inform "No BS mode"
  213. nobs_mode="y"
  214. launch_opt="${launch_opt} -n"
  215. ;;
  216. "c"|"C")
  217. inform "Console requested"
  218. nobs_mode="y"
  219. launch_opt="${launch_opt} -c"
  220. ;;
  221. "v"|"V")
  222. if [ ! "${app_debug}" ]; then
  223. if [ ! "${app_verbose}" ]; then
  224. app_verbose="y"
  225. inform "Verbose enabled"
  226. inform "`realpath "${0}"` version ${script_ver} running as `whoami`"
  227. launch_opt=" -v${launch_opt}"
  228. fi
  229. fi
  230. ;;
  231. "t"|"T")
  232. debug "Hiding to system tray"
  233. launch_opt="${launch_opt} -t"
  234. ;;
  235. "u"|"U")
  236. debug "Setting user to ${OPTARG}"
  237. runas_user="${OPTARG}"
  238. ;;
  239. "i"|"I")
  240. debug "User-side install requested"
  241. refresh
  242. if [ "`whoami`" == "${runas_user}" ]; then
  243. debug "Running as original user, just downloading it"
  244. mkdir -p "${app_dir}" ||
  245. (echo "Failed to make directory ${app_dir}" && fizzle 1)
  246. wget "${dl_addr}" -O "${app_dir}${run_cmd}" ||
  247. (echo "Failed to download script from ${dl_addr}" && fizzle 1)
  248. chmod 700 "${app_dir}${run_cmd}" ||
  249. (echo "Failed to change permissions on ${app_dir}${run_cmd}" && fizzle 1)
  250. else
  251. debug "Running as ${runas_user}, switching and downloading"
  252. sudo -u "${runas_user}" -- mkdir -p "${app_dir}" ||
  253. (echo "Failed to make directory ${app_dir}" && fizzle 1)
  254. sudo -u "${runas_user}" -- wget "${dl_addr}" -O "${app_dir}${run_cmd}" ||
  255. (echo "Failed to download script from ${dl_addr}" && fizzle 1)
  256. sudo -u "${runas_user}" -- chmod 700 "${app_dir}${run_cmd}" ||
  257. (echo "Failed to change permissions on ${app_dir}${run_cmd}" && fizzle 1)
  258. fi
  259. echo "Installed successfully."
  260. fizzle 0
  261. ;;
  262. "h"|"H")
  263. echo "${usage}"
  264. fizzle 0
  265. ;;
  266. * ) echo "Unknown option: -${OPTARG}"
  267. echo "${usage}"
  268. fizzle 1
  269. ;;
  270. esac
  271. done
  272. debug "launch_opt:${launch_opt}"
  273. ## Pass our knowledge on.
  274. refresh
  275. debug "run_app"
  276. if [ "`whoami`" == "${runas_user}" ]; then
  277. debug "Launching as original user, going native"
  278. ${app_dir}${run_cmd}${launch_opt}
  279. else
  280. debug "Launching as ${runas_user}, checking xhost"
  281. if [ ! "`xhost | grep ${runas_user}`" ]; then
  282. inform "`xhost +SI:localuser:"${runas_user}"`"
  283. fi
  284. sudo -u "${runas_user}" -- dbus-launch ${app_dir}${run_cmd}${launch_opt}
  285. fi
  286. ## If we still exist, clean up.
  287. debug "Final Notice"
  288. if [ "${app_check}" ]; then
  289. check
  290. fizzle 0
  291. fi