#!/bin/bash

#
#   Runs me_cleaner on multiple ROM images .as many as you want!
#   Filename of modded image will be filename.neutered. so, .neutered added
#   to the end of the filename
#
#	Copyright (C) 2020 Leah Rowe <info@minifree.org>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

[ "x${DEBUG+set}" = 'xset' ] && set -v
set -u -e

if [ ! -f "me_cleaner/me_cleaner.py" ]; then
    printf "\nme_cleaner not found. Please run: ./download me_cleaner\n"
    exit 1
fi

if [ $# -gt 0 ]; then
	for rom in "${@}"; do
        if [ ! -f "${rom}" ]; then
            printf "\nWARNING: meclean script: file %s does not exist. skipping\n" "${rom}"
            continue 
        fi
        [ ! -d "bin/neutered/" ] && mkdir -p "bin/neutered/"
        [ -f "bin/neutered/${rom##*/}" ] && \
            rm -f "bin/neutered/${rom##*/}.neutered"
        python3 me_cleaner/me_cleaner.py -S -O "bin/neutered/${rom##*/}.neutered" "${rom}" \
            || continue
        printf "\nOriginal ROM:\n%s\nNeutered ROM available at: %s\n" \
            "${rom}" "bin/neutered/${rom##*/}.neutered"
	done
else
    printf "\nERROR: meclean script: no argument given. Please specify a ROM path\n"
    exit 1  
fi

printf "\nYour ME-neutered ROMs are under bin/neutered/\n"
