Skip to content

Instantly share code, notes, and snippets.

@amn
Last active August 4, 2016 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amn/7891fd333d5085e6f0a395db2aae6816 to your computer and use it in GitHub Desktop.
Save amn/7891fd333d5085e6f0a395db2aae6816 to your computer and use it in GitHub Desktop.
Download fresh copy of Cygwin installer if current copy is stale, and invoke it without administration rights (user-local install)
#!/bin/sh -ue
# Usage: /path/to/cygadmin.sh [preferred/path/to/setup-x86_64.exe]
# If first argument is omitted, this script behaves as if the value was ".", i.e. current working directory.
# If setup file exists, its extended attributes are checked to see if a property called 'etag' is attached. If it is, its value is used in HTTP request to Cygwin website to download the installer. The website will reply with "not modified, use your own copy" if etag values match, and in this case, the existing setup file will be used. This is in effect local caching of Web resources, the same thing your browser does, but through command line. The setup file is invoked with '--no-admin' option, because my Windows install is managed by crazy paranoid system administrators of our organization where even professionals are not trusted with Administrator role.
# When a fresh installer file is downloaded, an extended attribute with the name 'etag' is attached to it, value being as present in HTTP response. All this so that the cache validation can be done later.
# This script depends on you having getfattr, setfattr, curl, mktemp, cp, stat, sed, in your execution path.
# Written by http://github.com/amn
: ${TMPDIR=$TMP}
setup_file_name=setup-x86_64.exe
setup_file_path="${1-.}/$setup_file_name"
etag=
if [ -f "$setup_file_path" ]
then
etag=$(getfattr --name user.etag --only-values "$setup_file_path" 2>/dev/null) || :
if [ ! -z "$etag" ]
then
alias curl="curl --header 'If-None-Match: \"$etag\"'"
fi
fi
temp_file_path=$(mktemp "$TMPDIR/$setup_file_name-XXXX")
temp_file_header_file_path="$temp_file_path.headers"
curl -s -f -D $temp_file_header_file_path "https://www.cygwin.com/$setup_file_name" > $temp_file_path
if [ $(stat -c %s "$temp_file_path") -ne 0 ]
then
etag=$(sed -n 's/^ETag:\s*\(.*\)$/\1/p' "$temp_file_header_file_path")
setfattr -n user.etag -v $etag "$temp_file_path"
cp --preserve=xattr "$temp_file_path" "$setup_file_path"
chmod +x $setup_file_path
fi
"$setup_file_path" --no-admin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment