1 #!/usr/bin/env bash
2 # Copyright (c) 2021-present The Bitcoin Core developers
3 # Distributed under the MIT software license, see the accompanying
4 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 6 export LC_ALL=C
7 TOPDIR=${TOPDIR:-$(git rev-parse --show-toplevel)}
8 BUILDDIR=${BUILDDIR:-$TOPDIR/build}
9 BINDIR=${BINDIR:-$BUILDDIR/bin}
10 BITCOIND=${BITCOIND:-$BINDIR/bitcoind}
11 SHARE_EXAMPLES_DIR=${SHARE_EXAMPLES_DIR:-$TOPDIR/share/examples}
12 EXAMPLE_CONF_FILE=${EXAMPLE_CONF_FILE:-$SHARE_EXAMPLES_DIR/bitcoin.conf}
13 14 [ ! -x "$BITCOIND" ] && echo "$BITCOIND not found or not executable." && exit 1
15 16 DIRTY=""
17 VERSION_OUTPUT=$($BITCOIND --version)
18 if [[ $VERSION_OUTPUT == *"dirty"* ]]; then
19 DIRTY="${DIRTY}${BITCOIND}\n"
20 fi
21 22 if [ -n "$DIRTY" ]
23 then
24 echo -e "WARNING: $BITCOIND was built from a dirty tree.\n"
25 echo -e "To safely generate a bitcoin.conf file, please commit your changes to $BITCOIND, rebuild, then run this script again.\n"
26 fi
27 28 echo 'Generating example bitcoin.conf file in share/examples/'
29 30 # create the directory, if it doesn't exist
31 mkdir -p "${SHARE_EXAMPLES_DIR}"
32 33 # create the header text
34 cat > "${EXAMPLE_CONF_FILE}" << 'EOF'
35 ##
36 ## bitcoin.conf configuration file.
37 ## Generated by contrib/devtools/gen-bitcoin-conf.sh.
38 ##
39 ## Lines beginning with # are comments.
40 ## All possible configuration options are provided. To use, copy this file
41 ## to your data directory (default or specified by -datadir), uncomment
42 ## options you would like to change, and save the file.
43 ##
44 45 46 ### Options
47 EOF
48 49 # parse the output from bitcoind --help
50 # adding newlines is a bit funky to ensure portability for BSD
51 # see here for more details: https://stackoverflow.com/a/24575385
52 ${BITCOIND} --help \
53 | sed '1,/Options:/d' \
54 | sed -E '/^[[:space:]]{2}-help/,/^[[:space:]]*$/d' \
55 | sed -E 's/^[[:space:]]{2}\-/#/' \
56 | sed -E 's/^[[:space:]]{7}/# /' \
57 | sed -E '/[=[:space:]]/!s/#.*$/&=1/' \
58 | awk '/^#[a-z]/{x=$0;next}{if (NF==0) print x"\n",x="";else print}' \
59 | sed 's,\(^[[:upper:]].*\)\:$,\
60 ### \1,' \
61 | sed 's/[[:space:]]*$//' >> "${EXAMPLE_CONF_FILE}"
62 63 # create the footer text
64 cat >> "${EXAMPLE_CONF_FILE}" << 'EOF'
65 66 # [Sections]
67 # Most options will apply to all networks. To confine an option to a specific
68 # network, add it under the relevant section below.
69 #
70 # Note: If not specified under a network section, the options addnode, connect,
71 # port, bind, rpcport, rpcbind, and wallet will only apply to mainnet.
72 73 # Options for mainnet
74 [main]
75 76 # Options for testnet3
77 [test]
78 79 # Options for testnet4
80 [testnet4]
81 82 # Options for signet
83 [signet]
84 85 # Options for regtest
86 [regtest]
87 EOF
88