| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619 | #!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# ipfs functions
#
# License
# =======
#
# Copyright (C) 2014-2016 Bob Mottram <bob@robotics.uk.to>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
VARIANTS='mesh'
IPFS_GO_REPO_BASE="github.com/ipfs/go-ipfs"
IPFS_GO_REPO="https://${IPFS_GO_REPO_BASE}"
IPFS_COMMIT='20b06a4cbce8884f5b194da6e98cb11f2c77f166'
IPFS_PORT=4001
IPFS_NODE_VERSION='6.2.2'
IPFS_N_VERSION='2.1.4'
IPFS_JS_VERSION='0.14.3'
IPFS_JS_RONIN_VERSION='0.3.11'
IPFS_KEY_LENGTH=2048
function reconfigure_ipfs {
    echo -n ''
}
function upgrade_ipfs_go {
    if ! grep -Fxq "install_ipfs_go" $COMPLETION_FILE; then
        return
    fi
    function_check select_go_version
    select_go_version
    function_check set_repo_commit
    set_repo_commit $GOPATH/src/github.com/ipfs/go-ipfs "ipfs commit" "$IPFS_COMMIT" $IPFS_REPO
}
function upgrade_ipfs_js {
    if ! grep -Fxq "install_ipfs_js" $COMPLETION_FILE; then
        return
    fi
    npm cache clean -f
    npm install -g n
    n ${IPFS_NODE_VERSION}
    npm install ronin@${IPFS_JS_RONIN_VERSION} --global
    npm install ipfs@${IPFS_JS_VERSION} --global
}
function upgrade_ipfs {
    upgrade_ipfs_js
    upgrade_ipfs_go
}
function backup_local_ipfs {
    if ! grep -q "Admin user" $COMPLETION_FILE; then
        return
    fi
    ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
    source_directory=/home/$ADMIN_USERNAME/.ipfs
    if [ -d $source_directory ]; then
        dest_directory=ipfs
        echo $"Backing up $source_directory to $dest_directory"
        function_check backup_directory_to_usb
        backup_directory_to_usb $source_directory $dest_directory
        echo $"Backup to $dest_directory complete"
    fi
}
function restore_local_ipfs {
    if ! grep -q "Admin user" $COMPLETION_FILE; then
        return
    fi
    if [ -d $USB_MOUNT/backup/ipfs ]; then
        echo $"Restoring IPFS"
        temp_restore_dir=/root/tempipfs
        function_check restore_directory_from_usb
        restore_directory_from_usb $temp_restore_dir ipfs
        ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
        cp -rf $temp_restore_dir/home/$ADMIN_USERNAME/.ipfs/* /home/$ADMIN_USERNAME/.ipfs
        if [ ! "$?" = "0" ]; then
            rm -rf $temp_restore_dir
            function_check set_user_permissions
            set_user_permissions
            function_check backup_unmount_drive
            backup_unmount_drive
            exit 27627
        fi
        rm -rf $temp_restore_dir
        echo $"Restore of IPFS complete"
    fi
}
function backup_remote_ipfs {
    if ! grep -q "Admin user" $COMPLETION_FILE; then
        return
    fi
    ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
    if [ -d /home/$ADMIN_USERNAME/.ipfs ]; then
        echo $"Backing up IPFS"
        backup_directory_to_friend /home/$ADMIN_USERNAME/.ipfs ipfs
        echo $"Backup of IPFS complete"
    fi
}
function restore_remote_ipfs {
    if [ -d $SERVER_DIRECTORY/backup/ipfs ]; then
        echo $"Restoring IPFS"
        temp_restore_dir=/root/tempipfs
        function_check restore_directory_from_friend
        restore_directory_from_friend $temp_restore_dir ipfs
        cp -rf $temp_restore_dir/home/$ADMIN_USERNAME/.ipfs/* /home/$ADMIN_USERNAME/.ipfs
        if [ ! "$?" = "0" ]; then
            function_check set_user_permissions
            set_user_permissions
            rm -rf $temp_restore_dir
            exit 276357
        fi
        rm -rf $temp_restore_dir
        echo $"Restore of IPFS complete"
    fi
}
function remove_ipfs_go {
    if ! grep -Fxq "install_ipfs_go" $COMPLETION_FILE; then
        return
    fi
    function_check select_go_version
    select_go_version
    systemctl stop ipfs
    systemctl disable ipfs
    systemctl daemon-reload
    rm /etc/systemd/system/ipfs.service
    rm -rf $GOPATH/src/github.com/ipfs
    iptables -D INPUT -p tcp --dport $IPFS_PORT -j ACCEPT
    function_check save_firewall_settings
    save_firewall_settings
    sed -i '/install_ipfs/d' $COMPLETION_FILE
    sed -i '/ipfs/d' $COMPLETION_FILE
    sed -i '/configure_firewall_for_ipfs/d' $COMPLETION_FILE
}
function remove_ipfs_js {
    if ! grep -Fxq "install_ipfs_js" $COMPLETION_FILE; then
        return
    fi
    systemctl stop ipfs
    systemctl disable ipfs
    rm /etc/systemd/system/ipfs.service
    systemctl daemon-reload
    npm uninstall ipfs --global
    npm uninstall ronin --global
    iptables -D INPUT -p tcp --dport $IPFS_PORT -j ACCEPT
    function_check save_firewall_settings
    save_firewall_settings
    sed -i '/install_ipfs/d' $COMPLETION_FILE
    sed -i '/ipfs/d' $COMPLETION_FILE
    sed -i '/configure_firewall_for_ipfs/d' $COMPLETION_FILE
}
function remove_ipfs {
    remove_ipfs_js
    remove_ipfs_go
}
function configure_firewall_for_ipfs {
    if grep -Fxq "configure_firewall_for_ipfs" $COMPLETION_FILE; then
        return
    fi
    if [[ $ONION_ONLY != "no" ]]; then
        return
    fi
    iptables -A INPUT -p tcp --dport $IPFS_PORT -j ACCEPT
    function_check save_firewall_settings
    save_firewall_settings
    OPEN_PORTS+=("IPFS     $IPFS_PORT")
    echo 'configure_firewall_for_ipfs' >> $COMPLETION_FILE
}
function mesh_install_ipfs_js {
    if grep -Fxq "mesh_install_ipfs_js" $COMPLETION_FILE; then
        return
    fi
    chroot ${rootdir} apt-get -y install nodejs
    chroot ${rootdir} apt-get -y install npm curl
    chroot ${rootdir} apt-get -y install libpam0g-dev fuse
    if [ ! -f ${rootdir}/usr/bin/nodejs ]; then
        echo $'nodejs was not installed'
        exit 63962
    fi
    cat <<EOF > ${rootdir}/root/install-ipfs.sh
#!/bin/bash
PATH="/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/bin"
NODE_PATH="/usr/lib/node_modules"
cp /usr/bin/nodejs /usr/local/bin/node
cp /usr/bin/nodejs /usr/bin/node
/usr/bin/curl -0 -L https://npmjs.org/install.sh | sh
npm install -g n@${IPFS_N_VERSION} --save
n ${IPFS_NODE_VERSION}
npm install -g ronin@${IPFS_JS_RONIN_VERSION} --save
npm install -g ipfs@${IPFS_JS_VERSION} --save
exit 0
EOF
    chroot ${rootdir} chmod +x /root/install-ipfs.sh
    chroot ${rootdir} /root/install-ipfs.sh
    rm -f ${rootdir}/root/install-ipfs.sh
    IPFS_PATH=/usr/bin
    if [ ! -f ${rootdir}$IPFS_PATH/jsipfs ]; then
        exit 637292
    fi
    # directories to mount to
    if [ ! -d ${rootdir}/ipfs ]; then
        chroot ${rootdir} mkdir /ipfs
        chroot ${rootdir} mkdir /ipns
        chroot ${rootdir} chown $MY_USERNAME:$MY_USERNAME /ipfs
        chroot ${rootdir} chown $MY_USERNAME:$MY_USERNAME /ipns
    fi
    if [ -f ${rootdir}/etc/fuse.conf ]; then
        chroot ${rootdir} chown $MY_USERNAME:$MY_USERNAME /etc/fuse.conf
    fi
    if [ -f ${rootdir}/dev/fuse ]; then
        chroot ${rootdir} chown $MY_USERNAME:$MY_USERNAME /dev/fuse
    fi
    IPFS_DAEMON_NAME=ipfs
    IPFS_DAEMON_FILE=${rootdir}/etc/systemd/system/${IPFS_DAEMON_NAME}.service
    echo '[Unit]' > $IPFS_DAEMON_FILE
    echo 'Description=IPFS javascript daemon' >> $IPFS_DAEMON_FILE
    echo 'After=syslog.target' >> $IPFS_DAEMON_FILE
    echo 'After=network.target' >> $IPFS_DAEMON_FILE
    echo '' >> $IPFS_DAEMON_FILE
    echo '[Service]' >> $IPFS_DAEMON_FILE
    echo 'Type=simple' >> $IPFS_DAEMON_FILE
    echo "User=$MY_USERNAME" >> $IPFS_DAEMON_FILE
    echo "Group=$MY_USERNAME" >> $IPFS_DAEMON_FILE
    echo "WorkingDirectory=/home/$MY_USERNAME" >> $IPFS_DAEMON_FILE
    echo "ExecStart=${IPFS_PATH}/jsipfs daemon --mount" >> $IPFS_DAEMON_FILE
    echo 'Restart=on-failure' >> $IPFS_DAEMON_FILE
    echo "Environment=\"USER=$MY_USERNAME\" \"HOME=/home/$MY_USERNAME\"" >> $IPFS_DAEMON_FILE
    echo '' >> $IPFS_DAEMON_FILE
    echo '[Install]' >> $IPFS_DAEMON_FILE
    echo 'WantedBy=multi-user.target' >> $IPFS_DAEMON_FILE
    chroot ${rootdir} systemctl enable ${IPFS_DAEMON_NAME}
    echo 'mesh install_ipfs_js' >> $COMPLETION_FILE
}
function install_ipfs_js {
    if [ $INSTALLING_MESH ]; then
        mesh_install_ipfs_js
        return
    fi
    if grep -Fxq "install_ipfs_js" $COMPLETION_FILE; then
        return
    fi
    apt-get -y install nodejs
    apt-get -y install npm
    apt-get -y install libpam0g-dev fuse
    if [ ! -f /usr/bin/nodejs ]; then
        echo $'nodejs was not installed'
        exit 63962
    fi
    cp /usr/bin/nodejs /usr/local/bin/node
    cp /usr/bin/nodejs /usr/bin/node
    /usr/bin/curl -0 -L https://npmjs.org/install.sh | sh
    /usr/local/bin/npm install -g n@${IPFS_N_VERSION}
    /usr/local/bin/n ${IPFS_NODE_VERSION}
    /usr/local/bin/npm install -g ronin@${IPFS_JS_RONIN_VERSION}
    /usr/local/bin/npm install -g ipfs@${IPFS_JS_VERSION}
    IPFS_PATH=/usr/bin
    if [ ! -f $IPFS_PATH/jsipfs ]; then
        exit 637292
    fi
    # initialise
    su -c "$IPFS_PATH/jsipfs init -b $IPFS_KEY_LENGTH" - $MY_USERNAME
    if [ ! -d /home/$MY_USERNAME/.ipfs ]; then
        echo "IPFS could not be initialised for user $MY_USERNAME"
        exit 7358
    fi
    # directories to mount to
    if [ ! -d /ipfs ]; then
        mkdir /ipfs
        mkdir /ipns
        chown $MY_USERNAME:$MY_USERNAME /ipfs
        chown $MY_USERNAME:$MY_USERNAME /ipns
    fi
    if [ -f /etc/fuse.conf ]; then
        chown $MY_USERNAME:$MY_USERNAME /etc/fuse.conf
    fi
    if [ -f /dev/fuse ]; then
        chown $MY_USERNAME:$MY_USERNAME /dev/fuse
    fi
    IPFS_DAEMON_NAME=ipfs
    IPFS_DAEMON_FILE=/etc/systemd/system/${IPFS_DAEMON_NAME}.service
    echo '[Unit]' > $IPFS_DAEMON_FILE
    echo 'Description=IPFS javascript daemon' >> $IPFS_DAEMON_FILE
    echo 'After=syslog.target' >> $IPFS_DAEMON_FILE
    echo 'After=network.target' >> $IPFS_DAEMON_FILE
    echo '' >> $IPFS_DAEMON_FILE
    echo '[Service]' >> $IPFS_DAEMON_FILE
    echo 'Type=simple' >> $IPFS_DAEMON_FILE
    echo "User=$MY_USERNAME" >> $IPFS_DAEMON_FILE
    echo "Group=$MY_USERNAME" >> $IPFS_DAEMON_FILE
    echo "WorkingDirectory=/home/$MY_USERNAME" >> $IPFS_DAEMON_FILE
    echo "ExecStart=${IPFS_PATH}/jsipfs daemon --mount" >> $IPFS_DAEMON_FILE
    echo 'Restart=on-failure' >> $IPFS_DAEMON_FILE
    echo "Environment=\"USER=$MY_USERNAME\" \"HOME=/home/$MY_USERNAME\"" >> $IPFS_DAEMON_FILE
    echo '' >> $IPFS_DAEMON_FILE
    echo '[Install]' >> $IPFS_DAEMON_FILE
    echo 'WantedBy=multi-user.target' >> $IPFS_DAEMON_FILE
    systemctl enable ${IPFS_DAEMON_NAME}
    systemctl daemon-reload
    systemctl restart ${IPFS_DAEMON_NAME}
    if [ -d /etc/avahi ]; then
        su -c "echo $($IPFS_PATH/jsipfs id | grep '\"ID\":' | awk -F '\"' '{print $4}') > /tmp/ipfsid" - $MY_USERNAME
        if [ ! -f /tmp/ipfsid ]; then
            echo 'No IPFS identity was created'
            exit 37895
        fi
        IPFS_PEER_ID=$(cat /tmp/ipfsid)
        if [ ${#IPFS_PEER_ID} -lt 10 ]; then
            echo 'Invalid IPFS peer ID'
            echo "$IPFS_PEER_ID"
            exit 74782
        fi
        rm /tmp/ipfsid
    fi
    function_check configure_firewall_for_ipfs
    configure_firewall_for_ipfs
    echo 'install_ipfs_js' >> $COMPLETION_FILE
}
function mesh_install_ipfs_go {
    chroot ${rootdir} apt-get -y install golang libpam0g-dev fuse
    mesh_upgrade_golang
    GOPATH=/home/git/gvm/pkgsets/go${GO_VERSION}/global
    cat <<EOF > ${rootdir}/root/install_ipfs_go.sh
#!/bin/bash
export GOPATH=$GOPATH
if [ ${#GOPATH} -lt 1 ]; then
    echo $'GOPATH was not set'
    exit 62925
fi
if [ ! -d /home/git ]; then
    adduser --disabled-login --gecos 'Gogs' git
    # install Go
    if ! grep -q "export GOPATH=" ~/.bashrc; then
        echo "export GOPATH=\$GOPATH" >> ~/.bashrc
    else
        sed -i "s|export GOPATH=.*|export GOPATH=\$GOPATH|g" ~/.bashrc
    fi
    systemctl set-environment GOPATH=\$GOPATH
    if ! grep -q "systemctl set-environment GOPATH=" ~/.bashrc; then
        echo "systemctl set-environment GOPATH=\$GOPATH" >> ~/.bashrc
    else
        sed -i "s|systemctl set-environment GOPATH=.*|systemctl set-environment GOPATH=\$GOPATH|g" ~/.bashrc
    fi
    if [ ! -d \$GOPATH ]; then
        mkdir -p \$GOPATH
    fi
fi
IPFS_PATH=\$GOPATH/bin
export PATH="\$GOPATH/bin:\$PATH:"
echo 'export PATH="\$GOPATH/bin:\$PATH:";' >> /home/$MY_USERNAME/.bashrc
if grep -q 'GOPATH/bin' ~/.bashrc; then
    sed -i "s|systemctl set-environment GOPATH=.*|systemctl set-environment GOPATH=\$GOPATH|g" ~/.bashrc
fi
chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.bashrc
echo "go get -u ${IPFS_GO_REPO_BASE}/cmd/ipfs"
go get -u ${IPFS_GO_REPO_BASE}/cmd/ipfs
if [ ! "$?" = "0" ]; then
    exit 8242
fi
if [ ! -d $GOPATH/src/$IPFS_GO_REPO_BASE ]; then
    echo $'go get failed to get ipfs'
    exit 63923
fi
cd \$GOPATH/src/$IPFS_GO_REPO_BASE
git checkout $IPFS_COMMIT -b $IPFS_COMMIT
if [ ! "$?" = "0" ]; then
    exit 735639
fi
exit 0
EOF
    chroot ${rootdir} chmod +x /root/install_ipfs_go.sh
    chroot ${rootdir} /root/install_ipfs_go.sh
    if [ ! "$?" = "0" ]; then
        echo $'Failed to install ipfs go'
        cat ${rootdir}/root/install_ipfs_go.sh
        rm -f ${rootdir}/root/install_ipfs_go.sh
        exit 365728
    fi
    chroot ${rootdir} rm -f /root/install_ipfs_go.sh
    echo '[Unit]' > ${rootdir}/etc/systemd/system/ipfs.service
    echo 'Description=IPFS go daemon' >> ${rootdir}/etc/systemd/system/ipfs.service
    echo 'After=syslog.target' >> ${rootdir}/etc/systemd/system/ipfs.service
    echo 'After=network.target' >> ${rootdir}/etc/systemd/system/ipfs.service
    echo '' >> ${rootdir}/etc/systemd/system/ipfs.service
    echo '[Service]' >> ${rootdir}/etc/systemd/system/ipfs.service
    echo 'Type=simple' >> ${rootdir}/etc/systemd/system/ipfs.service
    echo "User=$MY_USERNAME" >> ${rootdir}/etc/systemd/system/ipfs.service
    echo "Group=$MY_USERNAME" >> ${rootdir}/etc/systemd/system/ipfs.service
    echo "WorkingDirectory=/home/$MY_USERNAME" >> ${rootdir}/etc/systemd/system/ipfs.service
    echo "ExecStart=$IPFS_PATH/ipfs daemon --mount" >> ${rootdir}/etc/systemd/system/ipfs.service
    echo 'Restart=on-failure' >> ${rootdir}/etc/systemd/system/ipfs.service
    echo "Environment=\"USER=$MY_USERNAME\" \"HOME=/home/$MY_USERNAME\" \"GOPATH=$GOPATH\" \"GVM_ROOT=$GVM_HOME\"" >> ${rootdir}/etc/systemd/system/ipfs.service
    echo '' >> ${rootdir}/etc/systemd/system/ipfs.service
    echo '[Install]' >> ${rootdir}/etc/systemd/system/ipfs.service
    echo 'WantedBy=multi-user.target' >> ${rootdir}/etc/systemd/system/ipfs.service
    chroot ${rootdir} systemctl enable ipfs
}
function install_ipfs_go {
    if [ $INSTALLING_MESH ]; then
        mesh_install_ipfs_go
        return
    fi
    if grep -Fxq "install_ipfs_go" $COMPLETION_FILE; then
        return
    fi
    function_check select_go_version
    select_go_version
    apt-get -y install golang libpam0g-dev fuse
    if [ ! -d /home/git ]; then
        # add a gogs user account
        adduser --disabled-login --gecos 'Gogs' git
        # install Go
        if ! grep -q "export GOPATH=" ~/.bashrc; then
            echo "export GOPATH=$GOPATH" >> ~/.bashrc
        else
            sed -i "s|export GOPATH=.*|export GOPATH=$GOPATH|g" ~/.bashrc
        fi
        systemctl set-environment GOPATH=$GOPATH
        if ! grep -q "systemctl set-environment GOPATH=" ~/.bashrc; then
            echo "systemctl set-environment GOPATH=$GOPATH" >> ~/.bashrc
        else
            sed -i "s|systemctl set-environment GOPATH=.*|systemctl set-environment GOPATH=$GOPATH|g" ~/.bashrc
        fi
        if [ ! -d $GOPATH ]; then
            mkdir -p $GOPATH
        fi
    fi
    IPFS_PATH=$GOPATH/bin
    export PATH="$GOPATH/bin:$PATH:"
    if ! grep -q 'GOPATH/bin' ~/.bashrc; then
        echo 'export PATH="$GOPATH/bin:$PATH:";' >> ~/.bashrc
    else
        sed -i "s|systemctl set-environment GOPATH=.*|systemctl set-environment GOPATH=$GOPATH|g" ~/.bashrc
    fi
    # set gopath for the user
    if ! grep -q "GOPATH=" /home/$MY_USERNAME/.bashrc; then
        echo "export GOPATH=$GOPATH" >> /home/$MY_USERNAME/.bashrc
        echo 'export PATH="$GOPATH/bin:$PATH:";' >> /home/$MY_USERNAME/.bashrc
    else
        sed -i "s|export GOPATH=.*|export GOPATH=$GOPATH|g" /home/$MY_USERNAME/.bashrc
    fi
    chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.bashrc
    go get -u ${IPFS_GO_REPO_BASE}/cmd/ipfs
    if [ ! "$?" = "0" ]; then
        exit 8242
    fi
    if [ ! -d $GOPATH/src/$IPFS_GO_REPO_BASE ]; then
        echo $'go get failed to get ipfs'
        exit 63923
    fi
    cd $GOPATH/src/$IPFS_GO_REPO_BASE
    git checkout $IPFS_COMMIT -b $IPFS_COMMIT
    if [ ! "$?" = "0" ]; then
        exit 735639
    fi
    if ! grep -q "ipfs commit" $COMPLETION_FILE; then
        echo "ipfs commit:$IPFS_COMMIT" >> $COMPLETION_FILE
    else
        sed -i "s/ipfs commit.*/ipfs commit:$IPFS_COMMIT/g" $COMPLETION_FILE
    fi
    # initialise
    su -c "$IPFS_PATH/ipfs init -b 4096" - $MY_USERNAME
    if [ ! -d /home/$MY_USERNAME/.ipfs ]; then
        echo "IPFS could not be initialised for user $MY_USERNAME"
        exit 7358
    fi
    # directories to mount to
    if [ ! -d /ipfs ]; then
        mkdir /ipfs
        mkdir /ipns
        chown $MY_USERNAME:$MY_USERNAME /ipfs
        chown $MY_USERNAME:$MY_USERNAME /ipns
    fi
    if [ -f /etc/fuse.conf ]; then
        chown $MY_USERNAME:$MY_USERNAME /etc/fuse.conf
    fi
    if [ -f /dev/fuse ]; then
        chown $MY_USERNAME:$MY_USERNAME /dev/fuse
    fi
    echo '[Unit]' > /etc/systemd/system/ipfs.service
    echo 'Description=IPFS go daemon' >> /etc/systemd/system/ipfs.service
    echo 'After=syslog.target' >> /etc/systemd/system/ipfs.service
    echo 'After=network.target' >> /etc/systemd/system/ipfs.service
    echo '' >> /etc/systemd/system/ipfs.service
    echo '[Service]' >> /etc/systemd/system/ipfs.service
    echo 'Type=simple' >> /etc/systemd/system/ipfs.service
    echo "User=$MY_USERNAME" >> /etc/systemd/system/ipfs.service
    echo "Group=$MY_USERNAME" >> /etc/systemd/system/ipfs.service
    echo "WorkingDirectory=/home/$MY_USERNAME" >> /etc/systemd/system/ipfs.service
    echo "ExecStart=$IPFS_PATH/ipfs daemon --mount" >> /etc/systemd/system/ipfs.service
    echo 'Restart=on-failure' >> /etc/systemd/system/ipfs.service
    echo "Environment=\"USER=$MY_USERNAME\" \"HOME=/home/$MY_USERNAME\" \"GOPATH=$GOPATH\" \"GVM_ROOT=$GVM_HOME\"" >> /etc/systemd/system/ipfs.service
    echo '' >> /etc/systemd/system/ipfs.service
    echo '[Install]' >> /etc/systemd/system/ipfs.service
    echo 'WantedBy=multi-user.target' >> /etc/systemd/system/ipfs.service
    systemctl enable ipfs
    systemctl daemon-reload
    systemctl restart ipfs
    if [ -d /etc/avahi ]; then
        su -c "echo $($IPFS_PATH/ipfs id | grep '\"ID\":' | awk -F '\"' '{print $4}') > /tmp/ipfsid" - $MY_USERNAME
        if [ ! -f /tmp/ipfsid ]; then
            echo 'No IPFS identity was created'
            exit 37895
        fi
        IPFS_PEER_ID=$(cat /tmp/ipfsid)
        if [ ${#IPFS_PEER_ID} -lt 10 ]; then
            echo 'Invalid IPFS peer ID'
            echo "$IPFS_PEER_ID"
            exit 74782
        fi
        # Add an avahi service
        function_check create_avahi_service
        create_avahi_service ipfs "ipfs" udp $IPFS_PORT "$IPFS_PEER_ID"
        rm /tmp/ipfsid
    fi
    function_check configure_firewall_for_ipfs
    configure_firewall_for_ipfs
    echo 'install_ipfs_go' >> $COMPLETION_FILE
}
function install_ipfs {
    #install_ipfs_js
    install_ipfs_go
}
 |