#!/bin/sh
##
## This script can be run on the empeg to automatically
## sift and sort the FIDS into subdirectories to help
## optimise filesystem access for rebuilds and fscks.
##
function dosift() {
	echo "Sifting $1.."
	cd $1
	for fid in [0-9a-f]*; do
		f="00000${fid}"
		f="${f:${#f}-8}"
		d="_${f:0:5}"
		f="${f:5:3}"
		mkdir --verbose -p $d || exit -1
		mv ${fid} ${d}/${f} || exit -1
	done
	cd /
}

function dodrive() {
	drive=$1
	if [ -d ${drive}/fids ]; then
		mount -n ${drive} -oremount,rw,nocheck && dosift ${drive}/fids
		mount -n ${drive} -oremount,ro
		sync
	fi
}

## main() {
	dodrive /drive0
	dodrive /drive1
#}
