Updater_RetroDriven.sh 12 KB

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