Updater_RetroDriven.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. #!/bin/bash
  2. # This program is free software: you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation, either version 3 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. # Copyright 2018-2019 Alessandro "Locutus73" Miele
  13. # You can download the latest version of this script from:
  14. # https://github.com/RetroDriven/MiSTer_UnofficialCores
  15. : '
  16. ###### Disclaimer / Legal Information ######
  17. By downloading ROM injected Core/RBF files you are agreeing to the following:
  18. * You are responsible for checking your local laws regarding the use of the ROMs associated with the injected Core/RBF files.
  19. * You are authorized/licensed to own/use the ROMs associated with the injected Core/RBF files that you download.
  20. * You will not distribute any of these files without the appropriate permissions.
  21. * You own the original Arcade PCB for each ROM injected Core/RBF file that you download.
  22. * I take no responsibility for any data loss or anything, use the script at your own risk.
  23. '
  24. # v1.2 - Removed Cores from GitHub. Adjusted script to account for this change
  25. # v1.0 - Changed original Script from Locutus73 as needed
  26. #========= USER OPTIONS =========
  27. #Base directory for all script’s tasks, "/media/fat" for SD root, "/media/usb0" for USB drive root.
  28. BASE_PATH="/media/fat"
  29. #Directories where all core categories will be downloaded.
  30. declare -A CORE_CATEGORY_PATHS
  31. CORE_CATEGORY_PATHS["arcade-cores"]="$BASE_PATH/_RetroDriven"
  32. DELETE_OLD_FILES="true"
  33. DOWNLOAD_NEW_CORES="true"
  34. REMOVE_ARCADE_PREFIX="true"
  35. #EXPERIMENTAL: specifies if the update process must be done with parallel processing; use it at your own risk!
  36. PARALLEL_UPDATE="false"
  37. #========= ADVANCED OPTIONS =========
  38. #ALLOW_INSECURE_SSL="true" will check if SSL certificate verification (see https://curl.haxx.se/docs/sslcerts.html )
  39. #is working (CA certificates installed) and when it's working it will use this feature for safe curl HTTPS downloads,
  40. #otherwise it will use --insecure option for disabling SSL certificate verification.
  41. #If CA certificates aren't installed it's advised to install them (i.e. using security_fixes.sh).
  42. #ALLOW_INSECURE_SSL="false" will never use --insecure option and if CA certificates aren't installed
  43. #any download will fail.
  44. ALLOW_INSECURE_SSL="true"
  45. CURL_RETRY="--connect-timeout 15 --max-time 120 --retry 3 --retry-delay 5"
  46. SCRIPTS_PATH="Scripts"
  47. OLD_SCRIPTS_PATH="#Scripts"
  48. WORK_PATH="/media/fat/$SCRIPTS_PATH/.mister_updater"
  49. #Uncomment this if you want the script to sync the system date and time with a NTP server
  50. #NTP_SERVER="0.pool.ntp.org"
  51. AUTOREBOOT="false"
  52. REBOOT_PAUSE=0
  53. TEMP_PATH="/tmp"
  54. TO_BE_DELETED_EXTENSION="to_be_deleted"
  55. echo
  56. echo "RetroDriven.com - Launching Soon!"
  57. echo
  58. sleep 3
  59. echo "RetroDriven Core Updater - A Festivus for the rest of Us!"
  60. echo
  61. sleep 5
  62. #========= CODE STARTS HERE =========
  63. ORIGINAL_SCRIPT_PATH="$0"
  64. if [ "$ORIGINAL_SCRIPT_PATH" == "bash" ]
  65. then
  66. ORIGINAL_SCRIPT_PATH=$(ps | grep "^ *$PPID " | grep -o "[^ ]*$")
  67. fi
  68. INI_PATH=${ORIGINAL_SCRIPT_PATH%.*}.ini
  69. if [ -f $INI_PATH ]
  70. then
  71. eval "$(cat $INI_PATH | tr -d '\r')"
  72. fi
  73. if [ -d "${BASE_PATH}/${OLD_SCRIPTS_PATH}" ] && [ ! -d "${BASE_PATH}/${SCRIPTS_PATH}" ]
  74. then
  75. mv "${BASE_PATH}/${OLD_SCRIPTS_PATH}" "${BASE_PATH}/${SCRIPTS_PATH}"
  76. echo "Moved"
  77. echo "${BASE_PATH}/${OLD_SCRIPTS_PATH}"
  78. echo "to"
  79. echo "${BASE_PATH}/${SCRIPTS_PATH}"
  80. echo "please relaunch the script."
  81. exit 3
  82. fi
  83. SSL_SECURITY_OPTION=""
  84. curl $CURL_RETRY -q https://retrodriven.com &>/dev/null
  85. case $? in
  86. 0)
  87. ;;
  88. 60)
  89. if [ "$ALLOW_INSECURE_SSL" == "true" ]
  90. then
  91. SSL_SECURITY_OPTION="--insecure"
  92. else
  93. echo "CA certificates need"
  94. echo "to be fixed for"
  95. echo "using SSL certificate"
  96. echo "verification."
  97. echo "Please fix them i.e."
  98. echo "using security_fixes.sh"
  99. exit 2
  100. fi
  101. ;;
  102. *)
  103. echo "No Internet connection"
  104. exit 1
  105. ;;
  106. esac
  107. ## sync with a public time server
  108. if [[ -n "${NTP_SERVER}" ]] ; then
  109. echo "Syncing date and time with"
  110. echo "${NTP_SERVER}"
  111. # (-b) force time reset, (-s) write output to syslog, (-u) use
  112. # unprivileged port for outgoing packets to workaround firewalls
  113. ntpdate -b -s -u "${NTP_SERVER}"
  114. echo
  115. fi
  116. mkdir -p "${CORE_CATEGORY_PATHS[@]}"
  117. declare -A NEW_CORE_CATEGORY_PATHS
  118. if [ "$DOWNLOAD_NEW_CORES" != "true" ] && [ "$DOWNLOAD_NEW_CORES" != "false" ] && [ "$DOWNLOAD_NEW_CORES" != "" ]
  119. then
  120. for idx in "${!CORE_CATEGORY_PATHS[@]}"; do
  121. NEW_CORE_CATEGORY_PATHS[$idx]=$(echo ${CORE_CATEGORY_PATHS[$idx]} | sed "s/$(echo $BASE_PATH | sed 's/\//\\\//g')/$(echo $BASE_PATH | sed 's/\//\\\//g')\/$DOWNLOAD_NEW_CORES/g")
  122. done
  123. mkdir -p "${NEW_CORE_CATEGORY_PATHS[@]}"
  124. fi
  125. CORE_URLS=$(curl $CURL_RETRY $SSL_SECURITY_OPTION -sLf "https://github.com/RetroDriven/MiSTer_UnofficialCores/wiki" | grep -io '\(https://retrodriven.com/Mister_Cores/[a-zA-Z0-9./_-]*\)\|\(user-content-[a-z-]*\)')
  126. CORE_CATEGORY="-"
  127. SD_INSTALLER_PATH=""
  128. REBOOT_NEEDED="false"
  129. CORE_CATEGORIES_FILTER=""
  130. GOOD_CORES=""
  131. if [ "$GOOD_CORES_URL" != "" ]
  132. then
  133. GOOD_CORES=$(curl $CURL_RETRY $SSL_SECURITY_OPTION -sLf "$GOOD_CORES_URL")
  134. fi
  135. function checkCoreURL {
  136. echo "Checking $(echo $CORE_URL | sed 's/.*\///g' | sed 's/_MiSTer//gI')"
  137. [ "${SSH_CLIENT}" != "" ] && echo "URL: $CORE_URL"
  138. CORE_URL+="/releases/"
  139. RELEASE_URLS=$(curl $CURL_RETRY $SSL_SECURITY_OPTION -sLf "$CORE_URL" | grep -o '"[a-zA-Z0-9./_-]*_[0-9]\{8\}[a-zA-Z]\?\(\.rbf\|\.rar\)\?')
  140. MAX_VERSION=""
  141. MAX_RELEASE_URL=""
  142. GOOD_CORE_VERSION=""
  143. for RELEASE_URL in $RELEASE_URLS; do
  144. RELEASE_URL=$CORE_URL${RELEASE_URL#\"}
  145. if echo "$RELEASE_URL" | grep -q "SharpMZ"
  146. then
  147. RELEASE_URL=$(echo "$RELEASE_URL" | grep '\.rbf$')
  148. fi
  149. if echo "$RELEASE_URL" | grep -q "Atari800"
  150. then
  151. if [ "$CORE_CATEGORY" == "cores" ]
  152. then
  153. RELEASE_URL=$(echo "$RELEASE_URL" | grep '800_[0-9]\{8\}[a-zA-Z]\?\.rbf$')
  154. else
  155. RELEASE_URL=$(echo "$RELEASE_URL" | grep '5200_[0-9]\{8\}[a-zA-Z]\?\.rbf$')
  156. fi
  157. fi
  158. CURRENT_VERSION=$(echo "$RELEASE_URL" | grep -o '[0-9]\{8\}[a-zA-Z]\?')
  159. if [ "$GOOD_CORES" != "" ]
  160. then
  161. GOOD_CORE_VERSION=$(echo "$GOOD_CORES" | grep -wo "$(echo "$RELEASE_URL" | sed 's/.*\///g')" | grep -o '[0-9]\{8\}[a-zA-Z]\?')
  162. if [ "$GOOD_CORE_VERSION" != "" ]
  163. then
  164. MAX_VERSION=$CURRENT_VERSION
  165. MAX_RELEASE_URL=$RELEASE_URL
  166. break
  167. fi
  168. fi
  169. if [[ "$CURRENT_VERSION" > "$MAX_VERSION" ]]
  170. then
  171. MAX_VERSION=$CURRENT_VERSION
  172. MAX_RELEASE_URL=$RELEASE_URL
  173. fi
  174. done
  175. FILE_NAME=$(echo "$MAX_RELEASE_URL" | sed 's/.*\///g')
  176. if [ "$CORE_CATEGORY" == "arcade-cores" ] && [ $REMOVE_ARCADE_PREFIX == "true" ]
  177. then
  178. FILE_NAME=$(echo "$FILE_NAME" | sed 's/Arcade-//gI')
  179. fi
  180. BASE_FILE_NAME=$(echo "$FILE_NAME" | sed 's/_[0-9]\{8\}.*//g')
  181. CURRENT_DIRS="${CORE_CATEGORY_PATHS[$CORE_CATEGORY]}"
  182. if [ "${NEW_CORE_CATEGORY_PATHS[$CORE_CATEGORY]}" != "" ]
  183. then
  184. CURRENT_DIRS=("$CURRENT_DIRS" "${NEW_CORE_CATEGORY_PATHS[$CORE_CATEGORY]}")
  185. fi
  186. if [ "$CURRENT_DIRS" == "" ]
  187. then
  188. CURRENT_DIRS=("$BASE_PATH")
  189. fi
  190. if [ "$BASE_FILE_NAME" == "MiSTer" ] || [ "$BASE_FILE_NAME" == "menu" ] || { echo "$CORE_URL" | grep -q "SD-Installer"; }
  191. then
  192. mkdir -p "$WORK_PATH"
  193. CURRENT_DIRS=("$WORK_PATH")
  194. fi
  195. CURRENT_LOCAL_VERSION=""
  196. MAX_LOCAL_VERSION=""
  197. for CURRENT_DIR in "${CURRENT_DIRS[@]}"
  198. do
  199. for CURRENT_FILE in "$CURRENT_DIR/$BASE_FILE_NAME"*
  200. do
  201. if [ -f "$CURRENT_FILE" ]
  202. then
  203. if echo "$CURRENT_FILE" | grep -q "$BASE_FILE_NAME\_[0-9]\{8\}[a-zA-Z]\?\(\.rbf\|\.rar\)\?$"
  204. then
  205. CURRENT_LOCAL_VERSION=$(echo "$CURRENT_FILE" | grep -o '[0-9]\{8\}[a-zA-Z]\?')
  206. if [ "$GOOD_CORE_VERSION" != "" ]
  207. then
  208. if [ "$CURRENT_LOCAL_VERSION" == "$GOOD_CORE_VERSION" ]
  209. then
  210. MAX_LOCAL_VERSION=$CURRENT_LOCAL_VERSION
  211. else
  212. if [ "$MAX_LOCAL_VERSION" == "" ]
  213. then
  214. MAX_LOCAL_VERSION="00000000"
  215. fi
  216. if [ $DELETE_OLD_FILES == "true" ]
  217. then
  218. mv "${CURRENT_FILE}" "${CURRENT_FILE}.${TO_BE_DELETED_EXTENSION}" > /dev/null 2>&1
  219. fi
  220. fi
  221. else
  222. if [[ "$CURRENT_LOCAL_VERSION" > "$MAX_LOCAL_VERSION" ]]
  223. then
  224. MAX_LOCAL_VERSION=$CURRENT_LOCAL_VERSION
  225. fi
  226. if [[ "$MAX_VERSION" > "$CURRENT_LOCAL_VERSION" ]] && [ $DELETE_OLD_FILES == "true" ]
  227. then
  228. # echo "Moving $(echo ${CURRENT_FILE} | sed 's/.*\///g')"
  229. mv "${CURRENT_FILE}" "${CURRENT_FILE}.${TO_BE_DELETED_EXTENSION}" > /dev/null 2>&1
  230. fi
  231. fi
  232. fi
  233. fi
  234. done
  235. if [ "$MAX_LOCAL_VERSION" != "" ]
  236. then
  237. break
  238. fi
  239. done
  240. if [[ "$MAX_VERSION" > "$MAX_LOCAL_VERSION" ]]
  241. then
  242. if [ "$DOWNLOAD_NEW_CORES" != "false" ] || [ "$MAX_LOCAL_VERSION" != "" ] || [ "$BASE_FILE_NAME" == "MiSTer" ] || [ "$BASE_FILE_NAME" == "menu" ] || { echo "$CORE_URL" | grep -q "SD-Installer"; }
  243. then
  244. echo "Downloading $FILE_NAME"
  245. [ "${SSH_CLIENT}" != "" ] && echo "URL: $MAX_RELEASE_URL"
  246. if curl $CURL_RETRY $SSL_SECURITY_OPTION -L "$MAX_RELEASE_URL" -o "$CURRENT_DIR/$FILE_NAME"
  247. then
  248. if [ ${DELETE_OLD_FILES} == "true" ]
  249. then
  250. echo "Deleting old ${BASE_FILE_NAME} files"
  251. rm "${CURRENT_DIR}/${BASE_FILE_NAME}"*.${TO_BE_DELETED_EXTENSION} > /dev/null 2>&1
  252. fi
  253. if [ $BASE_FILE_NAME == "MiSTer" ] || [ $BASE_FILE_NAME == "menu" ]
  254. then
  255. DESTINATION_FILE=$(echo "$MAX_RELEASE_URL" | sed 's/.*\///g' | sed 's/_[0-9]\{8\}[a-zA-Z]\{0,1\}//g')
  256. echo "Moving $DESTINATION_FILE"
  257. rm "/media/fat/$DESTINATION_FILE" > /dev/null 2>&1
  258. mv "$CURRENT_DIR/$FILE_NAME" "/media/fat/$DESTINATION_FILE"
  259. touch "$CURRENT_DIR/$FILE_NAME"
  260. REBOOT_NEEDED="true"
  261. fi
  262. if echo "$CORE_URL" | grep -q "SD-Installer"
  263. then
  264. SD_INSTALLER_PATH="$CURRENT_DIR/$FILE_NAME"
  265. fi
  266. if [ "$CORE_CATEGORY" == "arcade-cores" ]
  267. then
  268. OLD_IFS="$IFS"
  269. IFS="|"
  270. for ARCADE_ALT_PATH in $ARCADE_ALT_PATHS
  271. do
  272. for ARCADE_ALT_DIR in "$ARCADE_ALT_PATH/_$BASE_FILE_NAME"*
  273. do
  274. if [ -d "$ARCADE_ALT_DIR" ]
  275. then
  276. echo "Updating $(echo $ARCADE_ALT_DIR | sed 's/.*\///g')"
  277. if [ $DELETE_OLD_FILES == "true" ]
  278. then
  279. for ARCADE_HACK_CORE in "$ARCADE_ALT_DIR/"*.rbf
  280. do
  281. if [ -f "$ARCADE_HACK_CORE" ] && { echo "$ARCADE_HACK_CORE" | grep -q "$BASE_FILE_NAME\_[0-9]\{8\}[a-zA-Z]\?\.rbf$"; }
  282. then
  283. rm "$ARCADE_HACK_CORE" > /dev/null 2>&1
  284. fi
  285. done
  286. fi
  287. cp "$CURRENT_DIR/$FILE_NAME" "$ARCADE_ALT_DIR/"
  288. fi
  289. done
  290. done
  291. IFS="$OLD_IFS"
  292. fi
  293. else
  294. echo "${FILE_NAME} download failed"
  295. rm "${CURRENT_DIR}/${FILE_NAME}" > /dev/null 2>&1
  296. if [ ${DELETE_OLD_FILES} == "true" ]
  297. then
  298. echo "Restoring old ${BASE_FILE_NAME} files"
  299. for FILE_TO_BE_RESTORED in "${CURRENT_DIR}/${BASE_FILE_NAME}"*.${TO_BE_DELETED_EXTENSION}
  300. do
  301. mv "${FILE_TO_BE_RESTORED}" "${FILE_TO_BE_RESTORED%.${TO_BE_DELETED_EXTENSION}}" > /dev/null 2>&1
  302. done
  303. fi
  304. fi
  305. sync
  306. else
  307. echo "New core: $FILE_NAME"
  308. fi
  309. else
  310. echo "Core is up to date!"
  311. fi
  312. echo ""
  313. }
  314. for CORE_URL in $CORE_URLS; do
  315. if [[ $CORE_URL == https://* ]]
  316. then
  317. if [ "$REPOSITORIES_FILTER" == "" ] || { echo "$CORE_URL" | grep -qi "$REPOSITORIES_FILTER"; } || { echo "$CORE_CATEGORY" | grep -qi "$CORE_CATEGORIES_FILTER"; }
  318. then
  319. if echo "$CORE_URL" | grep -qE "(SD-Installer)|(/Main_MiSTer$)|(/Menu_MiSTer$)"
  320. then
  321. checkCoreURL
  322. else
  323. [ "$PARALLEL_UPDATE" == "true" ] && { echo "$(checkCoreURL)"$'\n' & } || checkCoreURL
  324. fi
  325. fi
  326. else
  327. CORE_CATEGORY=$(echo "$CORE_URL" | sed 's/user-content-//g')
  328. if [ "$CORE_CATEGORY" == "" ]
  329. then
  330. CORE_CATEGORY="-"
  331. fi
  332. if [ "$CORE_CATEGORY" == "computer-cores" ]
  333. then
  334. CORE_CATEGORY="cores"
  335. fi
  336. fi
  337. done
  338. wait
  339. echo "All RetroDriven Cores have been Updated!"
  340. echo
  341. echo "Please visit RetroDriven.com for all of your MiSTer and Retro News!"
  342. echo
  343. sleep 5
  344. if [ $REBOOT_NEEDED == "true" ]
  345. then
  346. if [ $AUTOREBOOT == "true" ]
  347. then
  348. echo "Rebooting in $REBOOT_PAUSE seconds"
  349. sleep $REBOOT_PAUSE
  350. reboot now
  351. else
  352. echo "You should reboot"
  353. fi
  354. fi
  355. exit 0