aboutsummaryrefslogtreecommitdiff
path: root/stowpkg
blob: 545547625ad3fc204694f64719e4d05201531a34 (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
#!/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
VERSION="0.0.1"
DIRNAME="$(cd `dirname $0` && pwd)"
BASENAME="`basename $0`"
ACTION="$1"

__stowpkg_initialize () {
  # Check for stow
  if ! which stow > /dev/null 2>&1; then
    echo "$BASENAME: please install stow 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 BASE="$BINARIES"
}

stowpkg_usage () {
  echo "$BASENAME $VERSION - a package manager"
  echo ""
  echo "usage: $BASENAME <action> [options]"
  echo ""
  echo "available actions:"
  echo ""
  grep "^stowpkg_" $0 | 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
    ( 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/`$0 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

  # 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
      $0 install $dependency
    done
  fi

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

  # Go to sources folder
  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
  cd ${package}-${source_version}

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

  # Build
  stowpkg_build $package $version
}

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

  cd $BINARIES

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

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