i3-copy-scripts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/bin/bash
  2. #This script copies all the scripts specified in a file to a vm of your choice.
  3. #This is a qubes-os specific script.
  4. # i3-copy-scripts - Copy scripts to a vm of your choice.
  5. # Copyright (C) 2017 Fabrizio Romano Genovese <egonigredo@gmail.com>
  6. # This copyrighted material is made available to anyone wishing to use,
  7. # modify, copy, or redistribute it subject to the terms and conditions of
  8. # the GNU General Public License v.2, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful, but WITHOUT
  11. # ANY WARRANTY expressed or implied, including the implied warranties of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  13. # Public License for more details.
  14. # You should have received a copy of the
  15. # GNU General Public License along with this program; if not,
  16. # see <http://www.gnu.org/licenses/>
  17. # Put this file in /bin/ or in /usr/bin. Do not forget to give
  18. # sudo chmod +x i3-copy-scripts
  19. # to make it executable.
  20. # The subroutine that copies files
  21. version=1.1
  22. scripts_file=$HOME/.config/i3/i3-copy-scripts
  23. configs_file=$HOME/.config/i3/i3-copy-configs
  24. # This is the subroutine that performs the copying.
  25. # It checks that the config file exists and is not empty before
  26. # proceeding.
  27. checker()
  28. {
  29. vmstate=`qvm-check $1 | grep "not"`
  30. if [ -z "$vmstate" ]; then
  31. echo "VM $1 exists. Proceeding."
  32. else
  33. echo "The VM $1 does not exist. Aborting."
  34. echo "Usage: $0 {-v, --version|-h, --help| vmname}"
  35. exit 2
  36. fi
  37. }
  38. copy_scripts()
  39. {
  40. if [ ! -f $scripts_file ]; then
  41. echo "The scrips file does not exist!"
  42. exit 2
  43. elif [ -z `sed -n "1p" $scripts_file` ]; then
  44. echo "The scripts file is empty!"
  45. exit 2
  46. else
  47. # Here we copy all the scripts specified in the file
  48. # until we hit an empty line
  49. line_number=1
  50. while [ ! -z `sed -n "${line_number}p" $scripts_file` ]; do
  51. script=`sed -n "${line_number}p" $scripts_file`
  52. # Evaluate ~ if present
  53. script="${script/#\~/$HOME}"
  54. echo "Copying $script..."
  55. # Here we abort if a script specified in the file does not exist
  56. # or cannot be located
  57. if [ ! -f $script ]; then
  58. echo "The script '$script' does not exist or I cannot find it."
  59. echo "Aborting."
  60. exit 2
  61. fi
  62. # For security reason we don't want to export executable scripts.
  63. # So we make the script not executable, we pass it to the app vm
  64. # and we make it executable again for dom0. The change of permission must be done
  65. # locally, since invoking it with qvm-run within the appvm causes permission
  66. # problems.
  67. sudo chmod -x $script
  68. qvm-copy-to-vm $1 $script
  69. sudo chmod +x $script
  70. let line_number=line_number+1
  71. done
  72. # Here we copy all the configurations specified in the file
  73. # until we hit an empty line
  74. line_number=1
  75. while [ ! -z `sed -n "${line_number}p" $configs_file` ]; do
  76. config=`sed -n "${line_number}p" $configs_file`
  77. # Evaluate ~ if present
  78. config="${config/#\~/$HOME}"
  79. echo "Copying $config..."
  80. # Here we abort if a configuration file specified in the file does not exist
  81. # or cannot be located
  82. if [ ! -f $config ]; then
  83. echo "The configuration file '$config' does not exist or I cannot find it."
  84. echo "Aborting."
  85. exit 2
  86. fi
  87. # Here we copy to appvm
  88. qvm-copy-to-vm $1 $config
  89. let line_number=line_number+1
  90. done
  91. fi
  92. }
  93. # Here we give help messages and the like
  94. if [[ $# -le 1 ]]; then
  95. case $1 in
  96. -v | --version)
  97. echo "$0 - Version $version"
  98. exit 0
  99. ;;
  100. -h | --help)
  101. echo "This script is QubesOS specific.
  102. Usage: $0 {-v, --version|-h, --help| vmname}.
  103. It copies all the scripts/configuration files specified in a file to a vm of your choice. All the inputs must have the format
  104. /path/to/script/script
  105. There has to be one input per line, whithout empty lines.
  106. The scripts to copy have to be specified in the file
  107. ~./config/i3/i3-copy-scripts.
  108. The configuration files to copy have to be specified in the file ~./config/i3/i3-copy-configs.
  109. - v, --version Display current version
  110. -h, --help Display this message
  111. vmname Copies the scripts to vmname appvm"
  112. exit 0
  113. ;;
  114. *)
  115. # Gives usage instruction if imput is empty
  116. if [ -z ${1} ]; then
  117. echo "Usage: $0 {-v, --version|-h, --help|up|toggle|down}"
  118. exit 2
  119. fi
  120. # Check if the appvm exists
  121. checker $1
  122. # If the appvm exists, it copies the files
  123. copy_scripts $1
  124. esac
  125. fi