nds2rm/nds2rm
2009-03-21 18:55:50 +00:00

386 lines
9.5 KiB
Bash

#!/bin/bash
#-----------------------------------------------------------------------------------------------------------------------------------
# NDS2RM: NDS Simple Rom Manager
#
# (C) 2009 Arnaud G. Gibert
#-----------------------------------------------------------------------------------------------------------------------------------
# $RCSfile: nds2rm,v $
# $Revision: 1.1 $
# $Name: $
# $Date: 2009/03/21 18:55:50 $
# $Author: agibert $
#-----------------------------------------------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------------------------------------
# This file is part of NDS2RM
#
# NDS2RM is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public Licence as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# NDS2RM 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with NDS2RM; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#-----------------------------------------------------------------------------------------------------------------------------------
#NDS_REPOSITORY=/opt/public/nds
NDS_REPOSITORY=/opt/public/nds.new
NDS_ROMS=${NDS_REPOSITORY}/roms2
NDS_DAT=${NDS_REPOSITORY}/misc/dsrom.dat
NDS_IDX=${NDS_REPOSITORY}/roms/dsrom.idx
NDS_VERSION="$Name: $"
# Print Version
#-----------------------------------------------------------------------------------------------------------------------------------
function version_print()
{
echo ${NDS_VERSION} | sed -e 's/.*: //' -e 's/-/ /' -e 's/_/\./g' -e 's/\$$//'
}
# Prin Help
#-----------------------------------------------------------------------------------------------------------------------------------
function help_print()
{
echo ""
}
# Parse Args
#-----------------------------------------------------------------------------------------------------------------------------------
function args_parse()
{
mode="test"
rebuild="no"
tmp_args=$(getopt -o CThVr --long correct,test,help,version,rebuild -n 'nds_rom_mng' -- "$@")
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
# Note the quotes around `$TEMP': they are essential!
eval set -- "${tmp_args}"
while true ; do
case "$1" in
-C|--correct) mode="correct"; shift;;
-T|--test) mode="test"; shift;;
-h|--help) mode="exit"; help_print; shift;;
-V|--version) mode="exit"; version_print; shift;;
-r|--rebuild) rebuild="yes"; shift;;
--) shift; break;;
*) echo "Internal error [$1] !"; exit 1;;
esac
done
}
# Rename Archive
#-----------------------------------------------------------------------------------------------------------------------------------
function stats_update()
{
action=${1}
mode=${2}
case ${action} in
("set_status")
if [[ ${status} == "OK" ]]
then
ss_ok_cnt=$((${ss_ok_cnt} + 1))
else
ss_ko_cnt=$((${ss_ko_cnt} + 1))
fi
;;
("change_status")
if [[ ${status} == "OK" ]]
then
cs_ok_cnt=$((${cs_ok_cnt} + 1))
else
cs_ko_cnt=$((${cs_ko_cnt} + 1))
fi
;;
("keep_status")
if [[ ${status} == "OK" ]]
then
ks_ok_cnt=$((${ks_ok_cnt} + 1))
else
ks_ko_cnt=$((${ks_ko_cnt} + 1))
fi
;;
*)
echo "Internal error [$1] !";
exit 1
;;
esac
}
# Do Processing Action
#-----------------------------------------------------------------------------------------------------------------------------------
function do_action()
{
id=${1}
title=${2}
reg=${3}
lang=${4}
crc=${5}
status=${6}
file=${7}
action=${8}
mode=${9}
rebuild=${10}
if [[ "${mode}" == "correct" ]]
then
if [[ ( "${file}" != *.zip ) || ( ${rebuild} == "yes" ) ]]
then
rom="${id} - ${title} (${reg}) [${lang}].nds"
case "${file}"
in
(*.nds)
mv "${NDS_ROMS}/${file}" "${rom}"
;;
(*.rar)
unrar p "${NDS_ROMS}/${file}" > "${rom}"
\rm -f "${NDS_ROMS}/${file}"
;;
(*.zip)
unzip -p "${NDS_ROMS}/${file}" > "${rom}"
\rm -f "${NDS_ROMS}/${file}"
;;
(*)
echo "Internal error [$1] !";
exit 1
;;
esac
file="${id}.zip"
zip -m9 "${NDS_ROMS}/${id} - ${title} (${reg}) [${lang}] {${crc}} ${status}.zip" "${rom}" >/dev/null 2>&1 &
else
if [[ ${action} != "keep_status" ]]
then
mv "${NDS_ROMS}/${file}" "${NDS_ROMS}/${id} - ${title} (${reg}) [${lang}] {${crc}} ${status}.zip"
fi
fi
fi
stats_update "${action}" "${mode}"
}
# Main
#-----------------------------------------------------------------------------------------------------------------------------------
# Initialise stats variables
loaded_cnt=0
ss_ok_cnt=0
ss_ko_cnt=0
cs_ok_cnt=0
ct_ko_cnt=0
ks_ok_cnt=0
ks_ko_cnt=0
ok_cnt=0
ko_cnt=0
found_cnt=0
missing_cnt=0
args_parse "$@"
if [[ ${mode} == "exit" ]]
then
exit 0
fi
if [[ ! -f ${NDS_DAT} ]]
then
echo "error: dsrom.dat not found !"
exit 1
fi
#-----------------------------------------------------------------------------------------------------------------------------------
echo "loading dsrom.dat..."
IFS=';'
while read line
do
set ${line}
idx=$( echo ${2} | sed 's/^0*//')
if [[ "${idx}" != "xxxx" ]]
then
echo -ne "$2\r"
dat_rn[${idx}]="${1}"
dat_id[${idx}]="${2}"
dat_title[${idx}]="${3}"
dat_region[${idx}]="${4}"
dat_lang[${idx}]="${5}"
dat_save_type[${idx}]="${6}"
dat_crc[${idx}]="${7}"
loaded_cnt=$((${loaded_cnt} + 1))
fi
done < ${NDS_DAT}
#-----------------------------------------------------------------------------------------------------------------------------------
echo "scanning rom repository..."
IFS=' '
tmp_file=$(mktemp)
idx_old=0
ls ${NDS_ROMS} > ${tmp_file}
while read file
do
set ${file:0:4}
id=$1
idx=$( echo ${id} | sed 's/^0*//')
title=${dat_title[${idx}]}
reg=${dat_region[${idx}]}
lang=${dat_lang[${idx}]}
crc=${dat_crc[${idx}]}
regex=".... - .* \(.*\) \[.*\] \{.*\} ..\.zip$"
if [[ $file =~ $regex ]]
then
title2=$(echo ${file} | sed -e 's/.... - //' -e 's/ (.*//')
reg2=$(echo ${file} | sed -e 's/.*(//' -e 's/).*//')
lang2=$(echo ${file} | sed -e 's/.*\[//' -e 's/\].*//')
crc2=$(echo ${file} | sed -e 's/.*{//' -e 's/}.*//')
status=$(echo ${file} | sed -e 's/.* //' -e 's/\.zip$//')
if [[ "${crc}" != "${crc2}" ]]
then
if [[ "${status}" != "KO" ]]
then
do_action "${id}" "${title}" "${reg}" "${lang}" "${crc}" "KO" "${file}" "change_status" "${mode}" "${rebuild}"
else
do_action "${id}" "${title}" "${reg}" "${lang}" "${crc}" "KO" "${file}" "keep_status" "${mode}" "${rebuild}"
fi
else
if [[ "${status}" == "OK" ]]
then
do_action "${id}" "${title}" "${reg}" "${lang}" "${crc}" "OK" "${file}" "keep_status" "${mode}" "${rebuild}"
else
do_action "${id}" "${title}" "${reg}" "${lang}" "${crc}" "OK" "${file}" "change_status" "${mode}" "${rebuild}"
fi
fi
else
crc2=$( case "${file}"
in
(*.nds)
cat "${NDS_ROMS}/${file}"
;;
(*.rar)
unrar p "${NDS_ROMS}/${file}"
;;
(*.zip)
unzip -p "${NDS_ROMS}/${file}"
;;
(*)
break;
;;
esac | check -n 2>&1 | sed -e 's/,.*//' -e 's/.*= //' | tr [:lower:] [:upper:])
if [[ "${crc}" != "${crc2}" ]]
then
do_action "${id}" "${title}" "${reg}" "${lang}" "${crc}" "KO" "${file}" "set_status" "${mode}" "${rebuild}" "${rebuild}"
else
do_action "${id}" "${title}" "${reg}" "${lang}" "${crc}" "OK" "${file}" "set_status" "${mode}" "${rebuild}" "${rebuild}"
fi
fi
ok_cnt=$((${ss_ok_cnt} + ${cs_ok_cnt} + ${ks_ok_cnt}))
ko_cnt=$((${ss_ko_cnt} + ${cs_ko_cnt} + ${ks_ko_cnt}))
found_cnt=$((${ok_cnt} + ${ko_cnt}))
missing_cnt=$((${missing_cnt} + ${idx} - ${idx_old} - 1))
idx_old=${idx}
printf "found: %4d OK: %4d KO: %4d missing: %4d \r" ${found_cnt} ${ok_cnt} ${ko_cnt} ${missing_cnt}
done < ${tmp_file}
\rm -f ${tmp_file}
wait
# Print statistics
#-----------------------------------------------------------------------------------------------------------------------------------
echo " "
printf "%4d roms set to OK\n" ${ss_ok_cnt}
printf "%4d roms changed to OK\n" ${cs_ok_cnt}
printf "%4d roms kept to OK\n" ${ks_ok_cnt}
printf "%4d roms are OK\n" ${ok_cnt}
echo
printf "%4d roms set to KO\n" ${ss_ko_cnt}
printf "%4d roms changed to KO\n" ${cs_ko_cnt}
printf "%4d roms kept to KO\n" ${ks_ko_cnt}
printf "%4d roms are KO\n" ${ko_cnt}
echo
printf "%4d roms are in dsrom.dat\n" ${loaded_cnt}
printf "%4d roms are found\n" ${found_cnt}
printf "%4d roms are missing\n" ${missing_cnt}