summaryrefslogtreecommitdiffstats
path: root/punani/bin/punani
blob: 1dd72542a5fe8fcbf55e9f1b35b6a3a6069328f7 (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#! /bin/sh
#
# punani - filesystem scienteer
#
# Engineering Operations
# -E -i spec    insert a package to the target filesystem
# -E -r spec    remove a package
#
set -euf
godmode() {
  if test "${nosudo-false}" != true -a `id -u` != 0; then
    echo "!! we require god mode..." >&2
    exec sudo "$0" "$@"
    exit 23 # go to hell
  fi
}

# return the 'system' variable
# currently be:
# arch-like
# debian-like
guess_system()
{
  if [ -f "/etc/arch-release" ] ;then
    system="${system+$system, }arch-like"
  fi
  if [ -f "/etc/lsb-release" -o -f "/etc/debian_version" ] ;then
    system="${system+$system, }debian-like"
  fi

}
arch_aur_helper()
{
  # pacman is the last fallback helper
  manager="yaourt clyde packer bauerbill tupac pacaur paktahn pbfetch aurget aurora cower powaur pacman"
  for i in $manager;do
    mgr=`which $i`
    if [ "$mgr" ] ;then
      echo $mgr
      return 0
    fi
  done
  echo "!! no helper found, this should never happen(tm)."
  return 1
}
handle_system () {
  case "$system" in
    (*arch-like*)
      # dryrun
      # TODO dryrun not dry enough for aur helper
      if [ "${dryrun-}" ];then
        pacman () { echo  "pacman $@" ; }
        pkgfile () { echo "pkgfile $@"; }
        yaourt () { echo  "yaourt $@" ; }
      fi

      # get dependencies :
      # we need pkgfile 
      if ! [ `which pkgfile` ] ; then
        pacman -S --noconfirm pkgtools 
        pkgfile -u
      fi
      punani_Scientist_update() {
        pacman -Sy
        pkgtool -u
      }
      punani_Scientist_search() {
        pkgfile $1
        if [ "${hard-}" ] ; then
          mgr=`arch_aur_helper`
          $mgr -Ss $1 
        fi

      }

      punani_Engineer_insert() {
        # punani under archlinux first tries to load the packages with the given file name
        # it needs pkgfile for that
        #
        # when trying harder it tries to load the package with the given name directly via yaourt
        echo "** trying to find package with given file"
        if pacman -S `pkgfile $1` 2>/dev/null; then
          echo "++ finished"
          exit 0
        else
          echo "!! nothing found in base repos"
          if [ "${hard-}" ] ; then
            echo "** trying harder"
            echo "** trying yaourt directly with given package"
            mgr=`arch_aur_helper`
            if  $mgr -S $1 ;then
              echo "++ finished"
              exit 0
            else
              echo "!! giving up...i am sorry"
              exit 1
            fi
            echo
          else
            echo "?? When in doubt try $0 -h -Ei $1 "
          fi
        fi
      }
      punani_Engineer_remove() {
        pacman -Rcs "`pacman -Ql | grep $1$ | awk '{print $1}'`" 
        if [ "${hard-}" ] ; then
          echo "** trying harder"
          echo "** directly delete given package name"
          pacman -Rcs "$1"
        fi
      }
    ;;
    (*debian-like*)
      if [ "${dryrun-}" ]; then
        apt-file () { echo $@; }
        apt-get () { echo $@; }
      fi
      [ `which apt-file` ] || apt-get install --yes apt-file && apt-file update
      punani_Scientist_update() {
        apt-get update
        apt-file update
      }
      punani_Scientist_search() {
        apt-file search $1 && exit 0
        if [ "${hard-}" ] ; then
          apt-cache search $1
        fi
      }
      punani_Engineer_insert() {
        if apt-get install `apt-file search $1`;then
          echo "++ finished"
        else
          if [ "${hard-}" ] ; then
            echo "** trying harder"
            apt-get install $1
          fi
        fi
      }
      punani_Engineer_remove() {
        apt-get remove --purge "`apt-file search $1`" 
        if [ "${hard-}" ] ; then
          echo "** trying harder"
          echo "** directly delete given package name"
          apt-get remove --purge "$1"
        fi
      }
    ;;
    (*)
      email='krebs@syntax-fehler.de'
      irc_host='irc.freenode.org'
      irc_channel='#tincspasm'
      cat>&2<<EOF
Error 2: Your System Will Be Supported ASAP
1. send us a bug report
1.1 your operating system's name and version
1.2 this message: $0 $*
1.3 mailto:$email
2. join the relevant IRC channel
2.1 /connect $irc_host
2.2 /join $irc_channel
EOF
      exit 23
  esac
}
help(){
  cat <<EOF
Usage: $0 [Options] [role][command]

Options:
  -f  force
  -h  hard
  -v  verbose
  -d  dryrun
  -?  this message

Role:
  -E  Engineer
  -S  Scientist

Engineer:
  i  insert
  r  remove

Scientist:
  s  search
  y  update
EOF
  exit 1
}
punani (){
  ns=punani
  role=undefined
  while getopts 'dfhvSsopEir' OPT; do
    case $OPT in
      (f) force=true; continue;;
      (h) hard=true; continue;;
      (v) verbose=true; continue;;
      (d) dryrun=true; continue;; 
      (\?) help;continue ;;
    esac
    case ${role-Manager} in
      (Engineer)
        case $OPT in
          (i) command="${ns}_${role}_insert";;
          (r) command="${ns}_${role}_remove";;
          (*)
            echo 'Error 1: You are made of stupid!' >&2
            exit 23;;
        esac;;
      (Scientist)
        case $OPT in
          (s) command="${ns}_${role}_search";;
          (y) command="${ns}_${role}_update";;
          (*)
            echo 'Error 1: You are made of stupid!' >&2
            exit 23;;
        esac ;;
      (undefined)
        case $OPT in
          (E) role=Engineer;;
          (S) role=Scientist;;
          (*)
            exit 23;;
        esac
      ;;
      (*)
        echo 'Error 1: You are made of stupid!' >&2
        exit 23
      ;;
    esac
  done
}
punani $@

case $role in
  (Engineer) godmode $@;;
  (Scientist) 
    case $command in
      (*_update) godmode $@;;
    esac;;
esac

shift `echo $OPTIND-1 | bc`
guess_system


handle_system
for name; do
  "$command" "$name"
done