1#!/usr/bin/env bash
2# Version 6/27/2012
3
4###############################################################################
5#
6# Copyright (C) 2009-2013 Nintendo.  All rights reserved.
7#
8# These coded instructions, statements, and computer programs contain
9# proprietary information of Nintendo of America Inc. and/or Nintendo
10# Company Ltd., and are protected by Federal copyright law.  They may
11# not be disclosed to third parties or copied or duplicated in any form,
12# in whole or in part, without the prior written consent of Nintendo.
13#
14###############################################################################
15
16# Path to where ImageUploader.exe will extract the WUMAD to.
17WUMAD_EXTRACT_TMP=$TMP/${SESSION_PATH_PREFIX}NHBImageUploader
18
19##################################################################################################
20# Print Usage
21##################################################################################################
22usage()
23{
24	cat <<EOF
25
26$(basename $0) v1.0 - Copyright (c) 2012 Nintendo Co., Ltd.
27
28    Uploads a WUMAD image to one or more devkits suimultaneously.  The WUMAD file
29    is extracted during the first upload and re-used so that the extraction
30    is done only once, then the multiple uploads are performed.
31
32Usage:
33    $(basename $0) -h
34    $(basename $0) [-b bankno] wumad_path ip_address [...]
35    $(basename $0) [-b bankno] -r ip_address [...]
36    $(basename $0) [-b bankno] -r -l ip_file
37
38Options:
39    -b bank_no:     Bank # to upload image to.  Default is bank 10.
40    -l ip_file:     File contianing a list of IP address to upload to.
41                    Must have unix and not DOS style line ends.
42    -r              Reuse the already extracted TMP image.
43    -h              This help screen.
44
45    wumad_path:     Path to a .wumad file.
46    ip_address      First Devkit IP address to upload to.
47    ...             Additional Devkit IP addresses to upload the image to.
48EOF
49
50}
51
52##################################################################################################
53# Options & Help
54##################################################################################################
55
56if [ $# -eq 0 ]
57then
58    usage
59    exit 0
60fi
61
62IMGUPLOADFLAT=0
63skip_extract=0
64enable_workarounds=0
65
66while getopts "b:l:rh" OPT
67do
68    case $OPT in
69      b ) bank_no=$OPTARG;;
70
71      l ) ip_addresses=`cat $OPTARG`;;
72
73      r ) if [ ! -d "$WUMAD_EXTRACT_TMP" ]
74          then
75              echo "ERROR: No extracted WUMAD folder exists!  Please specify a WUMAD file first"
76              exit 2
77          fi
78          skip_extract=1;;
79
80      h ) usage
81          exit 0;;
82
83      \:) echo "$(basename $0): failed: missing argument!"
84          usage
85          exit;;
86      \?) echo "$(basename "$0") failed: invalid option: -$OPTARG"
87          usage
88          exit;;
89    esac
90done
91shift $(($OPTIND-1))
92
93##################################################################################################
94# Script variables
95##################################################################################################
96if [ $skip_extract -eq 0 ]
97then
98    wumad_path=`cygpath --windows "$1"`
99    shift
100else
101    wumad_path="empty"
102fi
103bank_no=${bank_no:-10}
104
105while [ $# -gt 0 ]
106do
107    ip_addresses="$ip_addresses $1"
108    shift
109done
110
111if [ "$wumad_path" == "" -o "$ip_addresses" == "" ]
112then
113    echo "$(basename $0): ERROR: Missing required arguments!"
114    usage
115    exit
116fi
117
118##################################################################################################
119# ImageUploader version check
120##################################################################################################
121check_imageuploader_version ()
122{
123    IMGUPLOADERV=`ImageUploader.exe | grep "Image Uploader by"`
124
125    if [ -z "$IMGUPLOADERV" ]; then
126        echo "Working around cygwin backtick output limitation"
127        # we really didn't want to create any files here -
128        # but certain versions of Cygwin don't capture the text that
129        # comes out of ImageUploader.exe for some strange unknown reason -
130        # but they are happy to capture the results of 'cat'
131        ImageUploader.exe>"$TMP/${SESSION_PATH_PREFIX}imguploaderv.txt"
132        IMGUPLOADERV=`cat "$TMP/${SESSION_PATH_PREFIX}imguploaderv.txt" | grep "Image Uploader by"`
133    fi
134
135    if [[ $IMGUPLOADERV == *ver.* ]]
136    then
137        IMGUPLOADERV=${IMGUPLOADERV#*.}
138        IMGUPLOADFLAT=0
139        vpart=0
140        while [ $vpart -lt 4 ]
141        do
142            # delete all after the first dot
143            IMGVF1=${IMGUPLOADERV%%.*}
144            IMGUPLOADERV=${IMGUPLOADERV#*.}
145            IMGVF1=${IMGVF1//[^0-9]/}
146            ((IMGUPLOADFLAT=IMGUPLOADFLAT*100+IMGVF1))
147            ((vpart=vpart+1))
148        done
149            echo "Detected ImageUploader flatversion to be $IMGUPLOADFLAT"
150    else
151        set IMGUPLOADFLAT=$MIN_WUMAD_SUPPORT_UPLOADER
152        echo "WARNING: Unable to query ImageUploader version"
153    fi
154}
155
156
157##################################################################################################
158# WUMAD file path validation check
159##################################################################################################
160wumad_path_check()
161{
162		if [ ! -f "$wumad_path" ]
163        then
164			echo "Could not find a .wumad file in the specified directory!"
165            echo "Please confirm the file path:"
166			echo "    $wumad_path"
167			exit 2
168		fi
169
170        #echo "WUMAD file: $wumad_path"
171}
172
173##################################################################################################
174# WUMAD file decompression using ImageUploader
175##################################################################################################
176main_image_extract()
177{
178    echo "Extracting $wumad_path to $WUMAD_EXTRACT_TMP."
179
180    # Use ImageUploader.exe to just extract the image to a folder
181    ImageUploader.exe -ip 0.0.0.0 -upload 0 "$wumad_path" > wumad_extract.log
182    IMGUPLOAD_RVAL=$?
183
184    if [ $enable_workarounds -eq 1 ]
185    then
186        # Ignore the return from ImageUploader as it will always be -1
187
188        # Workaround for a directory attribute test bug in ImageUploader.exe
189        echo "Changing attributes on $WUMAD_EXTRACT_TMP."
190        attrib -I `cygpath -w "$WUMAD_EXTRACT_TMP"`
191    else
192        # Check the return from ImageUploader to see if we have a successful image extraction
193        if [ $IMGUPLOAD_RVAL -ne 0 ]
194        then
195            echo "$(basename $0): ERROR: Failed to extract $wumad_path!"
196            exit 1
197        fi
198    fi
199}
200
201##################################################################################################
202# Main Image Upload Process
203##################################################################################################
204main_image_upload()
205{
206	for catdev_ip in $ip_addresses
207	{
208        # Use the output of the image extract on all uploads
209        echo "Uploading $WUMAD_EXTRACT_TMP to bank number $bank_no on Devkit using IP $catdev_ip."
210        uploadimg.sh $bank_no "$WUMAD_EXTRACT_TMP" $catdev_ip > $catdev_ip.log &
211
212        if [ $enable_workarounds -eq 1 ]
213        then
214            # Workaround for a file share access bug in ImageUploader.exe
215            echo "Sleeping 5 seconds for ImageUploader syncronization."
216            sleep 5
217        fi
218	}
219
220    echo "Waiting for all images to finish uploading..."
221    wait
222}
223
224##################################################################################################
225# Main
226##################################################################################################
227
228check_imageuploader_version
229if [ $IMGUPLOADFLAT -lt 805 ]
230then
231    # ImageUploader v805 has fixes for the workarounds
232    enable_workarounds=1
233fi
234
235if [ $skip_extract -eq 0 ]
236then
237    # Check and extract the WUMAD to the TMP folder
238    wumad_path_check
239    main_image_extract
240fi
241
242# Upload extracted image in TMP folder to DevKits
243main_image_upload
244
245echo "Complete! All images have finished uploading."
246exit 0
247