~oitofelix/dragora/dragora

« back to all changes in this revision

Viewing changes to source/base/pkgsystem/commands/add.old

  • Committer: Bruno Félix Rezende Ribeiro
  • Date: 2013-02-16 18:37:58 UTC
  • Revision ID: oitofelix@gmail.com-20130216183758-2m1tjhqi5vz87q9j
Various implementation progresses

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash
 
2
#  Copyright (C) 2007-2011  Matias A. Fonzo, Santiago del Estero, AR
 
3
#
 
4
#  This program is free software: you can redistribute it and/or modify
 
5
#  it under the terms of the GNU General Public License as published by
 
6
#  the Free Software Foundation, either version 3 of the License, or
 
7
#  (at your option) any later version.
 
8
#
 
9
#  This program is distributed in the hope that it will be useful,
 
10
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
#  GNU General Public License for more details.
 
13
#
 
14
#  You should have received a copy of the GNU General Public License
 
15
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
 
 
17
# Localizaci�n % Idioma:
 
18
TEXTDOMAINDIR=/usr/share/locale
 
19
TEXTDOMAIN=add
 
20
 
 
21
VERSION=3.5
 
22
 
 
23
# Funciones #
 
24
 
 
25
# Una funci�n para mostrar mensajes normales:
 
26
msg() { local LC_ALL ; printf '%s\n' "$@"; }
 
27
 
 
28
# Una funci�n para mensajes de advertencia:
 
29
warn() { local LC_ALL; printf '%b\n' "$@" >&2; }
 
30
 
 
31
usage() {
 
32
  msg $"Installs one or more packages in your system."                     \
 
33
       ""                                                                  \
 
34
      $"Usage: add [options] package_name.tlz ..."                         \
 
35
       ""                                                                  \
 
36
      $"Options:"                                                          \
 
37
      $"  -h, --help                Show this help and exit."              \
 
38
      $"  -v, --version             Show the version of the program."      \
 
39
      $"  -f, --force               Overwrite installed package."          \
 
40
      $"  -w, --warn                Warn about of the files and"           \
 
41
      $"                            directories that will be"              \
 
42
      $"                            installed or overwritten."             \
 
43
       ""
 
44
}
 
45
 
 
46
version() {
 
47
  msg "add $VERSION"                                                       \
 
48
      "Copyright (C) 2007-2011 Matias A. Fonzo <selk@dragora.org>."        \
 
49
      "License GPLv3+: GNU GPL version 3 or later:"                        \
 
50
      "<http://gnu.org/licenses/gpl.html>"                                 \
 
51
      "This is free software: you are free to change and redistribute it." \
 
52
      "There is NO WARRANTY, to the extent permitted by law."
 
53
}
 
54
 
 
55
# Opciones:
 
56
while (( $# )); do
 
57
  case "$1" in
 
58
    -f|--force)
 
59
      OPT=FORCE
 
60
      shift
 
61
      ;;
 
62
    -w|--warn)
 
63
      OPT=WARN
 
64
      shift
 
65
      ;;
 
66
    -[h?]|--help)
 
67
      usage
 
68
      exit 0
 
69
      ;;
 
70
    -[vV]|--version)
 
71
      version
 
72
      exit 0
 
73
      ;;
 
74
    -*)
 
75
      warn $"${0##*/}: Invalid option: $1"
 
76
      exit 1
 
77
      ;;
 
78
    *)
 
79
      break;
 
80
  esac
 
81
done
 
82
 
 
83
# Si no hay argumentos, llama a la funci�n de ayuda:
 
84
(( $# == 0 )) && { usage ; exit 0; }
 
85
 
 
86
# Sale ante cualquier error:
 
87
set -e
 
88
 
 
89
# Advierte acerca de los directorios y archivos del
 
90
# paquete que ser�n instalados o sobreescritos:
 
91
if [[ $OPT = WARN ]]; then
 
92
  while [[ -f $1 ]]; do
 
93
    msg "" $"% Scanning $1 ..." ""
 
94
    LC_ALL=C tar --use-compress-program=lzip -tvvf "$1" | awk '!/^drwx/'
 
95
    msg ""
 
96
    shift
 
97
  done
 
98
  exit 0;
 
99
fi
 
100
 
 
101
# Comprobamos la variable de entorno ROOT:
 
102
if [[ -n $ROOT && ! -d $ROOT ]]; then
 
103
  warn $"${0##*/}: ROOT=${ROOT}: Invalid directory"
 
104
  exit 1;
 
105
fi
 
106
 
 
107
umask 022
 
108
 
 
109
# Chequeo de sanidad.
 
110
#
 
111
# Comprueba si el sistema de archivos
 
112
# se encuentra en modo de s�lo-lectura:
 
113
if ! touch ${ROOT}/pkg-add$$ ; then
 
114
  warn $"The filesystem (on $ROOT) is in read-only mode."
 
115
  exit 1;
 
116
fi
 
117
rm -f ${ROOT}/pkg-add$$
 
118
 
 
119
# Base de datos por defecto:
 
120
DB="${ROOT}/var/db"
 
121
 
 
122
# Nos aseguramos de que las partes de la base de datos exista:
 
123
for directory in \
 
124
 pkg/pre-post \
 
125
 pkg/post-install \
 
126
 pkg/description
 
127
do
 
128
  if [[ ! -d ${DB}/$directory ]]; then
 
129
    mkdir -p "${DB}/$directory"
 
130
  fi
 
131
done
 
132
 
 
133
# M�s funciones #
 
134
 
 
135
# Una funci�n para reflejar el nombre base:
 
136
_basename() { local name ; name=${1##*/} ; printf "${name%$2}"; }
 
137
 
 
138
# Una funci�n que ejecuta los scripts de pre y post instalaci�n:
 
139
exe_install() {
 
140
  local fileName
 
141
  fileName="$1"
 
142
  if [[ -r ${ROOT}/install/$fileName ]]; then
 
143
    msg $"| $fileName execution for $pkgname ..."
 
144
    ( cd $ROOT/
 
145
      sh install/$fileName
 
146
      install -m 0755 install/$fileName \
 
147
       -D ${DB}/pkg/${fileName}/$pkgname && \
 
148
      rm -f install/$fileName
 
149
    )
 
150
  fi
 
151
}
 
152
 
 
153
# Guardamos y exportamos la variable de entorno LC_ALL
 
154
# para que algunos comandos corran m�s r�pido:
 
155
DLANG=$LANG
 
156
export LC_ALL=C
 
157
 
 
158
# Loop:
 
159
for package in "$@" ; do
 
160
  if [[ -f $package ]]; then
 
161
    # Comprueba la extensi�n del paquete:
 
162
    case "$package" in
 
163
      *.tlz)
 
164
        pkgname=$(_basename $package .tlz)
 
165
        ;;
 
166
      *)
 
167
        warn "${package}: Does not end in .tlz"
 
168
        CODE=1
 
169
        continue;
 
170
    esac
 
171
  else
 
172
    warn $"${0##*/}: ${package}: File not found or non-regular"
 
173
    CODE=1
 
174
    continue;
 
175
  fi
 
176
 
 
177
  # Si el paquete no est� en la base de datos, lo instalamos:
 
178
  if [[ ! -r ${DB}/pkg/$pkgname || $OPT = FORCE ]]; then
 
179
    msg "" $"> Installing $pkgname ..." ""
 
180
 
 
181
    # Chequea la integridad del paquete:
 
182
    msg $"% Testing integrity ..." ""
 
183
    tar --use-compress-program=lzip -tf $package >/dev/null
 
184
 
 
185
    # Descomprime el tarball:
 
186
    ( cd $ROOT/
 
187
      tar \
 
188
       --use-compress-program=lzip \
 
189
       --strip-components=1 --show-stored-names \
 
190
       -xvphlf -
 
191
    ) < $package > ${DB}/pkg/$pkgname
 
192
    CODE=$?
 
193
    if (( $CODE != 0 )); then
 
194
      rm -f ${DB}/pkg/$pkgname
 
195
      break;
 
196
    fi
 
197
  else
 
198
    warn $"${DB}/pkg/${pkgname}: Already installed"
 
199
    CODE=3
 
200
    continue;
 
201
  fi
 
202
 
 
203
  # Muestra la descripci�n en el lenguaje disponible, de acuerdo a la
 
204
  # "locale" usada por el sistema, y a la variable de entorno $LANG:
 
205
  case "$DLANG" in
 
206
    *.UTF-8*|*.utf8)
 
207
      DLANG=${DLANG%_*}
 
208
      UTF8_OUTPUT=y
 
209
      ;;
 
210
    ??_??|*.ISO8859-1|*.iso88591|*.ISO-8859-1)
 
211
      DLANG=${DLANG%_*}
 
212
      ;;
 
213
    *)
 
214
      DLANG=en;
 
215
  esac
 
216
  # Imprime el archivo de descripci�n:
 
217
  if [[ -f ${ROOT}/description/${DLANG} ]]; then
 
218
    ( cd ${ROOT}/description
 
219
      if [[ $UTF8_OUTPUT ]]; then
 
220
        fmt <(iconv --from-code=ISO8859-1 --to-code=UTF-8 $DLANG)
 
221
      else
 
222
        fmt $DLANG
 
223
      fi
 
224
      # Movemos los archivos a la base de datos:
 
225
      for file in ?? ; do
 
226
        mv "$file" "${DB}/pkg/description/${pkgname}_${file}"
 
227
      done
 
228
    )
 
229
  fi
 
230
 
 
231
  # Ejecuta el script de pre-post instalaci�n:
 
232
  exe_install "pre-post"
 
233
 
 
234
  # Ejecuta el script de post-instalaci�n:
 
235
  exe_install "post-install"
 
236
 
 
237
  # Remueve los directorios de descripci�n e instalaci�n:
 
238
  rmdir ${ROOT}/{description,install} >/dev/null 2>&1 || :
 
239
done
 
240
 
 
241
exit $CODE
 
242