#!/bin/bash

if [ $# -ne 2 ]; then
	echo "Usage: $0 initial_resource attack_goal"
fi

##################################################
init_pred=""
goal_pred=""

##################################################

if [ $1 = "net" ]; then
	init_pred="net_init"
elif [ $1 = "local" ]; then
	init_pred="local_init"
else
	echo "unrecognized initial_resource: $1"
	exit 1 
fi

##################################################

if [ $2 = "module" ]; then
	goal_pred="load_module_goal"
elif [ $2 = "trojan" ]; then
	goal_pred="trojan_goal"
else
	echo "unrecognized attack_goal: $2"
	exit 1 
fi

##################################################

cd ../ubun-hardy-apparmor/prolog

outfile="ub_aa_$1_$2"

echo "['ub_aa_file_data.P']." > prolog.tmp
echo "['ub_aa_user_data.P']." >> prolog.tmp
echo "['ub_aa_group_data.P']." >> prolog.tmp
echo "['ub_aa_ps_data.P']." >> prolog.tmp
echo "['ub_aa_netstat_data.P']." >> prolog.tmp
echo "['ub_aa_policy_data.P']." >> prolog.tmp
echo "['ub_aa_profile_data.P']." >> prolog.tmp
echo "['ub_aa_rules.P']." >> prolog.tmp
echo "time(aa_generate_graph($init_pred, $goal_pred))." >> prolog.tmp
echo "write_graph('${outfile}.out', $goal_pred)." >> prolog.tmp
echo "halt." >> prolog.tmp

swipl < prolog.tmp

cd ../output
time ./analyze_path.sh $outfile
time ./gen_pdf.sh  $outfile

