Created
May 7, 2022 07:36
-
-
Save cyberfined/6d0594ffb7a9ad9cf6856fb7b21f9a6c to your computer and use it in GitHub Desktop.
Download latest version of stack and create arch linux package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
installDir=/usr/bin | |
srcDir=src | |
pkgDir=pkg | |
function prepareEnv { | |
mkdir -p "$srcDir" | |
} | |
function cleanEnv { | |
rm -rf "$srcDir" | |
rm -rf "$pkgDir" | |
rm "PKGBUILD" | |
} | |
function getUrl { | |
local findHeader="location: " | |
local returl=https://get.haskellstack.org/stable/linux-x86_64.tar.gz | |
local realurl="$(curl -I ${returl} 2>/dev/null | grep --color=never ${findHeader})" | |
local realurl="${realurl:${#findHeader}}" | |
echo -n "$realurl" | |
} | |
function getVersion { | |
local url="$1" | |
local verre="\/download\/v([0-9.]*)\/" | |
[[ $url =~ $verre ]] | |
local version="${BASH_REMATCH[1]}" | |
echo -n "$version" | |
} | |
function checkVersion { | |
local newVer="$1" | |
local curVer="$(stack --version)" | |
local verre="Version ([0-9.]*)" | |
[[ $curVer =~ $verre ]] | |
curVer="${BASH_REMATCH[1]}" | |
local newToks=(${newVer//./ }) | |
local curToks=(${curVer//./ }) | |
if [[ "${#newToks[@]}" -le "${#curToks[@]}" ]]; then | |
local numToks="${#newToks[@]}" | |
else | |
local numToks="${#curToks[@]}" | |
fi | |
for ((i=0; i < "$numToks"; i++)); do | |
if [[ "${newToks[$i]}" -gt "${curToks[$i]}" ]]; then | |
local retval=1 | |
break | |
elif [[ "${newToks[$i]}" -lt "${curToks[$i]}" ]]; then | |
local retval=0 | |
break | |
fi | |
done | |
return $retval | |
} | |
function generatePkgBuild { | |
local ver="$1" | |
pkgbuild="pkgname=stack\npkgver=${ver}\npkgrel=1\npkgdesc=\"haskell tool stack\"\narch=('x86_64')\nlicense=('GPL')\ndepends=('pacman>=5.1')\n\npackage() {\n cd \"\$srcdir\"\n mkdir -p \"\$pkgdir/${installDir}\"\n cp stack \"\$pkgdir/${installDir}\"\n}\n" | |
echo -ne "$pkgbuild" > PKGBUILD | |
} | |
function createPackage { | |
makepkg | |
} | |
function updateStack { | |
local url="$1" | |
local ver="$2" | |
local urlToks=(${url//\// }) | |
local numToks="${#urlToks[@]}" | |
local fname="${urlToks[$numToks-1]}" | |
local dirname="${fname:0:-7}" | |
curl -L "$url" > "$fname" 2>/dev/null | |
tar -xzf "$fname" | |
rm "$fname" | |
mv "$dirname/stack" "$srcDir" | |
rm -rf "$dirname" | |
generatePkgBuild "$ver" | |
createPackage | |
cleanEnv | |
} | |
# Retrieve URL and Version | |
url="$(getUrl)" | |
ver="$(getVersion $url)" | |
# Delete line terminator | |
url="${url:0:-1}" | |
shouldUpdate=0 | |
if [[ "$(command -v stack)" ]]; then | |
checkVersion "$ver" | |
shouldUpdate="$?" | |
else | |
shouldUpdate=1 | |
fi | |
if [[ "$shouldUpdate" -eq 1 ]]; then | |
prepareEnv | |
updateStack "$url" "$ver" | |
else | |
echo "You have a current version of stack" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment