Updater_RetroDriven.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 ROM injected Core/RBF files.
  19. * You are authorized to own a license to use any the ROM injected Core/RBF file 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 files 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. sleep 3
  55. echo
  56. echo "RetroDriven.com - Launching Soon!"
  57. echo
  58. echo "RetroDriven Core Updater - A Festivus for the rest of Us!"
  59. echo
  60. sleep 3
  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://github.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/wiki" | grep -io '\(https://github.com/RetroDriven/MiSTer_UnofficialCores/tree/master/Arcade_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. if echo "$CORE_URL" | grep -q "SD-Installer"
  138. then
  139. RELEASES_URL="$CORE_URL"
  140. else
  141. RELEASES_URL=https://github.com$(curl $CURL_RETRY $SSL_SECURITY_OPTION -sLf "$CORE_URL" | grep -o '/RetroDriven/[a-zA-Z0-9./_-]*/tree/master/[a-zA-Z0-9./_-]*/releases' | head -n1)
  142. fi
  143. RELEASE_URLS=$(curl $CURL_RETRY $SSL_SECURITY_OPTION -sLf "$RELEASES_URL" | grep -o '/RetroDriven/[a-zA-Z0-9./_-]*_[0-9]\{8\}[a-zA-Z]\?\(\.rbf\|\.rar\)\?')
  144. MAX_VERSION=""
  145. MAX_RELEASE_URL=""
  146. GOOD_CORE_VERSION=""
  147. for RELEASE_URL in $RELEASE_URLS; do
  148. if echo "$RELEASE_URL" | grep -q "SharpMZ"
  149. then
  150. RELEASE_URL=$(echo "$RELEASE_URL" | grep '\.rbf$')
  151. fi
  152. if echo "$RELEASE_URL" | grep -q "Atari800"
  153. then
  154. if [ "$CORE_CATEGORY" == "cores" ]
  155. then
  156. RELEASE_URL=$(echo "$RELEASE_URL" | grep '800_[0-9]\{8\}[a-zA-Z]\?\.rbf$')
  157. else
  158. RELEASE_URL=$(echo "$RELEASE_URL" | grep '5200_[0-9]\{8\}[a-zA-Z]\?\.rbf$')
  159. fi
  160. fi
  161. CURRENT_VERSION=$(echo "$RELEASE_URL" | grep -o '[0-9]\{8\}[a-zA-Z]\?')
  162. if [ "$GOOD_CORES" != "" ]
  163. then
  164. GOOD_CORE_VERSION=$(echo "$GOOD_CORES" | grep -wo "$(echo "$RELEASE_URL" | sed 's/.*\///g')" | grep -o '[0-9]\{8\}[a-zA-Z]\?')
  165. if [ "$GOOD_CORE_VERSION" != "" ]
  166. then
  167. MAX_VERSION=$CURRENT_VERSION
  168. MAX_RELEASE_URL=$RELEASE_URL
  169. break
  170. fi
  171. fi
  172. if [[ "$CURRENT_VERSION" > "$MAX_VERSION" ]]
  173. then
  174. MAX_VERSION=$CURRENT_VERSION
  175. MAX_RELEASE_URL=$RELEASE_URL
  176. fi
  177. done
  178. FILE_NAME=$(echo "$MAX_RELEASE_URL" | sed 's/.*\///g')
  179. if [ "$CORE_CATEGORY" == "arcade-cores" ] && [ $REMOVE_ARCADE_PREFIX == "true" ]
  180. then
  181. FILE_NAME=$(echo "$FILE_NAME" | sed 's/Arcade-//gI')
  182. fi
  183. BASE_FILE_NAME=$(echo "$FILE_NAME" | sed 's/_[0-9]\{8\}.*//g')
  184. CURRENT_DIRS="${CORE_CATEGORY_PATHS[$CORE_CATEGORY]}"
  185. if [ "${NEW_CORE_CATEGORY_PATHS[$CORE_CATEGORY]}" != "" ]
  186. then
  187. CURRENT_DIRS=("$CURRENT_DIRS" "${NEW_CORE_CATEGORY_PATHS[$CORE_CATEGORY]}")
  188. fi
  189. if [ "$CURRENT_DIRS" == "" ]
  190. then
  191. CURRENT_DIRS=("$BASE_PATH")
  192. fi
  193. if [ "$BASE_FILE_NAME" == "MiSTer" ] || [ "$BASE_FILE_NAME" == "menu" ] || { echo "$CORE_URL" | grep -q "SD-Installer"; }
  194. then
  195. mkdir -p "$WORK_PATH"
  196. CURRENT_DIRS=("$WORK_PATH")
  197. fi
  198. CURRENT_LOCAL_VERSION=""
  199. MAX_LOCAL_VERSION=""
  200. for CURRENT_DIR in "${CURRENT_DIRS[@]}"
  201. do
  202. for CURRENT_FILE in "$CURRENT_DIR/$BASE_FILE_NAME"*
  203. do
  204. if [ -f "$CURRENT_FILE" ]
  205. then
  206. if echo "$CURRENT_FILE" | grep -q "$BASE_FILE_NAME\_[0-9]\{8\}[a-zA-Z]\?\(\.rbf\|\.rar\)\?$"
  207. then
  208. CURRENT_LOCAL_VERSION=$(echo "$CURRENT_FILE" | grep -o '[0-9]\{8\}[a-zA-Z]\?')
  209. if [ "$GOOD_CORE_VERSION" != "" ]
  210. then
  211. if [ "$CURRENT_LOCAL_VERSION" == "$GOOD_CORE_VERSION" ]
  212. then
  213. MAX_LOCAL_VERSION=$CURRENT_LOCAL_VERSION
  214. else
  215. if [ "$MAX_LOCAL_VERSION" == "" ]
  216. then
  217. MAX_LOCAL_VERSION="00000000"
  218. fi
  219. if [ $DELETE_OLD_FILES == "true" ]
  220. then
  221. mv "${CURRENT_FILE}" "${CURRENT_FILE}.${TO_BE_DELETED_EXTENSION}" > /dev/null 2>&1
  222. fi
  223. fi
  224. else
  225. if [[ "$CURRENT_LOCAL_VERSION" > "$MAX_LOCAL_VERSION" ]]
  226. then
  227. MAX_LOCAL_VERSION=$CURRENT_LOCAL_VERSION
  228. fi
  229. if [[ "$MAX_VERSION" > "$CURRENT_LOCAL_VERSION" ]] && [ $DELETE_OLD_FILES == "true" ]
  230. then
  231. # echo "Moving $(echo ${CURRENT_FILE} | sed 's/.*\///g')"
  232. mv "${CURRENT_FILE}" "${CURRENT_FILE}.${TO_BE_DELETED_EXTENSION}" > /dev/null 2>&1
  233. fi
  234. fi
  235. fi
  236. fi
  237. done
  238. if [ "$MAX_LOCAL_VERSION" != "" ]
  239. then
  240. break
  241. fi
  242. done
  243. if [[ "$MAX_VERSION" > "$MAX_LOCAL_VERSION" ]]
  244. then
  245. if [ "$DOWNLOAD_NEW_CORES" != "false" ] || [ "$MAX_LOCAL_VERSION" != "" ] || [ "$BASE_FILE_NAME" == "MiSTer" ] || [ "$BASE_FILE_NAME" == "menu" ] || { echo "$CORE_URL" | grep -q "SD-Installer"; }
  246. then
  247. echo "Downloading $FILE_NAME"
  248. [ "${SSH_CLIENT}" != "" ] && echo "URL: https://github.com$MAX_RELEASE_URL?raw=true"
  249. if curl $CURL_RETRY $SSL_SECURITY_OPTION -L "https://github.com$MAX_RELEASE_URL?raw=true" -o "$CURRENT_DIR/$FILE_NAME"
  250. then
  251. if [ ${DELETE_OLD_FILES} == "true" ]
  252. then
  253. echo "Deleting old ${BASE_FILE_NAME} files"
  254. rm "${CURRENT_DIR}/${BASE_FILE_NAME}"*.${TO_BE_DELETED_EXTENSION} > /dev/null 2>&1
  255. fi
  256. if [ $BASE_FILE_NAME == "MiSTer" ] || [ $BASE_FILE_NAME == "menu" ]
  257. then
  258. DESTINATION_FILE=$(echo "$MAX_RELEASE_URL" | sed 's/.*\///g' | sed 's/_[0-9]\{8\}[a-zA-Z]\{0,1\}//g')
  259. echo "Moving $DESTINATION_FILE"
  260. rm "/media/fat/$DESTINATION_FILE" > /dev/null 2>&1
  261. mv "$CURRENT_DIR/$FILE_NAME" "/media/fat/$DESTINATION_FILE"
  262. touch "$CURRENT_DIR/$FILE_NAME"
  263. REBOOT_NEEDED="true"
  264. fi
  265. if echo "$CORE_URL" | grep -q "SD-Installer"
  266. then
  267. SD_INSTALLER_PATH="$CURRENT_DIR/$FILE_NAME"
  268. fi
  269. if [ "$CORE_CATEGORY" == "arcade-cores" ]
  270. then
  271. OLD_IFS="$IFS"
  272. IFS="|"
  273. for ARCADE_ALT_PATH in $ARCADE_ALT_PATHS
  274. do
  275. for ARCADE_ALT_DIR in "$ARCADE_ALT_PATH/_$BASE_FILE_NAME"*
  276. do
  277. if [ -d "$ARCADE_ALT_DIR" ]
  278. then
  279. echo "Updating $(echo $ARCADE_ALT_DIR | sed 's/.*\///g')"
  280. if [ $DELETE_OLD_FILES == "true" ]
  281. then
  282. for ARCADE_HACK_CORE in "$ARCADE_ALT_DIR/"*.rbf
  283. do
  284. if [ -f "$ARCADE_HACK_CORE" ] && { echo "$ARCADE_HACK_CORE" | grep -q "$BASE_FILE_NAME\_[0-9]\{8\}[a-zA-Z]\?\.rbf$"; }
  285. then
  286. rm "$ARCADE_HACK_CORE" > /dev/null 2>&1
  287. fi
  288. done
  289. fi
  290. cp "$CURRENT_DIR/$FILE_NAME" "$ARCADE_ALT_DIR/"
  291. fi
  292. done
  293. done
  294. IFS="$OLD_IFS"
  295. fi
  296. else
  297. echo "${FILE_NAME} download failed"
  298. rm "${CURRENT_DIR}/${FILE_NAME}" > /dev/null 2>&1
  299. if [ ${DELETE_OLD_FILES} == "true" ]
  300. then
  301. echo "Restoring old ${BASE_FILE_NAME} files"
  302. for FILE_TO_BE_RESTORED in "${CURRENT_DIR}/${BASE_FILE_NAME}"*.${TO_BE_DELETED_EXTENSION}
  303. do
  304. mv "${FILE_TO_BE_RESTORED}" "${FILE_TO_BE_RESTORED%.${TO_BE_DELETED_EXTENSION}}" > /dev/null 2>&1
  305. done
  306. fi
  307. fi
  308. sync
  309. else
  310. echo "New core: $FILE_NAME"
  311. fi
  312. else
  313. echo "Core is up to date!"
  314. fi
  315. echo ""
  316. }
  317. for CORE_URL in $CORE_URLS; do
  318. if [[ $CORE_URL == https://* ]]
  319. then
  320. if [ "$REPOSITORIES_FILTER" == "" ] || { echo "$CORE_URL" | grep -qi "$REPOSITORIES_FILTER"; } || { echo "$CORE_CATEGORY" | grep -qi "$CORE_CATEGORIES_FILTER"; }
  321. then
  322. if echo "$CORE_URL" | grep -qE "(SD-Installer)|(/Main_MiSTer$)|(/Menu_MiSTer$)"
  323. then
  324. checkCoreURL
  325. else
  326. [ "$PARALLEL_UPDATE" == "true" ] && { echo "$(checkCoreURL)"$'\n' & } || checkCoreURL
  327. fi
  328. fi
  329. else
  330. CORE_CATEGORY=$(echo "$CORE_URL" | sed 's/user-content-//g')
  331. if [ "$CORE_CATEGORY" == "" ]
  332. then
  333. CORE_CATEGORY="-"
  334. fi
  335. if [ "$CORE_CATEGORY" == "computer-cores" ]
  336. then
  337. CORE_CATEGORY="cores"
  338. fi
  339. fi
  340. done
  341. wait
  342. echo "All RetroDriven Cores have been Updated!"
  343. echo
  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