You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

67 lines
1.8 KiB

  1. #!/usr/bin/env bash
  2. set -eu
  3. status_message() {
  4. printf "\\033[0;32m%s\\033[0m\\n" "$1"
  5. }
  6. error_message() {
  7. printf "\\033[0;31m%s\\033[0m\\n" "$1"
  8. }
  9. SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
  10. CACHE_LOCATION="${HOME}/.cabal/packages/hackage.haskell.org/01-index.cache"
  11. if [ ! -f "${CACHE_LOCATION}" ] ; then
  12. error_message "${CACHE_LOCATION} does not exist, did you run 'cabal update'?"
  13. exit 1
  14. fi
  15. if [ ! -f "${SCRIPTPATH}/cabal.project" ] ; then
  16. error_message "Could not find ${SCRIPTPATH}/cabal.project, skipping index state update."
  17. exit 3
  18. fi
  19. cabal v2-update
  20. arch=$(getconf LONG_BIT)
  21. case "${arch}" in
  22. 32)
  23. byte_size=4
  24. magic_word="CABA1002"
  25. ;;
  26. 64)
  27. byte_size=8
  28. magic_word="00000000CABA1002"
  29. ;;
  30. *)
  31. error_message "Unknown architecture (long bit): ${arch}"
  32. exit 2
  33. ;;
  34. esac
  35. # This is the logic to parse the binary format of 01-index.cache.
  36. # The first word is a magic 'caba1002', the second one is the timestamp in unix epoch.
  37. # Better than copying the cabal-install source code.
  38. if [ "$(xxd -u -p -l${byte_size} -s 0 "${CACHE_LOCATION}")" != "${magic_word}" ] ; then
  39. error_message "Magic word does not match!"
  40. exit 4
  41. fi
  42. cache_timestamp=$(echo "ibase=16;obase=A;$(xxd -u -p -l${byte_size} -s ${byte_size} "${CACHE_LOCATION}")" | bc)
  43. # If we got junk from the binary file, this should fail.
  44. cache_date=$(date --utc --date "@${cache_timestamp}" "+%FT%TZ")
  45. status_message "Updating index state in ${SCRIPTPATH}/cabal.project"
  46. if grep -q "^index-state: .*" "${SCRIPTPATH}/cabal.project" ; then
  47. awk '/index-state:/ {gsub(/.*/, "index-state: '${cache_date}'")}; { print }' "${SCRIPTPATH}/cabal.project" > "${SCRIPTPATH}/cabal.project.tmp"
  48. mv "${SCRIPTPATH}/cabal.project.tmp" "${SCRIPTPATH}/cabal.project"
  49. else
  50. printf "index-state: %s\n" "${cache_date}" >> "${SCRIPTPATH}/cabal.project"
  51. fi