#!/bin/sh

MAPPINGS=$*
ID_MAPPING="mapping-id-to-page.txt"
TINY_ID_MAPPING="mapping-tiny-to-id.txt"
TINY_MAPPING="mapping-tiny-to-page.txt"
TAB=`printf '\t'`

# Combine the space mappings into a common mapping from page identifiers to
# page names.
sort -n -u $MAPPINGS > "$ID_MAPPING"

# Produce a common mapping from tiny URL identifiers to page names.
cut -f 1 "$ID_MAPPING" | uniq | python tiny.py - > "$TINY_ID_MAPPING"
join -t "$TAB" -1 2 -2 1 "$TINY_ID_MAPPING" "$ID_MAPPING" | cut -f 2,3 | LC_ALL=C sort > "$TINY_MAPPING"
