summaryrefslogtreecommitdiffstats
path: root/ship/lib/punani
blob: af267ac8b726f74713c8c923b4590857091d6a16 (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
#@include core
#@include _punani_db

## usage: _punani_resolve_package PKGNAME
_punani_resolve_package(){
  eval "echo \"\${_punanidb_${PACKER}_$1}\""
}

KNOWN_PACKERS='pacman apt-get yum brew'
_punani_pacman_install(){ pacman --noconfirm -S --needed "$@" ;}
_punani_pacman_remove(){ pacman -Rcs "$@" ;}
_punani_pacman_has(){ pacman -Q "$1" >/dev/null;}
_punani_aptget_install(){ apt-get -y install "$@" ;}
_punani_aptget_remove(){ apt-get -y remove "$@" ;}
_punani_aptget_has() { dpkg -s "$1" | grep -q "Status: install";}
_punani_yum_install(){ yum -y install "$@" ;}
_punani_yum_remove(){ yum -y remove "$@" ;}
_punani_yum_has() { rpm -qa --qf "%{NAME}\n"| egrep  "^${1}\$" >/dev/null ;}
_punani_brew_install(){ brew install "$@"; }
_punani_brew_remove(){ brew remove "$@";}
_punani_brew_has(){ error "not implemented"; return 1 ;}

_punani_usage='punani {install,remove,has} PACKAGE...'
punani(){
  for p in ${KNOWN_PACKERS:-null}; do
    exists $p && info "using $p" && PACKER=`echo $p | tr -d -` && break
  done || die 'Error 2: no package manager found; no punani for you!'

  ACTION="$1"; shift

  if test $# = 0; then
    error 'no PACKAGE specified.'
    die "usage: $_punani_usage"
  fi

  for PKG; do
    RES="`_punani_resolve_package $PKG`"
    test  -z "$RES" && error "could not resolve '$PKG'; no punani for you!"&& return 23
    case "$ACTION" in 
      install)
          eval _punani_${PACKER}_has $RES && info "$RES already installed, skipping" && continue
          ! is_root && error "punani requires super-user rights for installing" && return 1
          eval _punani_${PACKER}_install $RES || error "cannot install $RES with $PACKER"
          ;;
      remove)
            ! eval  _punani_${PACKER}_has $RES && info "$RES not installed, skipping" && continue
            ! is_root && error "punani requires super-user rights for removing" && return 1
            eval _punani_${PACKER}_remove $RES || error "cannot install $RES with $PACKER"
            ;;
      has)
            if eval  _punani_${PACKER}_has $RES ;then
              info "$RES is installed"
            else
              info "$RES is not installed"
            fi
            ;;
      *)
        error "bad action: $ACTION"
        die "usage: $_punani_usage"
    esac
  done
}