summaryrefslogtreecommitdiffstats
path: root/apps/plugins/puzzles/resync.sh
blob: 7c2df45c7e56b4f4e45dd6a37b59a9094eecdfdb (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/sh
# Usage: resync.sh PUZZLES_PATH
#
# Automatic resync tool. Removes the current source snapshot in src/
# and copies just the source files we need from the puzzles source
# tree. Handles help generation as well. Stages changes in git.
#
# Expects a modified Halibut (https://www.fwei.tk/git/halibut) to be
# installed in $PATH. Also requires host CC and lz4 library to be
# available


if [ $# -ne 1 ]
then
    echo -e "Usage: $0 PUZZLES_PATH\n"
    echo "Automatically resync with upstream."
    echo "PUZZLES_PATH is the path to a puzzles source tree."
    exit
fi

echo "=== POTENTIALLY DANGEROUS OPERATION ==="
echo "Are you sure you want to remove all files in src/ and help/?"
echo -n "If so, type \"yes\" in all caps: "
read ans
if [ "YES" == $ans ]
then
    pushd "$(dirname "$0")" > /dev/null
    ROOT="$PWD"

    echo "[1/5] Removing current src/ directory"
    rm -rf src
    echo "[2/5] Copying new sources"
    mkdir src
    cp -r "$1"/{*.h,puzzles.but,LICENCE,README,CMakeLists.txt} src

    # Parse out definitions of core, core_obj, and common from
    # CMakeLists. Extract the .c filenames, except malloc.c, and store
    # in SOURCES.core.
    cat src/CMakeLists.txt | awk '/add_library\(/{p=1} p{printf $0" "} /\)/{if(p) print; p=0}' | grep -E "core|common" | grep -Po "[a-z0-9\-]*?\.c" | sort -n | grep -vE 'malloc\.c|ps\.c' | awk '{print "src/"$0}' | uniq > SOURCES.core
    echo "src/printing.c" >> SOURCES.core

    # Parse out puzzle definitions to build SOURCES.games, but
    # preserve the ability to disable puzzles based on memory size.
    cat src/CMakeLists.txt | awk '/puzzle\(/{p=1} p{print} /\)/{p=0}' | grep -Eo "\(.*$" | tr -dc "a-z\n" | grep -v nullgame | awk '$0!~/loopy|pearl|solo/' | awk '{print "src/"$0".c"}' > SOURCES.games

    SRC="$(cat SOURCES.games SOURCES.core | sed 's/src\///' | tr '\n' ' ' | head -c-1) loopy.c pearl.c solo.c"
    echo "Detected sources:" $SRC
    pushd "$1" > /dev/null
    cp $SRC "$ROOT"/src
    popd > /dev/null

    cat <<EOF >> SOURCES.games

/* Disabled for now. Fix puzzles.make and CATEGORIES to accomodate these. */
/* The help system would also need to be patched to compile these. */
/*src/unfinished/group.c*/
/*src/unfinished/separate.c*/
/*src/unfinished/slide.c*/
/*src/unfinished/sokoban.c*/

/* no c200v2 */
#if PLUGIN_BUFFER_SIZE > 0x14000
src/loopy.c
src/pearl.c
src/solo.c
#endif
EOF

    cat <<EOF > SOURCES
/* Auto-generated by resync.sh */
EOF
    cat SOURCES.rockbox | cpp | grep -vE "^#" >> SOURCES
    echo -e "\n/* puzzles core sources */" >> SOURCES
    cat SOURCES.core >> SOURCES
    rm SOURCES.core

    echo "[3/5] Regenerating help"
    rm -rf help
    ./genhelp.sh

    echo "[4/5] Staging for commit"
    git add src help
    echo "[5/5] Successfully resynced with upstream"

    popd > /dev/null
else
    echo "Did nothing."
fi