#!/bin/bash
IFS=$'\n'
cd "$(dirname $0)"
delListPath="lists"
echo "----------"
for eachFile in `ls $delListPath`
do
  echo $eachFile
  # sedの処理：'#'以降を削除：行末の空白を削除：空白行を削除
  for eachLine in `cat $delListPath/$eachFile|sed s/#.*$//|sed s/[[:blank:]]*$//|sed /^$/d`
  do
    # ファイルの削除
    if [ ${eachLine:0:1} = "f" ]
    then
      echo "delete file:${eachLine:1}"
      rm -f "${eachLine:1}"
    fi
    # ディレクトリの再帰的削除
    if [ ${eachLine:0:1} = "d" ]
    then
      echo "delete dir:${eachLine:1}"
      rm -fR "${eachLine:1}"
    fi
    # pkgutilによるインストール情報の削除
    if [ ${eachLine:0:1} = "p" ]
    then
      echo "forget pkg:${eachLine:1}"
      pkgutil --forget "${eachLine:1}"
    fi
    # ディレクトリが空の場合、そのディレクトリを削除
    if [ ${eachLine:0:1} = "c" ]
    then
      # .DS_Storeは削除しておく
      find "${eachLine:1}" -name .DS_Store -delete
      # findコマンドに絶対パスを渡すとだめな場合があるので、相対パスで渡す
      pushd "${eachLine:1}"
      cd ..
      find `basename "${eachLine:1}"` -type d -empty -delete
      popd
    fi
  done
  echo "----------"
done
