#!/bin/bash

#This script copies all the scripts specified in a file to a vm of your choice. 
#This is a qubes-os specific script.

# i3-copy-scripts - Copy scripts to a vm of your choice.
# Copyright (C) 2017 Fabrizio Romano Genovese <egonigredo@gmail.com>

# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties 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/>

# Put this file in /bin/ or in /usr/bin. Do not forget to give 
# sudo chmod +x i3-copy-scripts
# to make it executable.

# The subroutine that copies files

version=1.1
scripts_file=$HOME/.config/i3/i3-copy-scripts
configs_file=$HOME/.config/i3/i3-copy-configs
# This is the subroutine that performs the copying.
# It checks that the config file exists and is not empty before
# proceeding.

copy_scripts()
{
if [ ! -f $scripts_file ]; then
        echo "The scrips file does not exist!"
        exit 2
elif [ -z `sed -n "1p" $scripts_file` ]; then
        echo "The scripts file is empty!"
        exit 2
else

# Here we copy all the scripts specified in the file
# until we hit an empty line
	line_number=1
	while [ ! -z `sed -n "${line_number}p"  $scripts_file` ]; do
		script=`sed -n "${line_number}p" $scripts_file`

# Evaluate ~ if present
		script="${script/#\~/$HOME}"

		echo "Copying $script..."

# Here we abort if a script specified in the file does not exist
# or cannot be located
		if [ ! -f $script ]; then
			echo "The script '$script' does not exist or I cannot find it."
			echo "Aborting."
			exit 2
		fi

# For security reason we don't want to export executable scripts.
# So we make the script not executable, we pass it to the app vm
# and we make it executable again for dom0. The change of permission must be done
# locally, since invoking it with qvm-run within the appvm causes permission
# problems.	
		sudo chmod -x $script
		qvm-copy-to-vm $1 $script
		sudo chmod +x $script
		let line_number=line_number+1
	done

# Here we copy all the configurations specified in the file
# until we hit an empty line
        line_number=1
        while [ ! -z `sed -n "${line_number}p" $configs_file` ]; do
                config=`sed -n "${line_number}p" $configs_file`

# Evaluate ~ if present
                config="${config/#\~/$HOME}"

		echo "Copying $config..."

# Here we abort if a configuration file specified in the file does not exist
# or cannot be located
                if [ ! -f $config ]; then
                        echo "The configuration file '$config' does not exist or I cannot find it."
                        echo "Aborting."
                        exit 2
                fi

# Here we copy to appvm
		qvm-copy-to-vm $1 $config
                let line_number=line_number+1
        done

fi
}


# Here we give help messages and the like
if [[ $# -le 1 ]]; then
         case $1 in
               -v | --version)
                        echo "$0 - Version $version"
                        exit 0
                        ;;
                -h | --help)
			echo "This script is QubesOS specific."
			echo "It copies all the scripts/configuration files specified in a file to a vm of your choice."
			echo "All the inputs must have the format"
			echo "/path/to/script/script"
			echo "There has to be one input per line, whithout empty lines."
			echo "The scripts to copy have to be specified in the file ~./config/i3/i3-copy-scripts."
			echo "The configuration files to copy have to be specified in the file ~./config/i3/i3-copy-configs."
                        echo "- v, --version            Display current version"
                        echo "-h, --help                Display this message"
                        echo "vmname                    Copies the scripts to vmname appvm"
                        exit 0
                        ;;
		*)
# Gives usage instruction if imput is empty
			if [ -z ${1} ]; then
                        	echo "Usage: $0 {-v, --version|-h, --help|up|toggle|down}"
                                exit 2 
                        fi

# Check if the appvm exists
			if [ ! -d /var/lib/qubes/appvms/${1} ]; then 
				echo "The appvm '$1' does not exist."
				echo "Usage: $0 {-v, --version|-h, --help|up|toggle|down}"
				echo "Aborting."
				exit 2
			else 

# If the appvm exists, it copies the files
			copy_scripts $1	
			fi
	esac
fi