summaryrefslogtreecommitdiffstats
path: root/pomf2gomf.bash
blob: 7f3d172b06e33911ef041fb99138e2482ef0f715 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash

# This software is released into public domain.
# It is provided "as is", without warranties or conditions of any kind.
# Anyone is free to modify, redistribute and do anything with this software.

# pomf2gomf.bash: convert pomf-style flat folders with uploads to gomf storage

set -eu

for req in basename sha1sum cut xxd base64 tr touch; do
	if ! type "$req" >/dev/null; then
		printf "fatal error: missing command %s\n" "$req" >&2
		exit 1
	fi
done

usage() {
	printf "usage: %s POMF_DIR GOMF_DIR\n" "$(basename "$0")"
	printf "  where POMF_DIR is a flat directory with files named after their file IDs and\n"
	printf "  extensions and GOMF_DIR is the root dir (created if necessary) of gomf-web\n"
}

if [ $# -ne 2 ]; then
	usage >&2
	exit 2
fi

pomf="$1"
gomf="$2"

if ! [ -d "$pomf" ]; then
	printf "directory %s does not exist\n" "$pomf" >&2
	exit 1
fi

mkdir -p "$gomf/upload/files"
mkdir -p "$gomf/upload/ids"

for file in "$pomf"/*; do
	name="$(basename "$file")"
	id="${name%%.*}"
	ext="${name#*.}"
	sha="$(sha1sum -b "$file" | cut -d' ' -f1 | xxd -r -p | base64 | tr -d = | tr '+/' '-_')"
	if [ "$name" = "$ext" ]; then # no ext
		ext=
	else
		ext=".$ext"
	fi
	name="$id"
	while [ ${#name} -lt 4 ]; do
		name="_$name"
	done
	id1="${name:0:2}" # first two letters
	id2="${name:2:2}" # third and fourth letter
	hash1="${sha:0:2}" # first two letters
	hash2="${sha:2:2}" # third and fourth letter

	hashd="$gomf/upload/files/$hash1/$hash2/$sha"
	idd="$gomf/upload/ids/$id1/$id2/$id"
	mkdir -p "$hashd"
	cp -va "$file" "$hashd/file"
	mkdir -p "$idd"
	ln -vs "../../../../files/$hash1/$hash2/$sha/file" "$idd/file$ext" # relative link to $gomf/upload/ids file
	touch --no-dereference --reference "$file" "$idd/file$ext"
done