summarylogtreecommitdiffstats
path: root/update_pkgbuild.sh
blob: cd5a6a078e526b42f0371a140cceabb80a8709f8 (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
#!/bin/bash

readonly PROGRAM_NAME=$(basename $0)

usage() {
	cat << EOF
Update PKGBUILD

Usage: ${PROGRAM_NAME} [options]

Options:
	-h, --help 		display this help message and exit.
	-r, --pkgrel 		the package version
	-v, --pkgver 		the package release
EOF
}

main() {
	pkgver=""
	pkgrel=""

	optspec=":hr:v:-:"
	while getopts "$optspec" optchar; do
		case "${optchar}" in
			-)
				case "${OPTARG}" in
					help)
						usage
						exit 2
						;;
					pkgver)
						pkgver="${!OPTIND}";
						OPTIND=$(( $OPTIND + 1 ))
						;;
					pkgrel)
						pkgrel="${!OPTIND}";
						OPTIND=$(( $OPTIND + 1 ))
						;;
					*)
						echo "Unknown option --${OPTARG}" >&2
						exit 1
						;;
				esac;;
			h)
				usage
				exit 2
				;;
			r)
				pkgrel="${OPTARG}"
				;;
			v)
				pkgver="${OPTARG}"
				;;
			*)
				echo "Non-option argument: '-${OPTARG}'" >&2
				exit 1
				;;
		esac
	done

	if [ -z "${pkgver}" ]; then
		echo "You must specify the package version!"
		echo "Exiting..."
		exit 1
	fi
	sed -i -r "s/^pkgver=([0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,3})$/pkgver=${pkgver}/g" PKGBUILD

	if [ -z "${pkgrel}" ]; then
		pkgrel=$(grep "^pkgrel" PKGBUILD | cut -d '=' -f 2)
	fi
	sed -i -r "s/^pkgrel=([0-9])$/pkgrel=${pkgrel}/g" PKGBUILD

	algorithm="sha512"
	checksum_file="Joplin-${pkgver}.AppImage.${algorithm}"
	repo_url="https://github.com/laurent22/joplin/releases/download/v${pkgver}/${checksum_file}"

	status=$(curl --output /dev/null -I --write-out '%{http_code}' "${repo_url}")
	# requests are always redirected to the aws servers
	if [[ "302" != "${status}" ]]; then
		echo "The file ${checksum_file} does not exists!"
		echo "Exiting..."
		exit 1
	fi

	sha512=$(curl --silent -L "${repo_url}")
	sed -i -r "s/^\s\s([A-Fa-f0-9]{128})$/  ${sha512}/g" PKGBUILD
	if [ $? -ne 0 ]; then
		echo "Failed to update sha512!"
		echo "Need manual intervention!"
		exit 1
	fi
}

main $@