freedombone-app-emacs 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Emacs application
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. VARIANTS='full'
  31. IN_DEFAULT_INSTALL=1
  32. SHOW_ON_ABOUT=0
  33. emacs_variables=(USB_MOUNT
  34. MY_USERNAME
  35. DEFAULT_LANGUAGE)
  36. function logging_on_emacs {
  37. echo -n ''
  38. }
  39. function logging_off_emacs {
  40. echo -n ''
  41. }
  42. function reconfigure_emacs {
  43. echo -n ''
  44. }
  45. function upgrade_emacs {
  46. echo -n ''
  47. }
  48. function backup_local_emacs {
  49. for d in /home/*/ ; do
  50. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  51. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  52. if [ -d "/home/$USERNAME/.emacs.d" ]; then
  53. echo $"Backing up Emacs config for $USERNAME"
  54. if [ -f "/home/$USERNAME/.emacs" ]; then
  55. cp "/home/$USERNAME/.emacs" "/home/$USERNAME/.emacs.d/dotemacs"
  56. fi
  57. function_check backup_directory_to_usb
  58. backup_directory_to_usb "/home/$USERNAME/.emacs.d" "emacs/$USERNAME"
  59. fi
  60. fi
  61. done
  62. }
  63. function restore_local_emacs {
  64. temp_restore_dir=/root/tempemacs
  65. if [ -d "$USB_MOUNT/backup/emacs" ]; then
  66. for d in $USB_MOUNT/backup/emacs/*/ ; do
  67. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  68. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  69. if [ ! -d "/home/$USERNAME" ]; then
  70. "${PROJECT_NAME}-adduser" "$USERNAME"
  71. fi
  72. echo $"Restoring Emacs config for $USERNAME"
  73. function_check restore_directory_from_usb
  74. restore_directory_from_usb "$temp_restore_dir" "emacs/$USERNAME"
  75. if [ -d "$temp_restore_dir/home/$USERNAME/.emacs.d" ]; then
  76. cp -r "$temp_restore_dir/home/$USERNAME/.emacs.d" "/home/$USERNAME/"
  77. else
  78. if [ ! -d "/home/$USERNAME/.emacs.d" ]; then
  79. mkdir "/home/$USERNAME/.emacs.d"
  80. fi
  81. cp -r "$temp_restore_dir/"* "/home/$USERNAME/.emacs.d"
  82. fi
  83. # shellcheck disable=SC2181
  84. if [ ! "$?" = "0" ]; then
  85. rm -rf $temp_restore_dir
  86. function_check set_user_permissions
  87. set_user_permissions
  88. function_check backup_unmount_drive
  89. backup_unmount_drive
  90. exit 664
  91. fi
  92. if [ -d "$temp_restore_dir/home/$USERNAME/.emacs.d" ]; then
  93. cp -f "$temp_restore_dir/home/$USERNAME/.emacs.d/dotemacs" "/home/$USERNAME/.emacs"
  94. else
  95. cp -f "$temp_restore_dir/dotemacs" "/home/$USERNAME/.emacs"
  96. fi
  97. rm -rf $temp_restore_dir
  98. fi
  99. done
  100. fi
  101. }
  102. function backup_remote_emacs {
  103. for d in /home/*/ ; do
  104. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  105. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  106. if [ -d "/home/$USERNAME/.emacs.d" ]; then
  107. echo $"Backing up Emacs config for $USERNAME"
  108. if [ -f "/home/$USERNAME/.emacs" ]; then
  109. cp "/home/$USERNAME/.emacs" "/home/$USERNAME/.emacs.d/dotemacs"
  110. fi
  111. function_check backup_directory_to_friend
  112. backup_directory_to_friend "/home/$USERNAME/.emacs.d" "emacs/$USERNAME"
  113. fi
  114. fi
  115. done
  116. }
  117. function restore_remote_emacs {
  118. temp_restore_dir=/root/tempemacs
  119. if [ -d "$USB_MOUNT/backup/emacs" ]; then
  120. for d in $USB_MOUNT/backup/emacs/*/ ; do
  121. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  122. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  123. if [ ! -d "/home/$USERNAME" ]; then
  124. "${PROJECT_NAME}-adduser" "$USERNAME"
  125. fi
  126. echo $"Restoring Emacs config for $USERNAME"
  127. function_check restore_directory_from_friend
  128. restore_directory_from_friend "$temp_restore_dir" "emacs/$USERNAME"
  129. if [ -d "$temp_restore_dir/home/$USERNAME/.emacs.d" ]; then
  130. cp -r "$temp_restore_dir/home/$USERNAME/.emacs.d" "/home/$USERNAME/"
  131. else
  132. if [ ! -d "/home/$USERNAME/.emacs.d" ]; then
  133. mkdir "/home/$USERNAME/.emacs.d"
  134. fi
  135. cp -r "$temp_restore_dir/"* "/home/$USERNAME/.emacs.d/"
  136. fi
  137. # shellcheck disable=SC2181
  138. if [ ! "$?" = "0" ]; then
  139. rm -rf $temp_restore_dir
  140. function_check set_user_permissions
  141. set_user_permissions
  142. function_check backup_unmount_drive
  143. backup_unmount_drive
  144. exit 664
  145. fi
  146. if [ -d "$temp_restore_dir/home/$USERNAME/.emacs.d" ]; then
  147. cp -f "$temp_restore_dir/home/$USERNAME/.emacs.d/dotemacs" "/home/$USERNAME/.emacs"
  148. else
  149. cp -f "$temp_restore_dir/dotemacs" "/home/$USERNAME/.emacs"
  150. fi
  151. rm -rf $temp_restore_dir
  152. fi
  153. done
  154. fi
  155. }
  156. function remove_emacs {
  157. apt-get -yq remove --purge emacs
  158. update-alternatives --set editor /usr/bin/nano
  159. sed -i '/install_emacs/d' "$COMPLETION_FILE"
  160. # remove emacs as the mutt email editor
  161. if [ -f /etc/Muttrc ]; then
  162. if grep -q "set editor=" /etc/Muttrc; then
  163. sed -i '/set editor=/d' /etc/Muttrc
  164. fi
  165. for d in /home/*/ ; do
  166. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  167. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  168. if [ -f "/home/$USERNAME/.muttrc" ]; then
  169. if grep -q "set editor=" "/home/$USERNAME/.muttrc"; then
  170. sed -i '/set editor=/d' "/home/$USERNAME/.muttrc"
  171. fi
  172. fi
  173. fi
  174. done
  175. fi
  176. }
  177. function install_emacs {
  178. apt-get -yq install emacs ispell ibritish ifrench ispanish iitalian irussian iswedish inorwegian iirish ingerman iswiss iogerman idutch idanish ibrazilian ibulgarian ipolish iczech iestonian ilithuanian iukrainian icatalan
  179. update-alternatives --set editor /usr/bin/emacs24
  180. # A minimal emacs configuration
  181. #echo -n "(add-to-list 'load-path " > /home/$MY_USERNAME/.emacs
  182. #echo '"~/.emacs.d/")' >> /home/$MY_USERNAME/.emacs
  183. #echo '' >> /home/$MY_USERNAME/.emacs
  184. { echo $';; ===== Remove trailing whitepace ======================================';
  185. echo '';
  186. echo ";;(add-hook 'before-save-hook 'delete-trailing-whitespace)";
  187. echo '';
  188. echo ';; Goto a line number with CTRL-l';
  189. echo -n '(global-set-key "\C-l" ';
  190. echo "'goto-line)";
  191. echo '';
  192. echo $';; ===== Show line numbers ==============================================';
  193. echo '';
  194. echo "(add-hook 'find-file-hook (lambda () (linum-mode 1)))";
  195. echo '';
  196. echo $';; ===== Enable line wrapping in org-mode ===============================';
  197. echo '';
  198. echo " (add-hook 'org-mode-hook";
  199. echo " '(lambda ()";
  200. echo " (visual-line-mode 1)))";
  201. echo '';
  202. echo $';; ===== Enable shift select in org mode ================================';
  203. echo '';
  204. echo '(setq org-support-shift-select t)';
  205. echo '';
  206. echo $';; ===== Set standard indent to 4 rather that 4 =========================';
  207. echo '';
  208. echo '(setq standard-indent 4)';
  209. echo '(setq-default tab-width 4)';
  210. echo '(setq c-basic-offset 4)';
  211. echo '';
  212. echo $';; ===== Support Wheel Mouse Scrolling ==================================';
  213. echo '';
  214. echo '(mouse-wheel-mode t)';
  215. echo '';
  216. echo $';; ===== Place Backup Files in Specific Directory =======================';
  217. echo '';
  218. echo '(setq make-backup-files t)';
  219. echo '(setq version-control t)';
  220. echo '(setq backup-directory-alist (quote ((".*" . "~/.emacs_backups/"))))';
  221. echo '';
  222. echo $';; ===== Make Text mode the default mode for new buffers ================';
  223. echo '';
  224. echo "(setq default-major-mode 'text-mode)";
  225. echo '';
  226. echo $';; ===== Line length ====================================================';
  227. echo '';
  228. echo '(setq-default fill-column 72)';
  229. echo '';
  230. echo $';; ===== Enable Line and Column Numbering ===============================';
  231. echo '';
  232. echo '(line-number-mode 1)';
  233. echo '(column-number-mode 1)';
  234. echo '';
  235. echo $';; ===== Turn on Auto Fill mode automatically in all modes ==============';
  236. echo '';
  237. echo ';; Auto-fill-mode the the automatic wrapping of lines and insertion of';
  238. echo ';; newlines when the cursor goes over the column limit.';
  239. echo '';
  240. echo ';; This should actually turn on auto-fill-mode by default in all major';
  241. echo ';; modes. The other way to do this is to turn on the fill for specific modes';
  242. echo ';; via hooks.';
  243. echo '';
  244. echo '(setq auto-fill-mode 1)';
  245. echo '';
  246. echo $';; ===== Enable GPG encryption =========================================';
  247. echo '';
  248. echo "(require 'epa)";
  249. echo '(epa-file-enable)'; } >> "/home/$MY_USERNAME/.emacs"
  250. cp "/home/$MY_USERNAME/.emacs" /root/.emacs
  251. chown "$MY_USERNAME":"$MY_USERNAME" "/home/$MY_USERNAME/.emacs"
  252. # add a mutt entry to use emacs to compose emails
  253. if [ -f /etc/Muttrc ]; then
  254. if ! grep -q "set editor=" /etc/Muttrc; then
  255. echo 'set editor="emacs -q --load ~/.emacs-mutt"' >> /etc/Muttrc
  256. else
  257. sed -i 's|set editor=.*|set editor="emacs -q --load ~/.emacs-mutt"|g' /etc/Muttrc
  258. fi
  259. for d in /home/*/ ; do
  260. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  261. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  262. if [ -f "/home/$USERNAME/.muttrc" ]; then
  263. if ! grep -q "set editor=" "/home/$USERNAME/.muttrc"; then
  264. echo 'set editor="emacs -q --load ~/.emacs-mutt"' >> "/home/$USERNAME/.muttrc"
  265. else
  266. sed -i 's|set editor=.*|set editor="emacs -q --load ~/.emacs-mutt"|g' "/home/$USERNAME/.muttrc"
  267. fi
  268. fi
  269. # create an Emacs configuration specifically for use with Mutt, which
  270. # has word wrap and spell checking on by default
  271. if [ ! -f "/home/$USERNAME/.emacs-mutt" ]; then
  272. { echo "(add-hook 'before-save-hook 'delete-trailing-whitespace)";
  273. echo '(setq org-support-shift-select t)';
  274. echo '(setq standard-indent 4)';
  275. echo '(setq-default tab-width 4)';
  276. echo '(setq c-basic-offset 4)';
  277. echo '(mouse-wheel-mode t)';
  278. echo '(setq make-backup-files t)';
  279. echo '(setq version-control t)';
  280. echo '(setq backup-directory-alist (quote ((".*" . "~/.emacs_backups/"))))';
  281. echo "(setq default-major-mode 'text-mode)";
  282. echo "(dolist (hook '(text-mode-hook))";
  283. echo ' (add-hook hook (lambda () (flyspell-mode 1))))';
  284. echo '(setq-default fill-column 72)'; } > "/home/$USERNAME/.emacs-mutt"
  285. if [[ $DEFAULT_LANGUAGE == 'en_US'* ]]; then
  286. echo '(setq ispell-dictionary "american")' >> "/home/$USERNAME/.emacs-mutt"
  287. fi
  288. if [[ $DEFAULT_LANGUAGE == 'en_GB'* ]]; then
  289. echo '(setq ispell-dictionary "british")' >> "/home/$USERNAME/.emacs-mutt"
  290. fi
  291. if [[ $DEFAULT_LANGUAGE == 'fr_FR'* ]]; then
  292. echo '(setq ispell-dictionary "french")' >> "/home/$USERNAME/.emacs-mutt"
  293. fi
  294. if [[ $DEFAULT_LANGUAGE == 'it_IT'* ]]; then
  295. echo '(setq ispell-dictionary "italian")' >> "/home/$USERNAME/.emacs-mutt"
  296. fi
  297. if [[ $DEFAULT_LANGUAGE == 'cs_CZ'* ]]; then
  298. echo '(setq ispell-dictionary "czech")' >> "/home/$USERNAME/.emacs-mutt"
  299. fi
  300. if [[ $DEFAULT_LANGUAGE == 'da_DK'* ]]; then
  301. echo '(setq ispell-dictionary "danish")' >> "/home/$USERNAME/.emacs-mutt"
  302. fi
  303. if [[ $DEFAULT_LANGUAGE == 'nl_NL'* ]]; then
  304. echo '(setq ispell-dictionary "dutch")' >> "/home/$USERNAME/.emacs-mutt"
  305. fi
  306. if [[ $DEFAULT_LANGUAGE == 'ru_RU'* ]]; then
  307. echo '(setq ispell-dictionary "russian")' >> "/home/$USERNAME/.emacs-mutt"
  308. fi
  309. if [[ $DEFAULT_LANGUAGE == 'es_ES'* ]]; then
  310. echo '(setq ispell-dictionary "spanish")' >> "/home/$USERNAME/.emacs-mutt"
  311. fi
  312. if [[ $DEFAULT_LANGUAGE == 'sv_SE'* ]]; then
  313. echo '(setq ispell-dictionary "swedish")' >> "/home/$USERNAME/.emacs-mutt"
  314. fi
  315. if [[ $DEFAULT_LANGUAGE == 'no_NO'* ]]; then
  316. echo '(setq ispell-dictionary "norwegian")' >> "/home/$USERNAME/.emacs-mutt"
  317. fi
  318. if [[ $DEFAULT_LANGUAGE == 'de_DE'* ]]; then
  319. echo '(setq ispell-dictionary "ngerman")' >> "/home/$USERNAME/.emacs-mutt"
  320. fi
  321. if [[ $DEFAULT_LANGUAGE == 'bg_BG'* ]]; then
  322. echo '(setq ispell-dictionary "bulgarian")' >> "/home/$USERNAME/.emacs-mutt"
  323. fi
  324. if [[ $DEFAULT_LANGUAGE == 'pl'* ]]; then
  325. echo '(setq ispell-dictionary "polish")' >> "/home/$USERNAME/.emacs-mutt"
  326. fi
  327. if [[ $DEFAULT_LANGUAGE == 'et_EE'* ]]; then
  328. echo '(setq ispell-dictionary "estonian")' >> "/home/$USERNAME/.emacs-mutt"
  329. fi
  330. if [[ $DEFAULT_LANGUAGE == 'lt_LT'* ]]; then
  331. echo '(setq ispell-dictionary "lithuanian")' >> "/home/$USERNAME/.emacs-mutt"
  332. fi
  333. if [[ $DEFAULT_LANGUAGE == 'uk_UA'* ]]; then
  334. echo '(setq ispell-dictionary "ukranian")' >> "/home/$USERNAME/.emacs-mutt"
  335. fi
  336. if [[ $DEFAULT_LANGUAGE == 'ca_ES'* ]]; then
  337. echo '(setq ispell-dictionary "catalan")' >> "/home/$USERNAME/.emacs-mutt"
  338. fi
  339. { echo '(setq auto-fill-mode 0)';
  340. echo "(add-hook 'text-mode-hook 'turn-on-auto-fill)";
  341. echo "(setq-default auto-fill-function 'do-auto-fill)"; } >> "/home/$USERNAME/.emacs-mutt"
  342. chown "$USERNAME":"$USERNAME" "/home/$USERNAME/.emacs-mutt"
  343. # add the emacs mutt configuration to the user profile skeleton
  344. if [ ! -f /etc/skel/.emacs-mutt ]; then
  345. cp "/home/$USERNAME/.emacs-mutt" /etc/skel/.emacs-mutt
  346. chown root:root /etc/skel/.emacs-mutt
  347. fi
  348. fi
  349. fi
  350. done
  351. fi
  352. APP_INSTALLED=1
  353. }
  354. function install_interactive_emacs {
  355. install_emacs
  356. }
  357. # NOTE: deliberately no exit 0