#!/bin/sh

addr=$1
malware_name=$2
mode=$3
# Corresponding option of each mode: either cpu usage for calculation mode, or portno for port_listening mode.
crspd_option=$4
basevm_type=$5
abs_path=$6
os_type=$7

inst_dir="instantiation"

if [ ${basevm_type} = "kvm" ]; then
    # Copy the source code to the /usr/bin/ of base image.
    scp ${abs_path}${inst_dir}/malware_creation/dummy_malware root@${addr}:/usr/bin/${malware_name}

    # Add code to rc.d/rc.local file.
    if [ ${mode} = "calculation" ]; then
        scp ${abs_path}${inst_dir}/malware_creation/cpulimit/src/cpulimit root@${addr}:/usr/bin/
        command="\"exec -a cpusage cpulimit -l ${crspd_option} ${malware_name} ${mode} 1000 &\"";
        ssh root@${addr} "echo 'bash -c ${command}'  >> /etc/rc.d/rc.local";
    else
        command="${malware_name} ${mode} ${crspd_option} &";
        ssh root@${addr} "echo '${command}'  >> /etc/rc.d/rc.local";
    fi
elif [ ${basevm_type} = "aws" ]; then
    if [ ${os_type} = "red_hat" -o ${os_type} = "amazon_linux" -o ${os_type} = "amazon_linux2" ]; then
        # Copy the source code to the /usr/bin/ of base image.
        scp -r -i TESTKEY.pem -o StrictHostKeyChecking=no ${abs_path}${inst_dir}/malware_creation/dummy_malware ec2-user@${addr}:${malware_name}
        ssh -i TESTKEY.pem ec2-user@${addr} "chmod +x ${malware_name}; sudo mv ${malware_name} /usr/bin"
        # Add code to rc.d/rc.local file.
        if [ ${mode} = "calculation" ]; then
            scp -r -i TESTKEY.pem -o StrictHostKeyChecking=no ${abs_path}${inst_dir}/malware_creation/cpulimit/src/cpulimit ec2-user@${addr}:
            command="\"exec -a cpusage cpulimit -l ${crspd_option} ${malware_name} ${mode} 1000 &\"";
            #ssh -i TESTKEY.pem -o StrictHostKeyChecking=no ec2-user@${addr} "chmod +x cpulimit; sudo mv cpulimit /usr/bin"
            ssh -i TESTKEY.pem -o StrictHostKeyChecking=no ec2-user@${addr} "chmod +x cpulimit; sudo mv cpulimit /usr/bin; sudo chmod 777 /etc/rc.d/rc.local; echo 'bash -c ${command}' >> /etc/rc.d/rc.local";
        else
            command="${malware_name} ${mode} ${crspd_option} &";
            ssh -i TESTKEY.pem -o StrictHostKeyChecking=no ec2-user@${addr} "echo '${command}'  >> /etc/rc.d/rc.local";
        fi
    elif [ ${os_type} = "ubuntu_20" -o ${os_type} = "ubuntu_18" ]; then
        # Copy the source code to the /usr/bin/ of base image.
        scp -r -i TESTKEY.pem -o StrictHostKeyChecking=no ${abs_path}${inst_dir}/malware_creation/dummy_malware ubuntu@${addr}:${malware_name}
        ssh -i TESTKEY.pem ubuntu@${addr} "chmod +x ${malware_name}; sudo mv ${malware_name} /usr/bin"
        # Add code to rc.d/rc.local file.
        if [ ${mode} = "calculation" ]; then
            scp -r -i TESTKEY.pem -o StrictHostKeyChecking=no ${abs_path}${inst_dir}/malware_creation/cpulimit/src/cpulimit ubuntu@${addr}:
            command="\"exec -a cpusage cpulimit -l ${crspd_option} ${malware_name} ${mode} 1000 &\"";
            ssh -i TESTKEY.pem -o StrictHostKeyChecking=no ubuntu@${addr} "chmod +x cpulimit; sudo mv cpulimit /usr/bin"
            ssh -i TESTKEY.pem -o StrictHostKeyChecking=no ubuntu@${addr} "sudo touch /etc/rc.local; sudo chmod 777 /etc/rc.local; echo -e '#!/bin/bash\nbash -c ${command}'  >> /etc/rc.local";
        else
            command="${malware_name} ${mode} ${crspd_option} &";
            ssh -i TESTKEY.pem -o StrictHostKeyChecking=no ubuntu@${addr} "echo '${command}'  >> /etc/rc.local";
        fi
    elif [ ${os_type} = "ubuntu_16" ]; then
        scp -r -i TESTKEY.pem -o StrictHostKeyChecking=no ${abs_path}${inst_dir}/malware_creation/dummy_malware ubuntu@${addr}:${malware_name}
        ssh -i TESTKEY.pem ubuntu@${addr} "chmod +x ${malware_name}; sudo mv ${malware_name} /usr/bin"
        if [ ${mode} = "calculation" ]; then
            scp -r -i TESTKEY.pem -o StrictHostKeyChecking=no ${abs_path}${inst_dir}/malware_creation/cpulimit/src/cpulimit ubuntu@${addr}:
            command="\"exec -a cpusage cpulimit -l ${crspd_option} ${malware_name} ${mode} 1000 &\"";
            ssh -i TESTKEY.pem -o StrictHostKeyChecking=no ubuntu@${addr} "chmod +x cpulimit; sudo mv cpulimit /usr/bin; touch rc.local; sudo chmod 777 rc.local /etc/rc.local; sudo echo -e '#!/bin/bash\nbash -c ${command}'  >> rc.local; sudo mv rc.local /etc/rc.local; "
            #ssh -i TESTKEY.pem -o StrictHostKeyChecking=no ubuntu@${addr} "sudo sed 14d /etc/rc.local; echo -e 'bash -c ${command}' >> /etc/rc.local";
        else
            command="${malware_name} ${mode} ${crspd_option} &";
            ssh -i TESTKEY.pem -o StrictHostKeyChecking=no ubuntu@${addr} "echo '${command}'  >> /etc/rc.local";
        fi
    fi
fi
