# Elimina bélicas de la base de datos
# Dominio público. 2008. vtamara@pasosdeJesus.org

if (test ! -f ./vardb.sh) then {
	echo "Ejecute desde directorio de SIVeL";
	exit 1;
} fi;

. ./vardb.sh

echo "Este script eliminará clasificación de bélicas de casos así como casos que sólo tengan clasificación de bélicas (y las víctimas que pudieran tener erradamente  asociadas.  Confirma continuar (s para continuar)";
read sn
if (test "$sn" = "s") then {
	bin/psql.sh -c "delete from antecedente_combatiente;"
	bin/psql.sh -c "delete from p_responsable_agrede_combatiente;"
	bin/psql.sh -c "delete from combatiente;"
	bin/psql.sh -c "delete from combatiente;"
	bin/psql.sh -c "delete from categoria_p_responsable_caso where id_tipo_violencia='C';"
	bin/psql.sh -c "delete from categoria_comunidad where id_tipo_violencia='C';"
	bin/psql.sh -c "delete from categoria_caso where id_tipo_violencia='C';"
	bin/psql.sh -c "delete from caso_contexto where id_caso not in (select distinct id_caso from categoria_caso order by 1);"
	bin/psql.sh -c "delete from presuntos_responsables_caso where id_caso not in (select distinct id_caso from categoria_caso order by 1);"
	bin/psql.sh -c "delete from antecedente_caso where id_caso not in (select distinct id_caso from categoria_caso order by 1);"
	bin/psql.sh -c "delete from departamento_caso where id_caso not in (select distinct id_caso from categoria_caso order by 1);"
	bin/psql.sh -c "delete from municipio_caso where id_caso not in (select distinct id_caso from categoria_caso order by 1);"
	bin/psql.sh -c "delete from clase_caso where id_caso not in (select distinct id_caso from categoria_caso order by 1);"
	bin/psql.sh -c "delete from ubicacion_caso where id_caso not in (select distinct id_caso from categoria_caso order by 1);"
	bin/psql.sh -c "delete from funcionario_caso where id_caso not in (select distinct id_caso from categoria_caso order by 1);"
	bin/psql.sh -c "delete from categoria_personal where id_victima in (select distinct id from victima where id_caso not in (select distinct id_caso from categoria_caso order by 1) order by 1);";
	bin/psql.sh -c "delete from p_responsable_agrede_persona where id_victima in (select distinct id from victima where id_caso not in (select distinct id_caso from categoria_caso order by 1) order by 1);";
	bin/psql.sh -c "delete from antecedente_victima where id_victima in (select distinct id from victima where id_caso not in (select distinct id_caso from categoria_caso order by 1) order by 1);";
	bin/psql.sh -c "delete from victima where id_caso not in (select distinct id_caso from categoria_caso order by 1) ;"
	bin/psql.sh -c "delete from region_caso where id_caso not in (select distinct id_caso from categoria_caso order by 1) ;"
	bin/psql.sh -c "delete from frontera_caso where id_caso not in (select distinct id_caso from categoria_caso order by 1) ;"
	bin/psql.sh -c "delete from caso where id not in (select distinct id_caso from categoria_caso order by 1);"
} fi;
