1#!/bin/bash
2
3#
4# This script will upload the packaged RetroArch cores to a WiiU running
5# FTPiiU or FTPiiU Anywhere
6#
7# IMPORTANT: This script assumes the following structur
8#
9# WARNING: I experienced corrupt uploads when using Dimok's FTPiiU. You
10# probably want to use FIX94's FTPiiU Anywhere.
11#
12# After uploading everything, the script will re-download the RPX files and
13# compare their hash and print an error if the file was corrupted.
14#
15# The WiiU's IP address can be specified by either setting the WIIU_IP_ADDRESS
16# environment variable, or by configuring the wiiu-devel.properties file
17# (see the file wiiu-devel.properties.template for instructions).
18#
19
20# The path to the parent directory of your retroarch/ and wiiu/ folders, as
21# visible in FTPiiU.
22
23RETRO_ROOT=sd
24
25here=$(pwd)
26cd $(dirname $(readlink -f $0))
27if [ -e ../wiiu-devel.properties ]; then
28  . ../wiiu-devel.properties
29fi
30
31if [ -z "$WIIU_IP_ADDRESS" ]; then
32  echo "WIIU_IP_ADDRESS not set. Set up ../wiiu-devel.properties or set the"
33  echo "environment variable."
34  cd $here
35  exit 1
36fi
37
38filesToUpload()
39{
40  find . -type f \( -name "*.rpx" -o -name "*.xml" -o -name "*.png" -o -name "*.info" \)
41}
42
43cd ../pkg/wiiu/rpx
44
45# First, delete any previous *.remote files from previous uploads.
46find . -name '*.remote' | xargs rm -f {}
47
48# Now generate the FTP command list
49rm -f .ftpcommands
50
51# Now create the directory structure
52for dir in $(find . -type "d"); do
53  if [ "$dir" == "." ]; then
54    continue
55  fi
56  echo "mkdir $dir" >> .ftpcommands
57done
58
59# Delete and re-upload the files we just built
60for cmd in rm put; do
61  filesToUpload | xargs -L 1 echo "$cmd" >> .ftpcommands
62done
63
64# Lastly, download the RPX files as *.rpx.remote files
65for rpx in $(find . -name "*.rpx"); do
66  echo "get $rpx ${rpx}.remote" >> .ftpcommands
67done
68
69# The command list is done. Time to execute it.
70ftp -n $WIIU_IP_ADDRESS <<END_SCRIPT
71quote USER wiiu
72quote PASS wiiu
73passive
74bin
75cd $RETRO_ROOT
76
77$(cat .ftpcommands)
78END_SCRIPT
79
80rm -f .ftpcommands
81
82errors=0
83# Now, we compare the hashes of the original file and the file we got back,
84# and print an error if the hashes don't match.
85for remote in $(find . -name "*.remote"); do
86  originalFile=$(echo $remote |sed 's/\.remote//')
87  originalHash=$(md5sum -b $originalFile |awk '{print $1}')
88  remoteHash=$(md5sum -b $remote |awk '{print $1}')
89
90  if [ "$originalHash" != "$remoteHash" ]; then
91    echo "ERROR: $remote was corrupted during upload."
92    errors=$((errors+1))
93  fi
94done
95
96cd $here
97
98if [ $errors -ne 0 ]; then
99  echo "Upload failed. $errors files failed to upload correctly."
100  exit 1
101fi
102
103echo "RetroArch build uploaded and validated successfully."
104exit 0
105