1 #!/bin/bash
  2 
  3 # DMT 1.2.210 · 2023-08-29
  4 #
  5 # *** ONE-LINE INSTALL ***
  6 #
  7 # curl get-dmt.com | bash
  8 #
  9 # Paste this command into your bash shell on macOS or Linux (Debian, Raspbian etc.)
 10 #
 11 # More info : https://dmt-system.com
 12 # Basic demo: http://get-dmt.com/home
 13 #
 14 # Requirements:
 15 #
 16 # - curl (linux: sudo apt-get install curl)
 17 # - node.js > v14.0 (macOS / linux: curl -L https://git.io/n-install | bash)
 18 # - mpv.io (for multimedia play)
 19 #
 20 # RPi guide:
 21 # https://github.com/uniqpath/info/blob/master/assets/pdf/rpi_guide.pdf
 22 
 23 # DEFINE COLORS
 24 YELLOW='\033[0;33m'
 25 GREEN='\033[0;32m'
 26 RED='\033[0;31m'
 27 BLUE='\033[0;94m'
 28 CYAN='\e[0;36m'
 29 MAGENTA='\033[0;35m'
 30 GRAY='\e[1;30m'
 31 WHITE='\e[33;97m'
 32 NC='\033[0m' # No Color
 33 
 34 function get_dmt_proc_pid 
 35   local proc=$(ps -ef | grep " dmt-proc" | grep -v "nano dmt-proc" | grep -v "eslint" | grep -v "manager.js" | grep -v "command -v openssl >/dev/null" | grep -v grep | awk '{print $2}')
 36   local procFG=$(ps -ef | grep "controller/processes/dmt-proc.js" | grep -v "eslint" | grep -v "command -v openssl >/dev/null" | grep -v grep | awk '{print $2}')
 37 
 38   if [ -n "$proc" ]; then
 39     eval "$1='$proc'"
 40     eval "$2='bg'"
 41     return
 42   fi
 43 
 44   # dmt process running in foreground
 45   if [ -n "$procFG" ]; then
 46     eval "$1='$procFG'"
 47     eval "$2='fg'"
 48   fi
 49 }
 50 
 51 function report_dmt_running 
 52   local dmt_proc_pid="$1"
 53   local fg_bg="$2"
 54 
 55   if [ -n "$dmt_proc_pid" ]; then
 56     local fg_bg_msg=""
 57     if [ "$fg_bg" == 'fg' ]; then
 58       fg_bg_msg="${MAGENTA}in foreground${NC} "
 59     fi
 60     printf "${GREEN}✓ ${CYAN}dmt-proc${NC} ${GREEN}seems to be running ${fg_bg_msg}with ${YELLOW}pid ${dmt_proc_pid}${NC}\n"
 61     return 0
 62   fi
 63 
 64   return 1
 65 }
 66 
 67 function empty_backup_dir 
 68   local backup_dir="$1"
 69 
 70   if [ -d "$backup_dir" ]; then
 71     printf "${backup_dir} already existed, purging it ...${NC}\n"
 72     rm -rf "$backup_dir"
 73 
 74     if [ -d "$backup_dir" ]; then
 75       printf "${RED}${backup_dir} already exists and cannot delete it! Aborting operation ...${NC}\n"
 76       exit
 77     fi
 78   fi
 79 
 80   mkdir "$backup_dir"
 81 }
 82 
 83 function move_dmt_engine 
 84   local source="$1"
 85   local target="$2"
 86 
 87   local cwd="`pwd`"
 88   cd "$source"
 89 
 90   shopt -s dotglob # always include hidden files
 91   for entry in *; do
 92     if [ "$entry" != '.' ] && [ "$entry" != '..' ] && [ "$entry" != 'user' ] && [ "$entry" != 'state' ] && [ "$entry" != 'log' ]; then
 93       mv $entry $target
 94     fi
 95   done
 96   shopt -u dotglob
 97 
 98   cd "$cwd"
 99 }
100 
101 if [ -f ~/.dmt/.prevent_dmt_next ]; then
102   printf "${RED}Prevented operation because ~/.dmt/.prevent_dmt_next file is present, please remove it to continue${NC}\n"
103   exit
104 fi
105 
106 # abort if dmt-proc is currently running
107 
108 dmt_proc_pid=''
109 dmt_fg_bg=''
110 get_dmt_proc_pid dmt_proc_pid dmt_fg_bg
111 
112 if report_dmt_running "$dmt_proc_pid" "$dmt_fg_bg"; then
113   printf "${RED}Please stop it and try again${NC}\n"
114   exit
115 fi
116 
117 cwd="`pwd`"
118 
119 cd $(mktemp -d)
120 mkdir dmt-next && cd $_
121 
122 # we are now in /some_temp_directory/dmt-next
123 
124 echo
125 printf "${WHITE}Fetching ${MAGENTA}dmt.zip ${WHITE}from ${CYAN}get-dmt.com${NC} ...\n\n"
126 printf "${YELLOW}Please wait for up to a minute even if nothing seems to be happening ...${NC}\n"
127 
128 # STEP 1 → fetch dmt.zip
129 if curl -L --fail http://get-dmt.com/dmt.zip -o dmt.zip; then
130 
131   # STEP 2 → unzip dmt.zip into /some_temp_directory/dmt-next
132   # we now have a complete dmt directory without user, state and log subdirectories
133   unzip dmt.zip
134 
135   if [ $? -ne 0 ]; then # error
136     printf "${RED}Error when unzipping dmt.zip, exiting ...${NC}\n"
137 
138     cd ..
139     rm -rf dmt-next
140 
141     cd "$cwd"
142     exit
143   fi
144 
145   rm dmt.zip
146 
147   # STEP 3 → move contents of our current ~/.dmt  directory (without user, state and log) into ~/.dmt-backup-[timestamp]
148   #          or create ~/.dmt if we don't currently have it
149 
150   now=$(date +"%Y-%m-%dT%H-%M-%S")
151   backup_dir="$HOME/.dmt-backup-${now}"
152   empty_backup_dir "$backup_dir"
153 
154   if [ -d ~/.dmt ]; then
155     move_dmt_engine ~/.dmt "$backup_dir"
156   else
157     mkdir ~/.dmt
158 
159     if [ ! -d ~/.dmt ]; then
160       printf "${RED}Could not create ~/.dmt directory, exiting.${NC}\n"
161       exit
162     fi
163   fi
164 
165   # STEP 4 → move dmt-next/* into ~/.dmt
166   # ( we are still in /some_temp_directory/dmt-next )
167 
168   move_dmt_engine . ~/.dmt
169 
170   # potential open issues:
171   # unsolved fail PC-DMT 26.3.2021: Cannot move `core` to '/home/iztok/.dmt/core': Permission denied
172   # fix: dmt next --force
173 
174   if [ ! -f ~/.dmt/.version ]; then # should never come here!
175     printf "${RED}Panic: could not install / update DMT: ~/.dmt/.version file was not copied over, exiting.${NC}\n"
176     exit
177   fi
178 
179   if [ ! -d ~/.dmt/shell ]; then # should never come here!
180     printf "${RED}Panic: could not install / update DMTL ~/.dmt/shell directory was not copied over, exiting.${NC}\n"
181 
182     if [ -d "$backup_dir" ]; then
183       move_dmt_engine "$backup_dir" ~/.dmt
184       rm -rf "$backup_dir"
185     fi
186 
187     exit
188   fi
189 
190   # STEP 5 → cleanup
191 
192   if [ -d "$backup_dir" ]; then
193     rm -rf "$backup_dir"
194   fi
195 
196   # STEP 6 → directory is ready, run install
197 
198   cd ~/.dmt
199 
200   ./install
201 
202   # STEP 7 → update dmtSource pointer
203 
204   # add dmtSource pointer to user.def so that "dmt next" updates work correctly
205   user_def=~/.dmt/user/def/user.def
206   if [ -f "$user_def" ]; then
207     if [ -n "get-dmt.com" ]; then
208       if ! grep -Fq "dmtSource" "$user_def"; then
209         echo "  dmtSource: get-dmt.com" >> "$user_def"
210       fi
211     fi
212   fi
213 
214 else
215   printf "${RED}Failed to fetch dmt.zip, exiting ...${NC}\n"
216 fi
217 
218 cd "$cwd"
219