install_zsh.sh

1#!/usr/bin/env bash
2
3# Function to get the base name of a file
4basename() {
5  echo "${1##*/}"
6}
7
8# Display usage information
9usage() {
10  cat <<EOF
11Usage: $(basename "$0") [options]
12Installs the zshrc file.
13
14Options:
15  -h, --help    Display this help message.
16EOF
17}
18
19check_if_root() {
20  if [[ $EUID -eq 0 ]]; then
21    echo "Error: This script should not be run as root." 1>&2
22    exit 1
23  fi
24}
25
26# Function to finalize the installation
27finalize() {
28  local zsh_path
29  zsh_path=$(command -v zsh)
30
31  if [[ -z "$zsh_path" ]]; then
32    echo "Error: zsh not found."
33    exit 1
34  fi
35
36  echo "Changing the default shell to zsh..."
37  chsh -s "$zsh_path" || { echo "Error: Unable to change the shell."; exit 1; }
38
39  echo "Sourcing the new zsh configuration..."
40  source ~/.zshrc || { echo "Error: Unable to source ~/.zshrc."; exit 1; }
41
42  echo "Installation complete! Please restart your terminal to apply the changes."
43}
44
45# Check if required dependencies are installed
46check_dependencies() {
47  local dependencies=("git" "zsh" "curl")
48  for cmd in "${dependencies[@]}"; do
49    if ! command -v "$cmd" > /dev/null 2>&1; then
50      echo "Error: $cmd is not installed. Please install it using your package manager."
51      exit 1
52    fi
53  done
54}
55
56# Main function to install the zshrc file
57install_zshrc() {
58  local repo_url="https://github.com/chocoOnEstrogen/dotfiles"
59  local tmp_dir
60  tmp_dir=$(mktemp -d)
61
62  echo "Cloning the dotfiles repository..."
63  git clone "$repo_url" "$tmp_dir" || { echo "Error: Failed to clone the repository."; exit 1; }
64
65  echo "Copying the zshrc file..."
66  cp "$tmp_dir/machines/personal/assets/zsh/.zshrc" "$HOME/" || {
67    echo "Error: Failed to copy the zshrc file."; exit 1;
68  }
69
70  echo "Cleaning up temporary files..."
71  rm -rf "$tmp_dir"
72
73  finalize
74}
75
76# Parse command-line arguments
77parse_arguments() {
78  while [[ "$#" -gt 0 ]]; do
79    case "$1" in
80      -h|--help) usage; exit 0 ;;
81      *) echo "Error: Unknown option '$1'"; usage; exit 1 ;;
82    esac
83    shift
84  done
85}
86
87# Entry point
88main() {
89  parse_arguments "$@"
90  check_if_root
91  check_dependencies
92  install_zshrc
93}
94
95# Run the script
96main "$@"
97