1#!/usr/bin/env bash
2
3###############################################################################
4#
5# Copyright (C) 2009-2013 Nintendo.  All rights reserved.
6#
7# These coded instructions, statements, and computer programs contain
8# proprietary information of Nintendo of America Inc. and/or Nintendo
9# Company Ltd., and are protected by Federal copyright law.  They may
10# not be disclosed to third parties or copied or duplicated in any form,
11# in whole or in part, without the prior written consent of Nintendo.
12#
13###############################################################################
14
15###############################################################################
16# display name
17###############################################################################
18display_name ()
19{
20    echo "$(basename $0) - Download image creator, Copyright 2012 Nintendo."
21    echo ""
22}
23
24###############################################################################
25# wuma check
26###############################################################################
27wuma_check ()
28{
29    local WUMA="$1"
30
31    if [ ! -e "$WUMA" ]
32    then
33        echo "$(basename $0) failed: $WUMA does not exist."
34        return 1
35    else
36        if [ ! -f "$WUMA" ]
37        then
38            echo "$(basename $0) failed: $WUMA is not a file."
39            return 1
40        fi
41    fi
42
43    if [ ! "${WUMA##*.}" == "wumad" -a ! "${WUMA##*.}" == "wumat" -a ! "${WUMA##*.}" == "wumap" ]
44    then
45        echo "$(basename $0) failed: $WUMA is not a wumad/wumat/wumap file."
46        return 1
47    fi
48    return 0
49}
50
51###############################################################################
52# dir check
53###############################################################################
54dir_check ()
55{
56    local DIR="$1"
57    local CLEAN_UP="$2"
58
59    if [ -e "$DIR" ]
60    then
61        if [ ! -d "$DIR" ]
62        then
63            echo "$(basename $0) failed: $DIR is not a directory."
64            return 1
65        fi
66        if [ "$CLEAN_UP" -eq 1 ]
67        then
68            rm -Rf "$DIR"
69            mkdir "$DIR"
70        fi
71    else
72        mkdir "$DIR"
73    fi
74
75    return 0
76}
77
78###############################################################################
79# erase temporary items
80###############################################################################
81erase_work ()
82{
83    rm -Rf "$TEMP_WORK_DIR"
84}
85
86###############################################################################
87# print usage help message
88###############################################################################
89usage ()
90{
91    cat <<EOF
92    
93    $(basename $0) - Create download image from master archive file
94    
95    Usage: $(basename $0) -i <path> [-o <path>] [-w <path>] [-z]
96    
97    options:
98      -i <path> : Master archive file to create download image from.
99      -o <path> : Output base directory to create download images.
100                  They are created under here with generating each partition folder.
101                  If omitted, they'll be created at "$DEFAULT_OUTPUT_BASE_DIR".
102      -w <path> : Temporary directory for processing.
103                  If omitted, create temporary directory at the current work dir.
104      -z        : Omit extracting zip file
105EOF
106}
107
108###############################################################################
109# main
110###############################################################################
111
112# Environment settings
113
114BIN_TOOL_DIR="$CAFE_ROOT/system/bin/tool"
115DEFAULT_OUTPUT_BASE_DIR="$CAFE_ROOT/${SESSION_PATH_PREFIX}data/mlc/install"
116OMIT_EXTRACTING=
117
118while getopts ":i:o:w:zv" opt; do
119    case $opt in
120    i  ) INPUT_WUMA_FILE="`cygpath -a $OPTARG`" ;;
121    o  ) OUTPUT_BASE_DIR="`cygpath -a $OPTARG`" ;;
122    w  ) TEMP_WORK_DIR="`cygpath -a $OPTARG`" ;;
123    z  ) OMIT_EXTRACTING="TRUE" ;;
124    v  ) VERBOSE=1 ;;
125    \? ) usage
126         exit 1 ;;
127    esac
128done
129
130# setup bin/tool
131MASTER2PART="$BIN_TOOL_DIR/mastering/master2part.exe"
132UNPACKFILE="$BIN_TOOL_DIR/mastering/unpackfile.exe"
133
134# display script name
135display_name
136
137# check for bin tools
138if [ ! -f "$MASTER2PART" ]
139then
140    echo "$(basename $0) failed: $MASTER2PART not found."
141    exit 2
142fi
143
144# input wuma check
145if [ -z "$INPUT_WUMA_FILE" ]
146then
147    echo "$(basename $0) failed: Please specify input wumad/wumap file with -i."
148    exit 3
149fi
150wuma_check "$INPUT_WUMA_FILE"
151rval=$?
152if [ $rval -ne 0 ]
153then
154    exit 4
155fi
156
157# output base dir check
158if [ -z "$OUTPUT_BASE_DIR" ]
159then
160    OUTPUT_BASE_DIR="$DEFAULT_OUTPUT_BASE_DIR"
161fi
162dir_check "$OUTPUT_BASE_DIR" 0
163rval=$?
164if [ $rval -ne 0 ]
165then
166    exit 5
167fi
168
169# temp work dir check
170if [ -z "$TEMP_WORK_DIR" ]
171then
172    TEMP_WORK_DIR="`basename "${INPUT_WUMA_FILE%.*}"`_tmp"
173else
174    if [ -e "$TEMP_WORK_DIR" ]
175    then
176        TEMP_WORK_SUBDIR="`basename "${INPUT_WUMA_FILE%.*}"`_tmp"
177        TEMP_WORK_DIR="$TEMP_WORK_DIR/$TEMP_WORK_SUBDIR"
178    fi
179fi
180dir_check "$TEMP_WORK_DIR" 1
181rval=$?
182if [ $rval -ne 0 ]
183then
184    erase_work
185    exit 7
186fi
187TEMP_WORK_DIR_DOS=`cygpath -w -a "$TEMP_WORK_DIR"`
188
189# unpack partition data to the destination
190PART_DIR="$TEMP_WORK_DIR/part"
191INPUT_WUMA_FILE_DOS=`cygpath -w -a "$INPUT_WUMA_FILE"`
192PART_DIR_DOS=`cygpath -w -a "$PART_DIR"`
193"$MASTER2PART" -v -nc "-wd=$TEMP_WORK_DIR_DOS" "$INPUT_WUMA_FILE_DOS" "$PART_DIR_DOS"
194rval=$?
195if [ $rval -ne 0 ]
196then
197    erase_work
198    exit $rval
199fi
200
201# create package list
202PACKAGE_LIST=`find "$PART_DIR" | grep ".*p[0-9A-Fa-f][0-9A-Fa-f]\.zip.*$"`
203PACKAGE_LIST_FILE="$TEMP_WORK_DIR/package_list.txt"
204echo "`echo "$PACKAGE_LIST" | wc -l` package(s) found."
205
206echo "$PACKAGE_LIST" > "$PACKAGE_LIST_FILE"
207
208BUFIFS=$IFS
209IFS=
210l_num=1
211
212exec 3< "$PACKAGE_LIST_FILE"
213while read PACKAGE_FILE 0<&3
214do
215    echo "Processing $PACKAGE_FILE..."
216
217    # output dir check
218    OUTPUT_DIR="$OUTPUT_BASE_DIR/$(basename ${INPUT_WUMA_FILE%.*})_$(basename ${PACKAGE_FILE%.*})"
219    if [ -e "$OUTPUT_DIR" ]
220    then
221        echo "$(basename $0) WARNING: $OUTPUT_DIR already exists, erasing..."
222        rm -Rf "$OUTPUT_DIR"
223    fi
224
225    if [ -n "$OMIT_EXTRACTING" ]; then
226        OUTPUT_FILE="$(cygpath -a $OUTPUT_DIR).zip"
227        mv "$PACKAGE_FILE" "$OUTPUT_FILE"
228        echo "output file: $OUTPUT_FILE"
229    else
230        # unpack zip to output dir
231        PACKAGE_FILE_DOS=`cygpath -w -a "$PACKAGE_FILE"`
232        OUTPUT_DIR_DOS=`cygpath -w -a "$OUTPUT_DIR"`
233        "$UNPACKFILE" -v -nc "-wd=$TEMP_WORK_DIR_DOS" "$PACKAGE_FILE_DOS" "$OUTPUT_DIR_DOS"
234        rval=$?
235        if [ $rval -ne 0 ]
236        then
237            echo "$(basename $0) failed: Unable to unpack $(basename $PACKAGE_FILE)"
238            erase_work
239            exit $rval
240        fi
241        echo "unpacked $(basename $PACKAGE_FILE) to $OUTPUT_DIR"
242    fi
243done
244exec 3<&-
245
246IFS=$BUFIFS
247
248echo "Download image(s) successfully created under $OUTPUT_BASE_DIR."
249
250# Erase settings
251erase_work
252