aboutsummaryrefslogtreecommitdiff
path: root/stowpkg
blob: 76cb576d971ca6bf4a01ae3c822271b079068511 (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#!/usr/bin/env sh
#
# stowpkg package manager
#
# Copyright (C) 2017 Silvio Rhatto - rhatto at riseup.net
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License,
# or any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Parameters
PROGRAM="$0"
VERSION="0.0.1"
DIRNAME="$(cd `dirname $PROGRAM` && pwd)"
BASENAME="`basename $PROGRAM`"
ACTION="$1"
CWD="`pwd`"

__stowpkg_cd () {
  if [ -z "$1" ]; then
    cd $CWD > /dev/null 2>&1
  else
    if [ -d "$1" ]; then
      cd $1 > /dev/null 2>&1
    fi
  fi
}

__stowpkg_initialize () {
  # Check for stow
  if ! which stow > /dev/null 2>&1; then
    echo "$BASENAME: please install stow first"
    exit 1
  fi

  # Check for make
  if ! which make > /dev/null 2>&1; then
    echo "$BASENAME: please install make first"
    exit 1
  fi

  # Sudo config
  if [ "`whoami`" != 'root' ]; then
    SUDO="sudo"
  fi

  # Source config file
  if [ -e "$HOME/.config/stowpkg" ]; then
    . "$HOME/.config/stowpkg"
  fi

  # Ports
  if [ -z "$PORTS" ]; then
    PORTS="$DIRNAME/ports"
  fi

  # Set base
  if [ -z "$BASE" ]; then
    BASE="$DIRNAME/tree/`uname -m`"
  fi

  # Sources
  if [ -z "$SOURCES" ]; then
    SOURCES="$BASE/src"
  fi

  # Binaries
  if [ -z "$BINARIES" ]; then
    BINARIES="$BASE/stow"
  fi

  # Ensure folders exists
  mkdir -p $BASE $PORTS $SOURCES $BINARIES

  # Export
  export BASE="$BASE"
  export PORTS="$PORTS"
  export SOURCES="$SOURCES"
  export BINARIES="$BINARIES"
}

stowpkg_usage () {
  echo "$BASENAME $VERSION - a package manager"
  echo ""
  echo "usage: $BASENAME <action> [options]"
  echo ""
  echo "available actions:"
  echo ""
  grep "^stowpkg_" $PROGRAM | cut -d ' ' -f 1 | sed -e 's/stowpkg_//' | sort | xargs -L 6 | column -t -c 6 | sed -e 's/^/\t/'
  echo ""

  exit 1
}

stowpkg_search () {
  local package="$1"

  if [ ! -z "$package" ]; then
    stowpkg_search | grep "$package$"
  else
    ( __stowpkg_cd $PORTS && find -name 'desc' | sed -e 's|^\./||' -e 's|/desc||' )
  fi
}

stowpkg_install () {
  local package="$1"

  if [ -z "$package" ]; then
    stowpkg_usage
  fi

  # Find package on ports
  local location="$PORTS/`$PROGRAM search $package`"

  if [ "$location" = "${PORTS}/" ]; then
    echo "$BASENAME: package not found: $package"
    exit 1
  fi

  if [ "`basename $location`" != "$package" ]; then
    echo "$BASENAME: package not found: $package"
    exit 1
  fi

  local installed="`stowpkg_list $package-`"

  if [ ! -z "$installed" ]; then
    echo "$BASENAME: package $package already installed"
    echo "to reinstall, use '$BASENAME reinstall $package'"
    exit 1
  fi

  # Source environment
  if [ -e "$location/env" ]; then
    . $location/env
  fi

  # Set default version
  if [ -z "$version" ]; then
    version="latest"
  fi

  if [ "$source_format" = "git" ]; then
    source_version="git"
  else
    source_version="$version"
  fi

  # Resolve default dependencies
  if [ ! -z "$dependencies_stowpkg" ]; then
    for dependency in $dependencies_stowpkg; do
      $PROGRAM install $dependency
    done
  fi

  # Resolve debian dependencies
  if [ -e "/etc/debian_version" ]; then
    if [ ! -z "$dependencies_debian" ]; then
      $SUDO apt install -y $dependencies_debian
    fi
  fi

  # Go to sources folder
  __stowpkg_cd $SOURCES

  # Download sources if needed
  if [ ! -d "${package}-${source_version}" ]; then
    if [ "$source_format" = "git" ]; then
      git clone $url ${package}-${source_version}
    fi
  fi

  # Check source
  if [ ! -d "${package}-${source_version}" ]; then
    echo "$BASENAME: source not found for package $package"
    exit 1
  fi

  # Go to source folder
  __stowpkg_cd ${package}-${source_version}

  if [ "$source_format" = "git" ]; then
    git fetch --all
    git checkout $version
  fi

  # Build
  stowpkg_build $package $version
}

stowpkg_reinstall () {
  local package="$1"

  if [ -z "$package" ]; then
    echo "usage: $BASENAME reinstall <package>"
    exit 1
  fi

  stowpkg_remove $package && stowpkg_install $package
}

stowpkg_build () {
  local package="$1"
  local version="$2"

  # Check
  if [ -z "$version" ]; then
    echo "$BASENAME: please specify package name and version"
    exit 1
  fi

  # Set PREFIX
  if [ -z "$PREFIX" ]; then
    PREFIX="$BINARIES/${package}-${version}"
  fi

  # Build
  if [ -e "$location/rules" ]; then
    . $location/rules
  else
    # Try default rules
    if [ -e "Makefile" ]; then
      make clean
    fi

    if [ -e "configure" ]; then
      ./configure --prefix=$PREFIX
    fi

    if [ -e "Makefile" ]; then
      make PREFIX=$PREFIX
      make PREFIX=$PREFIX install
    fi
  fi

  __stowpkg_cd $BINARIES

  local installed="`stowpkg_list | xargs`"

  if [ -d "${package}-${version}" ]; then
    stow ${installed} ${package}-${version} || exit 1
  else
    echo "$BASENAME: error building $package"
    exit 1
  fi

  __stowpkg_cd
}

stowpkg_list () {
  local pattern="$1"

  #if [ ! -z "$pattern" ]; then
  #  local lname="-lname \'$pattern*\'"
  #fi
  if [ -z "$pattern" ]; then
    pattern='*'
  fi

  #find $BASE $lname -type l | grep -v "^src/" | grep -v "^stow/" | while read file; do
  find $BASE -type l | grep -v "^src/" | grep -v "^stow/" | while read file; do
    dest="`stat $file | head -n 1 | cut -d '>' -f 2 | sed -e 's/ //'`"

    if echo $dest | grep -q "^stow/" || echo $dest | grep -q "/stow/"; then
      echo $dest | sed -e 's|.*stow/\(.*\)/.*|\1|'
    fi
  done | grep -E "$pattern" | sort | uniq
}

stowpkg_binaries () {
  ls -1 $BINARIES
}

stowpkg_sources () {
  ls -1 $SOURCES
}

stowpkg_ls () {
  ( cd $BASE && ls $* )
}

stowpkg_location () {
  echo $BASE
}

stowpkg_switch () {
  local package="$1"
  local version="$2"

  if [ -z "$version" ]; then
    echo "usage: $BASENAME switch <package> <version>"
    exit 1
  fi

  if [ ! -d "$BINARIES/${package}-${version}" ]; then
    echo "$BASENAME: binaries not found for ${package}-${version}"
    exit 1
  fi

  local installed="`stowpkg_list $package | sed -e "s/$package-//"`"

  if [ "$version" = "$installed" ]; then
    echo "$BASENAME: package $version already installed"
    exit
  fi

  __stowpkg_cd $BINARIES

  echo "Removing currently installed ${package}-${installed}..."
  stow --delete ${package}-${installed}

  echo "Switching to ${package}-${version}..."
  stow ${package}-${version}

  __stowpkg_cd
}

stowpkg_remove () {
  local package="$1"

  if [ -z "$package" ]; then
    echo "usage: $BASENAME remove <package>"
    exit 1
  fi

  local installed="`stowpkg_list $package-`"

  if [ -z "$installed" ]; then
    echo "$BASENAME: package $package not installed"
    exit 1
  fi

  __stowpkg_cd $BINARIES
  echo "Removing currently installed ${installed}..."
  stow --delete $installed
  __stowpkg_cd
}

stowpkg_purge () {
  local package="$1"

  if [ -z "$package" ]; then
    echo "usage: $BASENAME purge <package>"
    exit 1
  fi

  local installed="`stowpkg_list $package-`"

  if [ ! -z "$installed" ]; then
    stowpkg_remove $package
  fi

  echo "Removing all sources and binaries that matches $package..."
  rm -rf $BINARIES/$package-*
  rm -rf $SOURCES/$package-*
}

stowpkg_upgrade () {
  local package="$1"

  if [ -z "$package" ]; then
    echo "usage: $BASENAME upgrade <package>"
    exit 1
  fi

  local location="$PORTS/`$PROGRAM search $package`"

  if [ "$location" = "${PORTS}/" ]; then
    echo "$BASENAME: package not found: $package"
    exit 1
  fi

  local installed="`stowpkg_list $package-`"

  if [ -z "$installed" ]; then
    echo "$BASENAME: package $package not installed"
    exit 1
  fi

  if [ -e "$location/env" ]; then
    . $location/env
  fi

  local current="`stowpkg_list $package | sed -e "s/$package-//"`"

  if [ "$version" = "$current" ]; then
    echo "$BASENAME: current package $version already installed"
    exit
  fi

  stowpkg_remove $package && stowpkg_install $package
}

stowpkg_update () {
  __stowpkg_cd $DIRNAME && git pull
  __stowpkg_cd
}

# Dispatch
if grep -q "^stowpkg_$ACTION () {$" $PROGRAM; then
  __stowpkg_initialize
  shift
  stowpkg_$ACTION $*
else
  stowpkg_usage
fi