/ install.sh
install.sh
  1  #!/usr/bin/env bash
  2  ###
  3  ### arching-kaos-tools
  4  ### Tools to interact and build an Arching Kaos Infochain
  5  ### Copyright (C) 2021 - 2025  kaotisk
  6  ###
  7  ### This program is free software: you can redistribute it and/or modify
  8  ### it under the terms of the GNU General Public License as published by
  9  ### the Free Software Foundation, either version 3 of the License, or
 10  ### (at your option) any later version.
 11  ###
 12  ### This program is distributed in the hope that it will be useful,
 13  ### but WITHOUT ANY WARRANTY; without even the implied warranty of
 14  ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15  ### GNU General Public License for more details.
 16  ###
 17  ### You should have received a copy of the GNU General Public License
 18  ### along with this program.  If not, see <http://www.gnu.org/licenses/>.
 19  ###
 20  ##
 21  ## Welcome to our ever involving installer
 22  ## We will be as verbose as possible, yet minimal
 23  ##
 24  ## Our default behaviour is to ask the less is needed
 25  ##
 26  ## For minimum overall friction, we will ask sudo access only if it's
 27  ## needed for a missing package.
 28  ##
 29  ## We discourage running the installer with sudo.
 30  ##
 31  clear
 32  
 33  export AK_DEBUG="yes"
 34  fullprogrampath="$(realpath $0)"
 35  PROGRAM="$(basename $0)"
 36  descriptionString="Arching Kaos Tools Installer"
 37  
 38  if [ -d "${HOME}/.arching-kaos" ]
 39  then
 40      printf '%s\n' "Error: Found ~/.arching-kaos directory."
 41      printf '%s\n' "Please backup your previous installationr and rerun ./install.sh."
 42      exit 1
 43  fi
 44  
 45  source ./config.sh
 46  if [ $? -ne 0 ]
 47  then
 48      printf "Error: Sourcing ./config.sh failed"
 49      exit 2
 50  fi
 51  
 52  printf "%s" $(pwd) > wam
 53  WHEREAMI="$(cat wam)"
 54  if [ ! -d $AK_WORKDIR ]
 55  then
 56      mkdir $AK_WORKDIR
 57  else
 58      printf "Error: Found %s.\n" "$AK_WORKDIR"
 59      printf "Please back up your previous installation\n"
 60      printf "and rerun ./install.sh.\n"
 61      exit 3
 62  fi
 63  
 64  source ./lib/_ak_log
 65  source ./lib/_ak_script
 66  _ak_usage
 67  
 68  printf "Installation starts in..."
 69  
 70  
 71  _ak_countdown_seconds 5
 72  
 73  _ak_check_and_create_dir $AK_CONFIGDIR
 74  _ak_check_and_create_dir $AK_BINDIR
 75  _ak_check_and_create_dir $AK_LIBDIR
 76  _ak_check_and_create_dir $AK_MODULESDIR
 77  _ak_check_and_create_dir $AK_ZBLOCKDIR
 78  _ak_check_and_create_dir $AK_BLOCKDIR
 79  _ak_check_and_create_dir $AK_DATADIR
 80  _ak_check_and_create_dir $AK_ARCHIVESDIR
 81  _ak_check_and_create_dir $AK_MINEDBLOCKSDIR
 82  _ak_check_and_create_dir $AK_CACHEDIR
 83  _ak_check_and_create_dir $AK_CHUNKSDIR
 84  _ak_check_and_create_dir $AK_LEAFSDIR
 85  _ak_check_and_create_dir $AK_MAPSDIR
 86  _ak_check_and_create_dir $AK_GPGHOME
 87  _ak_check_and_create_dir $AK_ZPEERSDIR
 88  chmod 700 $AK_GPGHOME
 89  _ak_let_there_be_file $AK_ZLATEST_HISTORY
 90  _ak_let_there_be_file $AK_GENESIS
 91  _ak_let_there_be_file $AK_ZBLOCKSFILE
 92  _ak_let_there_be_file $AK_ZPAIRSFILE
 93  
 94  packageManager=""
 95  installCommand=""
 96  dontAskFlag=""
 97  checkPkgManager(){
 98      declare -a pkgmanagers=("dnf" "apt" "zypper" "pacman" "apk")
 99      printf "Searching for package manager..."
100      for pkgm in "${pkgmanagers[@]}"
101      do
102          printf "? ${pkgm}"
103          which $pkgm 2> /dev/null 1>&2
104          if [ $? -eq 0 ]
105          then
106              printf '\tFound %s\n' "$pkgm"
107              packageManager="$(which $pkgm)"
108              installCommand="install"
109              dontAskFlag="-y"
110              break
111          fi
112      done
113      if [ "$packageManager" == "" ]
114      then
115          printf "Could not find package manager\n"
116          printf "In case you missing a package, you will be informed to install \
117              it manually.\n"
118      fi
119  }
120  checkPkgManager
121  
122  sudoBin="sudo"
123  checkIfSudoAvailable(){
124      which sudo 2> /dev/null 1>&2
125      if [ $? -ne 0 ]
126      then
127          sudoBin=""
128      fi
129  }
130  checkIfSudoAvailable
131  
132  # Depedencies check and install
133  declare -a depedencies=("curl" "bash" "jq" "npm" "gpg" "git" "make" "screen" "gpg-agent")
134  for dep in "${depedencies[@]}"
135  do
136      printf "Checking for %s..." "$dep"
137      which $dep 2> /dev/null 1>&2
138      if [ $? -ne 0 ]
139      then
140          printf "\t Not found!"
141          if [ "$dep" == "npm" ]
142          then
143              curl -o nvm_installer.sh https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh
144              if [ $? -ne 0 ]
145              then
146                  printf "\t Failed to download!\n"
147                  exit 1
148              fi
149              printf "\t Downloaded!"
150              bash nvm_installer.sh
151              if [ $? -ne 0 ]
152              then
153                  printf "\t Failed to install nvm!\n"
154                  exit 1
155              fi
156              printf "\t nvm installed!"
157              export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
158              [ -s "$NVM_DIR/nvm.sh" ] && source "$NVM_DIR/nvm.sh"
159              printf "\t Installing latest nodejs..."
160              nvm install $(nvm ls-remote|tail -n 1|sed -e 's/ *//g')
161              if [ $? -ne 0 ]
162              then
163                  printf "\t Failed to install nodejs!\n"
164                  exit 1
165              fi
166              printf "\t nodejs installed!\n"
167          elif [ "$packageManager" != "" ]
168          then
169              printf "\t Attempting installation..."
170                  $sudoBin $packageManager $installCommand $dontAskFlag $dep > /dev/null 2>&1
171                  if [ $? -ne 0 ]
172                  then
173                      printf "\t Failed to install!\n"
174                      exit 1
175                  fi
176                  printf "\t installed!\n"
177          else
178              printf "\t Don't know how to install!\n\nInstall $dep manually!\n"
179              exit 1
180          fi
181      else
182          printf "\t Found!\n"
183      fi
184  done
185  
186  # Work-around for gpg2 calls on distros that don't provide a link
187  _ak_log_debug "Looking for gpg2 link"
188  which gpg2 > /dev/null 2>&1
189  if [ $? -ne 0 ]
190  then
191      which gpg > /dev/null 2>&1
192      if [ $? -eq 0 ]
193      then
194          _ak_log_debug "Making a gpg2 link"
195          ln -s `which gpg` $AK_BINDIR/gpg2
196      fi
197  fi
198  
199  HAK=".arching-kaos"
200  _ak_log_debug "Searching for $HAK directory"
201  if [ ! -d $HOME/$HAK ]; then
202      mkdir $HOME/$HAK
203      _ak_log_debug "$HAK created in $HOME";
204  fi
205  
206  _ak_log_debug "Searching for rc"
207  if [ ! -f $HOME/$HAK/rc ]; then
208      echo export PATH=$PATH:$HOME/$HAK/bin > $HOME/$HAK/rc
209      cat config.sh >> $HOME/$HAK/rc
210      _ak_log_debug "New rc export to file";
211  fi
212  
213  _ak_log_debug "Searching for shell"
214  if [ -f "~/.zshrc" ]
215  then
216      SHELLRC="${HOME}/zshrc"
217      _ak_log_debug "ZSH found";
218  elif [ -f "${HOME}/.bashrc" ]
219  then
220      SHELLRC="${HOME}/.bashrc"
221      _ak_log_debug "BASH found";
222  else
223      _ak_log_debug "Unknown shell... defaulting to ~/.shrc"
224      SHELLRC="${HOME}/.shrc"
225  fi
226  
227  _ak_log_debug "Searching if rc is already there"
228  grep "source $HOME/$HAK/rc" $SHELLRC > /dev/null 2>&1
229  if [ $? -eq 0 ]
230  then
231      _ak_log_debug "Already installed";
232  else
233      echo "source $HOME/$HAK/rc" >> $SHELLRC
234      _ak_log_debug "$HAK installed at $HOME and sourced it in $SHELLRC"
235      source $HOME/$HAK/rc;
236  fi
237  
238  bash update.sh
239  source ./lib/_ak_ipfs
240  _ak_ipfs_check_and_install
241  source ./config.sh
242  source $HOME/$SHELLRC
243  bash init.sh
244  cd api
245  npm i
246  cd ..
247  make
248  if [ $? -ne 0 ]
249  then
250      printf 'Building API daemon failed\n'
251      exit 1
252  else
253      ln -s $WHEREAMI/build/ak-daemon $AK_BINDIR/ak-daemon
254  fi