aboutsummaryrefslogtreecommitdiff
path: root/sbodl
blob: 2db3409247e2aa9c43f96dbbb5a76f54a7f1ebb8 (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
#!/bin/bash

# sbodl, download slackbuilds.org source files

# 20230513 bkw: move to the sbo-maintainer-tools repo, clean up for release.
# 20191222 bkw: handle broken symlinks in current dir.
#               change default cache dir.
# 20170306 bkw: add caching

VERSION=0.8.0

SELF=$( basename $0 )

DEFCACHEDIR=~/sbodl-cache
CACHEDIR=${SBODL_CACHEDIR:-$DEFCACHEDIR}

ARCHIVE=https://slackware.uk/sbosrcarch

RED="\033[1;31m"
GREEN="\033[1;32m"
COLOR_OFF="\033[0m"


usage() {
	cat <<EOF
$SELF v$VERSION - Download the sources for a SlackBuilds.org build.
(c) 2014, 2023 B. Watson (urchlay@slackware.uk)
Licensed under the WTFPL: Do WTF you want with this.

Usage: $SELF [-f] <wget-options>

Execute $SELF in the directory that contains the .info and .SlackBuild
files. If the files are not found in the current directory, they'll be
looked for in the cache directory, and symlinked to the current dir if
found there. If they're not in the cache, they'll be downloaded with wget.

Each file's md5sum is checked against the .info file in the current directory.

Options:

-h, --help: Show this help message.

-f: Ignore locally cached files, always download.

-a: Download from sbosrcarch (Source Archive).

Any other options you pass to it will be passed to wget.

By default, --content-disposition is used. If you want to disable it, use
the --no-content-disposition wget option.

The default cache directory is $DEFCACHEDIR,
but you can override this by setting SBODL_CACHEDIR in the environment.

If you need to download files for a different architecture
(e.g. 32-bit files when running on a 64-bit OS), you can override ARCH
in the environment: "ARCH=i686 sbodl" would download the 32-bit source
file(s).

EOF
	exit 0
}

die() {
	echo "$SELF: $*" 2>&1
	exit 1
}

[ -d "$CACHEDIR/" ] || mkdir -p $CACHEDIR || die "Can't create $CACHEDIR"

# check for our one argument
case "$1" in
	-h|-help|-\?|--help) usage ;;
	-f) FORCEDL="yes" ; shift ;;
	-a) USEARCHIVE="yes" ; shift ;;
esac

source ./$( basename $( pwd ) ).info \
	|| die "No .info file, are you sure this is a SBo directory? Try '$SELF --help'"

# This stanza copied from the SBo template for 14.1:
if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ "$ARCH" = "x86_64" ]; then
	DL=${DOWNLOAD_x86_64:-$DOWNLOAD}
	SUM=${MD5SUM_x86_64:-$MD5SUM}
else
	DL=$DOWNLOAD
	SUM=${MD5SUM}
fi

if [ -z "$DL" ]; then
	die "Bad .info file (no DOWNLOAD= or DOWNLOAD_x86_64=)."
fi

# save passed-in command line args for use with wget
WGETARGS="$@"

set $SUM

for dl in $DL; do
	if [ "$USEARCHIVE" = "yes" ]; then
		file="$( echo "$dl" | sed 's,.*/,,' )"
		a=$( echo $1 | cut -b1 )
		b=$( echo $1 | cut -b2 )
		dl="$ARCHIVE/by-md5/$a/$b/$1/$file"
	fi

	EXTRAWGETARGS="--content-disposition "
	case "$dl" in
		*sourceforge.net/*|*.sf.net/*) EXTRAWGETARGS="--user-agent wget" ;;
		*) ;;
	esac

	FILE=$( echo "$dl" | sed 's,.*/,,' )

	# ignore (rm) broken symlinks
	[ -L "$FILE" ] && ! [ -e "$FILE" ] && FORCEDL=yes

	[ "$FORCEDL" = "yes" ] && rm -f "$FILE"

	if [ -f "$FILE" -a ! -L "$FILE" ]; then
		# file exists and is a regular file, cache it
		mv -b "$FILE" "$CACHEDIR"
		ln -s "$CACHEDIR/$FILE" "$FILE"
	elif [ -e "$FILE" ]; then
		# don't do anything
		:
	elif [ "$FORCEDL" != "yes" ] && [ -e "$CACHEDIR/$FILE" ]; then
		ln -s "$CACHEDIR/$FILE" "$FILE"
	else
		wget $EXTRAWGETARGS $WGETARGS "$dl" || die "Download failed, try '$SELF -a'."
		if [ -e "$FILE" ]; then
			mv -b "$FILE" "$CACHEDIR"
			ln -s "$CACHEDIR/$FILE" "$FILE"
		fi
	fi

	if [ -e "$FILE" ]; then
		GOTSUM="$( md5sum "$FILE" | cut -d' ' -f1 )"
		if [ "$1" != "$GOTSUM" ]; then
			echo -e "${RED}WARN: md5sum doesn't match${COLOR_OFF}\\n     expected: $1\\n           got: $GOTSUM"
		else
			echo -e "${GREEN}md5sum matches OK${COLOR_OFF}: $1"
		fi
	else
		echo -e "${RED}WARN${COLOR_OFF}: can't find downloaded file $FILE, try '$SELF -a'?"
	fi
	shift
done