aboutsummaryrefslogtreecommitdiffstats
path: root/auto.
blob: d86f855dae369863bbdea4aa2d9ad26258e48db8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/bin/sh

# defaults
topography=.topography

help() {
printf "Usage: auto. [OPTION]\n\
  -t,\tspecify the topography of your dotfiles (default: .topography)\n\
  -i,\tinstall dotfiles specified by -t\n\
  -u,\tuninstall dotfiles specified by -t\n\
  -b,\texit as soon as a command errors\n\
  -h,\tshows help text\n"
exit 0
}

# setup package management util
for file in\
  /etc/os-release\
  /etc/lsb-release\
  /usr/lib/os-release\
  /etc/openwrt_release
do
  [ -f "$file" ] && . "$file" && break
done

#######################################
# do root - runs a command as root
# Arguments:
#   ... command
#######################################
dort() {
  if [ -x /bin/doas ] && [ -f /etc/doas.conf ]; then
    doas "$@"
  elif [ -x /bin/sudo ]; then
    sudo "$@"
  fi
}

case "$ID" in
  void)
    pkg_man=xbps
    pkg_install() {
      [ -z "$*" ] && return 1
      printf "\033[31mRunning: xbps-install -y -- %s\033[m\n" "$*"
      dort xbps-install -y -- $*
    }

    pkg_uninstall() {
      [ -z "$*" ] && return 1
      printf "\033[31mRunning: xbps-remove -Roy -- %s\033[m\n" "$*"
      dort xbps-remove -Roy -- $*
    }

    pkg_search() {
      xbps-query --search= | fzf -f "$1" | head -1 | cut -d ' ' -f 2
    }
    ;;
  arch)
    pkg_man=pacman
    pkg_install() {
      [ -z "$*" ] && return 1
      printf "\033[31mRunning: pacman -s --noconfirm -- %s\033[m\n" "$*"
      dort pacman -S --noconfirm -- $*
    }

    pkg_uninstall() {
      [ -z "$*" ] && return 1
      printf "\033[31mRunning: pacman -Runs --noconfirm -- %s\033[m\n" "$*"
      dort pacman -Runs --noconfirm -- $*
    }

    pkg_search() {
      pacman -Slq | fzf -f "$1" | head -1
    }
    ;;
  pop|debian|ubuntu)
    pkg_man=apt
    pkg_install() {
      [ -z "$*" ] && return 1
      printf "\033[31mRunning: apt-get install -y -- %s\033[m\n" "$*"
      dort apt-get install -y -- $*
    }

    pkg_uninstall() {
      [ -z "$*" ] && return 1
      printf "\033[31mRunning: apt-get purge --autoremove -y -- %s\033[m\n" "$*"
      dort apt-get purge --autoremove -y -- $*
    }

    pkg_search() {
      apt list | fzf -f "$1" | head -1 | cut -d '/' -f 1
    }
    ;;
esac

# parse command line options
while getopts :biuht: c; do
  case $c in
    t)
      topography="${OPTARG}"

      [ ! -f "$topography" ] &&\
        printf "invalid topography file\n" &&\
        exit 1
      ;;
    # only install if not already uninstalling
    i) [ -z $uninstall ] && install=1 ;;
    # only uninstall if not already installing
    u) [ -z $install ] && uninstall=1 ;;
    b)
      set -e
      bailout() {
        [ $? -eq 0 ] && return # don't run when the script ends successfully
        printf "\033[31mlast command failed, bailing out!\033[m\n"
      }

      trap bailout EXIT
    ;;
    h|*) help ;;
  esac
done

[ ! -f "$topography" ] &&\
  printf "no '.topography' file found\n" &&\
  exit 1

[ -z $install ] && [ -z $uninstall ] &&\
  printf "no action specified\n" &&\
  help

# parse topography file line by line
while read -r line || return 0; do
  # skip lines starting with a # (comments)
  if [ "$(printf "%s" "$line" | head -c1)" = "#" ]; then
    continue

  # run a command
  elif [ "$(printf "%s" "$line" | head -c2)" = '$$' ]; then
    eval "${line##*\$\$}"

  # install/uninstall a package if on a supported distro
  elif [ "$(printf "%s" "$line" | head -c2)" = '::' ] && [ -n "$pkg_man" ]; then
    # get line without prefix
    s=${line##*\:\:}

    # get number of packages specified
    npkgs=$(printf "%s" "$s" | grep -o ',' | wc -l)
    npkgs=$((npkgs + 2))

    i=1
    while [ $i -lt $npkgs ]; do
      pkgname="$(printf "%s" "$s" | cut -d ',' -f $i)"

      pkg=$(pkg_search "$pkgname")

      [ $install ] && ipkgs=${ipkgs:+$ipkgs }$pkg
      [ $uninstall ] && upkgs=${upkgs:+$upkgs }$pkg

      i=$((i + 1))
    done

    pkg_install $ipkgs
    pkg_uninstall $upkgs

  # remove/add a file
  elif printf "%s" "$line" | grep -q -- "->"; then
    # get the path of the file
    sourcef=$(eval echo "${line%%\ ->*}")

    # get the destination for the file
    destf=$(eval echo "${line##*->\ }")

    # install/uninstall
    [ $install ] && cp -rv -- "$sourcef" "$destf"
    [ $uninstall ] && rm -rv -- "$destf"
  fi
done < "$topography"