first check-in. migrated all ugly perl code.