#!/bin/bash

NFS_OPTIONS="rw,async,hard,noexec,rsize=8192,wsize=8192,timeo=14"
BINDFS_RIGHTS="--force-user=2222 --force-group=2223 --perms=0777"

if ( systemctl is-active --quiet tailscaled.service && [[ $(tailscale status --peers=false --json=true | jq -r '.Self.Online') = "true" ]] )
then
    mkdir -p /mnt/kut-nas/{backup,ebooks,inbox,outbox,public,scripts}
    mkdir -p /mnt/{backup,ebooks,inbox,outbox,public,scripts}
    if ( mount -o $NFS_OPTIONS  kut-nas.swallow-grue.ts.net:/backup /mnt/kut-nas/backup )
    then
        bindfs $BINDFS_RIGHTS /mnt/kut-nas/backup /mnt/backup
    fi
    if ( mount -o $NFS_OPTIONS kut-nas.swallow-grue.ts.net:/ebooks /mnt/kut-nas/ebooks )
    then
        bindfs $BINDFS_RIGHTS /mnt/kut-nas/ebooks /mnt/ebooks
    fi
    if ( mount -o $NFS_OPTIONS kut-nas.swallow-grue.ts.net:/inbox /mnt/kut-nas/inbox )
    then
        bindfs $BINDFS_RIGHTS /mnt/kut-nas/inbox /mnt/inbox
    fi
    if ( mount -o $NFS_OPTIONS kut-nas.swallow-grue.ts.net:/outbox /mnt/kut-nas/outbox )
    then
        bindfs $BINDFS_RIGHTS /mnt/kut-nas/outbox /mnt/outbox
    fi
    if ( mount -o $NFS_OPTIONS kut-nas.swallow-grue.ts.net:/public /mnt/kut-nas/public )
    then
        bindfs $BINDFS_RIGHTS /mnt/kut-nas/public /mnt/public
    fi
    if ( mount -o $NFS_OPTIONS kut-nas.swallow-grue.ts.net:/scripts /mnt/kut-nas/scripts )
    then
        bindfs $BINDFS_RIGHTS /mnt/kut-nas/scripts /mnt/scripts
    fi
fi
