#!/bin/sh
RECEIPTS=/Library/Receipts
BASENAME="textureanarchy"
BASEID=com.digitalanarchy.textureanarchy_le_113ps
MAINID=com.digitalanarchy.textureanarchy_le_113
LINKNAME="Digital Anarchy/Texture Anarchy PS 1.2.5"
THISVOL=`dirname "$0"`
while [[ "$THISVOL" != `dirname "$THISVOL"` && (! -d "$THISVOL/$RECEIPTS") ]] ; do THISVOL=`dirname "$THISVOL"` ; done
if [[ "$THISVOL" == "." ]]; then
  THISVOL=/
fi
if [[ "$THISVOL" == "" ]]; then
  THISVOL=/
fi
for i in "" 7 cs cs2 cs3 cs4 cs5 cs51 cs6 cc cc2014 cc2015 cccommon elems4 elems5 elems6 elems7 elems8 elems9 elems10 elems11 elems12 elems13 elems14 other; do
  if [[ -x /usr/sbin/pkgutil ]]; then
    ID="${BASEID}${i}"
    if [[ -z "$i" ]]; then
      ID="${MAINID}"
    fi
    for vol in / "$HOME" "$THISVOL"; do
      VOLUME=`pkgutil --volume "$vol" --pkg-info "${ID}" 2>/dev/null | egrep '^volume: ' | sed 's/^volume: //'`
      INSTPATH=`pkgutil --volume "$vol" --pkg-info "${ID}" 2>/dev/null | egrep '^location: ' | sed 's/^location: //'`
      if [[ -d "${VOLUME}" && -d "${VOLUME}/${INSTPATH}" ]]; then
        echo "${VOLUME}/${INSTPATH}"
        (cd "${VOLUME}/${INSTPATH}" && pkgutil --volume $vol --files $ID 2>/dev/null | sort -r | tr '\n' '\0' | xargs -0 rm -df)
      fi
      pkgutil --force --volume "$vol" --forget "${ID}" 2>/dev/null
    done
  fi
  NAME="${BASENAME}${i}"
  for vol in / "$HOME" "$THISVOL"; do
    PKG="${vol}/${RECEIPTS}/${NAME}.pkg"
    if [[ -e "${PKG}" ]]; then
      VOLUME="${vol}"
      INSTPATH=`defaults read "${PKG}/Contents/Info" IFPkgRelocatedPath`
      if [[ -d "${VOLUME}/${INSTPATH}" ]]; then
        (cd "${VOLUME}/${INSTPATH}" && lsbom -s "${PKG}"/Contents/Archive.bom | sort -r | tr '\n' '\0' | xargs -0 rm -df)
      fi
      rm -rf "${PKG}"
    fi
  done
done
if [[ -h "/Library/Application Support/$LINKNAME" ]]; then
  rm -f "/Library/Application Support/$LINKNAME"
fi
