transfer_file(){
  [ -f "$1" ] || return 0
  dos2unix -u "$1"
  source "$1"
  local res=$?
  rm -f "$1"
  return $res
}

transfer_default(){
  rsync -ac \
  --exclude "/install" \
  --exclude "/transfer" \
  --exclude "/uninstall" \
  --exclude "*.hmod" \
  "$transferpath/" "$rootfs/"
}

transfer_path(){
  local transferpath="$1"
  chown -R 0:0 "$transferpath/"
  find "$transferpath/" -maxdepth 1 -type f -iname "readme.*" -delete
  find "$transferpath/" -maxdepth 1 -type f -iname "*.txt" -delete
  find "$transferpath/" -maxdepth 1 -type f -iname "*.md" -delete
  local docopy=y
  cd "$transferpath"
  [ -d "./bin" ] && chmod 755 ./bin/*
  [ -d "./etc/init.d" ] && chmod 755 ./etc/init.d/*
  transfer_file "$transferpath/install" || docopy=n
  cd "$transferpath"
  transfer_file "$transferpath/transfer" || docopy=n
  [ "$docopy" == "y" ] && transfer_default
}

pack_upath(){
  echo "$installpath/hmod/uninstall-$(basename "$1" .hmod)"
}

pack_install(){
  local packFile="$(readlink -f "$1")"
  local packName="$(basename "$1" .hmod)"
  echo "installing $packName..."
  if [ -f "$packFile" ]; then
    local transferpath="$temppath/pack"
    rm -rf "$transferpath"
    mkdir -p "$transferpath"
    cd "$transferpath" && tar -xzf "$packFile"
  else
    local transferpath="$packFile"
  fi
  transfer_path "$transferpath"
  if grep -xqF "no-uninstall" "$transferpath/uninstall"; then
    echo "package $packName installed"
    return 0
  fi
  echo "creating uninstall for $packName..."
  echo >> "$transferpath/uninstall"
  echo "# auto-generated" >> "$transferpath/uninstall"
  cd "$transferpath"
  find . -type l -exec echo rm -f \"\$rootfs/{}\" + >> "$transferpath/uninstall"
  find . -type f -exec echo rm -f \"\$rootfs/{}\" + >> "$transferpath/uninstall"
  find . -depth -mindepth 1 -type d -exec echo rmdir \"\$rootfs/{}\" + >> "$transferpath/uninstall"
  [ $(stat -c%s "$transferpath/uninstall") -gt 8 ] || rm -f "$transferpath/uninstall"
  cd /
  local unfile="$(pack_upath "$packFile")"
  if [ -f "$transferpath/uninstall" ]; then
    dos2unix -u "$transferpath/uninstall"
    sed -i "s#rmdir #rmdir --ignore-fail-on-non-empty #" "$transferpath/uninstall"
    mkdir -p "$installpath/hmod"
    copy "$transferpath/uninstall" "$unfile"
  else
    rm -f "$unfile"
  fi
  echo "package $packName installed"
}

pack_list(){
  if [ -d "$installpath/hmod" ]; then
    for i in $(find "$installpath/hmod/" -maxdepth 1 -type f -name "uninstall-*" | sort); do
      echo "${i##$installpath/hmod/uninstall-}"
    done
  fi
}

pack_uninstall(){
  if [ "$1" == "all" ]; then
    for i in $(pack_list); do
      pack_uninstall "$i"
    done
  else
    local unfile="$(pack_upath "$1")"
    if [ -f "$unfile" ]; then
      local packName="$(basename "$1" .hmod)"
      echo "uninstalling $packName..."
      cd "$rootfs"
      transfer_file "$unfile"
      echo "package $packName uninstalled"
    fi
  fi
  cd /
  rmdir --ignore-fail-on-non-empty "$installpath/hmod"
}

packs_install(){
  [ -d "$1" ] || return 1
  for i in $(find "$1" -maxdepth 1 -name "*.hmod" | sort); do
    pack_install "$i"
    rm -rf "$i"
  done
}

packs_uninstall(){
  for i in $@; do
    pack_uninstall "$i"
  done
}
