freedombone-app-emacs 16KB

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