summaryrefslogtreecommitdiff
path: root/bin/iso2wbfs
blob: 59b0eed8b8fc02d6ced8b5f3f404825b04b1aafc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/bin/bash
#===============================================================================
#
#          FILE:  iso2wbfs
#
#         USAGE:  ./iso2wbfs [option] FILE... [wbfs directory]
#
#   DESCRIPTION:  Uses wit to convert one or more  Wii ISO into a WBFS file
#                 properly named for use on non-WBFS partitions.
#
#       OPTIONS:  ---
#  REQUIREMENTS:  WIT ( http://wit.wiimm.de/wit/ )
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR:  Timothy Caraballo, openback@gmail.com
#       COMPANY:  Pixelpod International, Inc.
#       VERSION:  1.04
#       CREATED:  2010-08-10 08:31:01 AM EST
#===============================================================================

#===============================================================================
# Script variables
#===============================================================================
# The regex to grab info from the iso
REG="^([[:alnum:]]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:alpha:]]{3,4})[[:space:]]+(.+)$"
NOCLOBBER=1
NAMINGSTYLE=1
NAMINGSTYLESET=0
eval OUTPUTDIR=\$$#

#===============================================================================
# Prints usage info
#===============================================================================
function USAGE () {
echo ""
echo "USAGE: "
echo "    iso2wbfs [option] FILE... [wbfs_directory]"
echo ""
echo "OPTIONS:"
echo "    -f  Force overwrite of existing files"
echo "    -v  Version information"
echo "    -h  This usage information"
echo "  NAMING STYLES:"
echo "    -1  /GAMEID.wbfs    (default)"
echo "    -2  /GAMEID_Game title.wbfs"
echo "    -3  /Game title [GAMEID].wbfs"
echo "    -4  /GAMEID/GAMEID.wbfs"
echo "    -5  /GAMEID_Game title/GAMEID.wbfs"
echo "    -6  /Game title [GAMEID]/GAMEID.wbfs"
echo ""
exit $E_OPTERROR    # Exit and explain usage, if no argument(s) given.
}

#===============================================================================
# Main routine
#===============================================================================

# Set our option variables
while getopts ":123456fhv?" opt
do
    case $opt in
        [1-6])
            if (( $NAMINGSTYLESET == 1 )); then
                echo "Error: You may only specify one naming convention"
                USAGE
                exit 1
            else
                NAMINGSTYLESET=1
                NAMINGSTYLE=$opt
            fi
            ;;
        f)
            NOCLOBBER=0
            ;;
        h)
            USAGE
            exit 0
            ;;
        v)
            echo "`basename $0` 1.04 (2011-08-11)"
            exit 0
            ;;
        ?)
            echo ""
            echo "Invalid option: -$OPTARG" >&2
            USAGE
            exit 1
            ;;
    esac
done
shift $(($OPTIND - 1))

# Is the last argument a directory?
if [[ -d "$OUTPUTDIR" ]]; then
    HASDIR=1
else
    # It isn't a directory, so let's make sure it's a file and not a mistake
    if [[ -f "$OUTPUTDIR" ]]; then
        OUTPUTDIR=.
        HASDIR=0
    else
        echo "Error: $OUTPUTDIR was not found."
        exit 1
    fi
fi

# Do we have an iso to work with?
(( $# )) || USAGE

while (( $# > $HASDIR ))
do
    ISO=$1
    shift

    # Check if the ISO is a valid Wii disc
    if [[ `wit ll -H "$ISO"` =~ $REG ]]; then
        # Grab the info from the output
        WIITITLE=`echo ${BASH_REMATCH[4]}|tr -d '\n'|tr '[\:\*]' '_'`
        GAMEID=${BASH_REMATCH[1]}

        case $NAMINGSTYLE in
            1)
                WBFSFILE=$GAMEID.wbfs
                ;;
            2)
                WBFSFILE=${GAMEID}_$WIITITLE.wbfs
                ;;
            3)
                WBFSFILE=$WIITITLE\ \[$GAMEID\].wbfs
                ;;
            4)
                WBFSFILE=$GAMEID/$GAMEID.wbfs
                ;;
            5)
                WBFSFILE=${GAMEID}_$WIITITLE/$GAMEID.wbfs
                ;;
            6)
                WBFSFILE=${WIITITLE}\ \[$GAMEID\]/$GAMEID.wbfs
                ;;
        esac

        WBFSFILE=$OUTPUTDIR/$WBFSFILE

        if [[ -f "$WBFSFILE" ]]; then
            if (( $NOCLOBBER )); then
                echo "Error: $WBFSFILE exists."
            else
                rm "$WBFSFILE" || exit 1
            fi
        fi

        # Make the destination folder if it doesn't exist
        mkdir -p "`dirname "$WBFSFILE"`" || exit 1

        # Now just run the copy
        wit -BP COPY "$ISO" "$WBFSFILE"
    else
        echo "Error parsing $ISO"
        exit 1
    fi
done