update_bash(){
  # [ -e ~/.bashrc ] && rm -f .bashrc*
  # [ -e ~/.profile ] && rm -f .profile*
  wget -P ~/ -O .bashrc $SERVER_NAME/files/misc/.bashrc;
  # wget -P ~/ $SERVER_NAME/misc/.profile;
 }

update_vim(){
  # [ -e ~/.vimrc ] && rm -f .vimrc*
  wget -P ~/ -O .vimrc $SERVER_NAME/files/misc/.vimrc;
}

update_tmux(){
  echo TMUX_LOC is $TMUX_LOC
  [ -e ~/.tmux.conf ] && rm -f ~/.tmux.conf*;
  wget -P ~/ $SERVER_NAME/files/misc/.tmux.conf;
  # tmux source ~/.tmux.conf
  if [ ! -d $TMUX_LOC/plugins/tpm ]; then
    echo -e "\ntpm not detecting. Cloning..."
    git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm;
  fi
  $TMUX_LOC/plugins/tpm/bin/install_plugins
}
alias dl_makefile='rm -f makefile; wget "$SERVER_NAME"/files/misc/makefile'
update_terminator(){
  if [ ! -d "$TERMINATOR_LOC" ]; then
    mkdir -p $TERMINATOR_LOC
  fi
  wget -O $TERMINATOR_LOC/config $SERVER_NAME/files/misc/terminator_config;
  # mv $TERMINATOR_LOC/terminator_config $TERMINATOR_LOC/config;
}
update_alacritty(){
  if [ ! -f "$ALACRITTY_LOC" ]; then
    mkdir -p "$ALACRITTY_LOC"
  fi
  wget -O $ALACRITTY_LOC/alacritty.yml $SERVER_NAME/files/misc/alacritty.yml
}

update_all(){
  if [ -z $SCRIPTS_LOC ]; then
    [ ! -d ~/.config/TEMP ] mkdir ~/.config/TEMP;
    wget -P ~/.config/TEMP $SERVER_NAME/misc/scripts/globs.sh;
    . ~/.config/TEMP/globs.sh
    echo -e "\n\nLoaded global variables\n\n"
  fi
  update_scripts
  . $SCRIPTS_LOC/load_bash.sh
  echo -e "Loaded load_bash.sh"
  load_scripts
	update_bash
	update_vim
  update_tmux
  update_terminator
  echo "DONE!!!! Loading .bashrc..."
  lb
}

update_scripts(){
  local dir=$(pwd);
  if [ ! -d $SCRIPTS_LOC/.git ]; then
    rm -rf $SCRIPTS_LOC;
    git clone ssh://$GIT_USR@$SERVER_NAME:$GIT_PORT/srv/git/scripts $SCRIPTS_LOC;
  else
    cd $SCRIPTS_LOC
    git pull origin master
  fi

  # git fetch --all;
  # git reset --hard origin/master

  # [ -d $SCRIPTS_LOC ] && rm -rf $SCRIPTS_LOC;
  # cd ~/.config;
	# wgetod https://www.$SERVER_NAME/misc/scripts/ sh;
  # if [ ! -d $SCRIPTS_LOC ]; then
    # echo -e "\nError! Couldn't create $SCRIPTS_LOC"
  # fi
	cd $dir;
}

wgetod() {
  echo "inside wgetod"
  local use_only=""
  [ ! -z $2 ] && use_only="-A $2"
  
  NSLASH="$(echo "$1" | perl -pe 's|.*://[^/]+(.*?)/?$|\1|' | grep -o / | wc -l)"
  NCUT=$((NSLASH > 0 ? NSLASH-1 : 0))
  wget -r -nH -erobots=off --user-agent=Mozilla/5.0 --cut-dirs=$NCUT --no-parent --reject="index.html*" "$1" $use_only;
}

lb() {
  . ~/.bashrc;
  [ -d $SCRIPTS_LOC ] && load_scripts;
  c
}
load_scripts() {
for file in $SCRIPTS_LOC/*
  do
    . $file
done
}
