#!/bin/sh

: ${USER_HOME="/var/spool/ipfs"}
: ${USER_NAME="ipfs"}
: ${IPFS_PATH="/var/lib/ipfs"}


if [ "$1" = "configure" ]; then
    echo "Reloading systemd daemon"
    systemctl daemon-reload
    echo "Enabling ipfs.service"
    systemctl enable ipfs.service
    if [ ! -d ${USER_HOME} ]; then
        mkdir -p "${USER_HOME}"
        chown -R ${USER_NAME}:${USER_NAME} "${USER_HOME}"
    fi
    if [ -d "${IPFS_PATH}" ] && [ ! -f "${USER_HOME}/.init-complete" ]; then
        echo " 
        !!!!!!!!!!!!!!!!!!!!!!!!!!
        For safety, we're going to assume you're running on a server, or other setup where you're directly connected to the internet / there is no LAN,
        thus we're going to use the server profile, to prevent IPFS from scanning private subnets to find hosts on the LAN.

        Going to run the following command:

            su -c \"bash -c 'IPFS_PATH="${IPFS_PATH}" ipfs init --profile server'\" ipfs

        If you ARE on a private LAN and not connected directly to the internet (i.e. a computer on a personal home network),
        then you may re-enable private subnet scanning / MDNS discovery by running the command:
            
            su -c \"bash -c 'IPFS_PATH="${IPFS_PATH}" ipfs config profile apply local-discovery\" ipfs
            systemctl restart ipfs

        (If you're using a server provided by a hosting company such as Privex, DigitalOcean, Linode, Hetzner, Google Cloud etc.,
         do NOT enable local-discovery without first contacting your hosting company and confirming that both running IPFS is allowed,
         and that their firewalls won't freak out over IPFS scanning private subnets such as 192.168.0.0/16 and 10.0.0.0/8)
        !!!!!!!!!!!!!!!!!!!!!!!!!!

        "

        su -c "bash -c 'IPFS_PATH="${IPFS_PATH}" ipfs init --profile server'" ipfs
        if ! [[ -f /etc/fuse.conf ]]; then
            touch /etc/fuse.conf
        fi
        if ! grep -Eq '^user_allow_other' /etc/fuse.conf; then
            echo " >> Adding user_allow_other to /etc/fuse.conf"
            echo "user_allow_other" >> /etc/fuse.conf
        fi
        echo " >> Setting IPFS config Mounts.FuseAllowOther to 'true'..."
        su -c "bash -c 'IPFS_PATH="${IPFS_PATH}" ipfs config --bool Mounts.FuseAllowOther true"
        echo " >> Setting IPFS config Peering.Peers to contain a variety of known IPFS peers such as cloudflare, pinata, and privex..."
        su --preserve-environment -c "ipfs config --json Peering.Peers '[{\"ID\":\"QmcfgsJsMtx6qJb74akCw1M24X1zFwgGo11h1cuhwQjtJP\",\"Addrs\":[\"/ip6/2606:4700:60::6/tcp/4009\",\"/ip4/172.65.0.13/tcp/4009\"]},{\"ID\":\"QmWaik1eJcGHq1ybTWe7sezRfqKNcDRNkeBaLnGwQJz1Cj\",\"Addrs\":[\"/dnsaddr/fra1-1.hostnodes.pinata.cloud\"]},{\"ID\":\"QmNfpLrQQZr5Ns9FAJKpyzgnDL2GgC6xBug1yUZozKFgu4\",\"Addrs\":[\"/dnsaddr/fra1-2.hostnodes.pinata.cloud\"]},{\"ID\":\"QmUEMvxS2e7iDrereVYc5SWPauXPyNwxcy9BXZrC1QTcHE\",\"Addrs\":[\"/dns/cluster0.fsn.dwebops.pub\"]},{\"ID\":\"QmNSYxZAiJHeLdkBg38roksAR9So7Y5eojks1yjEcUtZ7i\",\"Addrs\":[\"/dns/cluster1.fsn.dwebops.pub\"]},{\"ID\":\"12D3KooWCRscMgHgEo3ojm8ovzheydpvTEqsDtq7Vby38cMHrYjt\",\"Addrs\":[\"/dns4/nft-storage-am6.nft.dwebops.net/tcp/18402\"]},{\"ID\":\"12D3KooWQcgCwNCTYkyLXXQSZuL5ry1TzpM8PRe9dKddfsk1BxXZ\",\"Addrs\":[\"/dns4/nft-storage-sv15.nft.dwebops.net/tcp/18402\"]},{\"ID\":\"QmR69wtWUMm1TWnmuD4JqC1TWLZcc8iR2KrTenfZZbiztd\",\"Addrs\":[\"/ip4/104.210.43.77\"]},{\"ID\":\"12D3KooWLMyLSQgQ1Y6RmtCZAQge6FwQTnLZkMRuUa6wgwajGqav\",\"Addrs\":[\"/dns4/cust06.se1.privex.cc/tcp/4001\",\"/dns6/cust06.se1.privex.cc/tcp/4001\"]},{\"ID\":\"12D3KooWQBYACq2A8XeeiZWiHhp2TkqK9RLyJtpFoby3pJYP3uTf\",\"Addrs\":[\"/dns4/fin1.files.privex.io/tcp/4001\",\"/dns6/fin1.files.privex.io/tcp/4001\"]},{\"ID\":\"12D3KooWJnrTQgRjv1VNsdcwJPym4sVcF8eNbECyqYXidd455fEr\",\"Addrs\":[\"/dns4/se1-filesrv-new.privex.bz/tcp/4001\",\"/dns6/se1-filesrv-new.privex.bz/tcp/4001\"]},{\"ID\":\"12D3KooWJefeaXhgJGmxzcqFq9twQASbt9Fuc2Ez7MRgrcpt3aLu\",\"Addrs\":[\"/dns4/de1.files.privex.io/tcp/4001\",\"/dns6/de1.files.privex.io/tcp/4001\"]}]'" ipfs
        echo " >> Setting IPFS config Bootstrap to contain a variety of known IPFS peers such as cloudflare, pinata, and privex..."
        su --preserve-environment -c "ipfs config --json Bootstrap '[\"/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN\",\"/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa\",\"/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb\",\"/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt\",\"/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ\",\"/ip4/104.131.131.82/udp/4001/quic/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ\",\"/dns4/se1-filesrv-new.privex.bz/tcp/4001/p2p/12D3KooWJnrTQgRjv1VNsdcwJPym4sVcF8eNbECyqYXidd455fEr\",\"/dns6/se1-filesrv-new.privex.bz/tcp/4001/p2p/12D3KooWJnrTQgRjv1VNsdcwJPym4sVcF8eNbECyqYXidd455fEr\",\"/dns4/cust06.se1.privex.cc/tcp/4001/p2p/12D3KooWLMyLSQgQ1Y6RmtCZAQge6FwQTnLZkMRuUa6wgwajGqav\",\"/dns6/cust06.se1.privex.cc/tcp/4001/p2p/12D3KooWLMyLSQgQ1Y6RmtCZAQge6FwQTnLZkMRuUa6wgwajGqav\",\"/dns4/fin1.files.privex.io/tcp/4001/p2p/12D3KooWQBYACq2A8XeeiZWiHhp2TkqK9RLyJtpFoby3pJYP3uTf\",\"/dns6/fin1.files.privex.io/tcp/4001/p2p/12D3KooWQBYACq2A8XeeiZWiHhp2TkqK9RLyJtpFoby3pJYP3uTf\",\"/dns4/se1.files.privex.io/tcp/4001/p2p/12D3KooWJxi43jwEV8WKvPRJtiGi89UXRDyYgDyHtbHZNG8FvNnS\",\"/dns6/se1.files.privex.io/tcp/4001/p2p/12D3KooWJxi43jwEV8WKvPRJtiGi89UXRDyYgDyHtbHZNG8FvNnS\",\"/dns4/de1.files.privex.io/tcp/4001/p2p/12D3KooWJefeaXhgJGmxzcqFq9twQASbt9Fuc2Ez7MRgrcpt3aLu\",\"/dns6/de1.files.privex.io/tcp/4001/p2p/12D3KooWJefeaXhgJGmxzcqFq9twQASbt9Fuc2Ez7MRgrcpt3aLu\"]'" ipfs
        touch "${USER_HOME}/.init-complete"
    fi

    echo "Restarting ipfs.service"
    systemctl start ipfs.service || >&2 echo "Failed to start ipfs.service - missing config?"

    if [ ! -f "/etc/ipfs.json" ]; then
        echo " >> Config symlink /etc/ipfs.json does not exist. Creating it..."
        ln -s "${IPFS_PATH}/config" /etc/ipfs.json
    fi
    
    if [ ! -f "${USER_HOME}/.firstinstall" ]; then
        cat /tmp/ipfs-help.txt
        touch "${USER_HOME}/.firstinstall"
    fi

fi

