Updater_RetroDriven.sh 13 KB

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