#!/bin/bash -e

PURPLE='\033[1;35m'
NC='\033[0m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
RED='\033[1;31m'
CYAN='\033[0;36m'
BLUE='\033[1;34m'

CURVERSION="v0.53"
TALISMANURL="https://talismanbbs.com/releases/"

echo -e "${YELLOW}This script will install Talisman into $(pwd)${NC}"

echo -e ""

read -n 1 -s -r -p "Press any enter to continue. Ctrl-C to Abort"

echo -e ""
echo -e ""

echo -e "${YELLOW}Using SUDO to see if essentials are installed..${NC}"
sudo apt install build-essential cmake libsqlite3-dev libssl-dev lrzsz liblua5.3-dev libssh-dev wget libcurl4-openssl-dev

mkdir source
mkdir build
mkdir build/talisman
mkdir build/servo
mkdir build/toolbelt
mkdir build/postie
mkdir build/qwkie
mkdir build/falcon
mkdir build/binki
mkdir build/gofer
mkdir dloads
mkdir dloads/general
mkdir dloads/general/uploads
mkdir dloads/general/misc
mkdir scripts
mkdir scripts/data
mkdir data
mkdir menus
mkdir gfiles
mkdir msgs
mkdir logs
mkdir temp
mkdir gopher

cd source/

wget "${TALISMANURL}Talisman-${CURVERSION}-Source.tar.gz"

tar xzf Talisman-${CURVERSION}-Source.tar.gz

cd ../build/servo

cmake ../../source/talisman/Servo
make

cd ../../build/talisman
cmake ../../source/talisman/Talisman
make

cd ../../build/toolbelt
cmake ../../source/talisman/Toolbelt
make

cd ../../build/postie
cmake ../../source/talisman/Postie
make

cd ../../build/qwkie
cmake ../../source/talisman/Qwkie
make

cd ../../build/falcon
cmake ../../source/talisman/Falcon
make

cd ../../build/binki
cmake ../../source/talisman/Binki
make

cd ../../build/gofer
cmake ../../source/talisman/Gofer
make


cd ../../
ln -s build/servo/servo
ln -s build/talisman/talisman
ln -s build/toolbelt/toolbelt
ln -s build/postie/postie
ln -s build/qwkie/qwkie
ln -s build/falcon/falcon
ln -s build/binki/binki
ln -s build/gofer/gofer

cp -r source/talisman/Talisman/menus/* menus/
cp -r source/talisman/Talisman/gfiles/* gfiles/
cp -r source/talisman/Talisman/scripts/* scripts/
cp -r source/talisman/Talisman/data/* data/
cp source/talisman/Talisman/win32_deps/gophermap gopher/

echo -e "${PURPLE}Please enter your user handle:${NC}"
read -e handle

echo -e "${PURPLE}Please enter your bbs name:${NC}"
read -e bbsname

echo -e "${PURPLE}Please enter your location:${NC}"
read -e location

echo -e "${PURPLE}Please enter a QWK ID (8 chars max uppercase):${NC}"
read -e qwkid

echo -e "${PURPLE}Please enter your bbs hostname:${NC}"
read -e hostname

cat > talisman.ini << EOF
[main]
; Ports below 1024 require setcap on servo to enable binding.
telnet port = 2323
ssh port = 2222
; Gopher is disabled by default set to a port (default 70) to enable
gopher port = -1
; BinkP is disabled by default set to a port (default 24554) to enable
binkp port = -1
max nodes = 4
root menu = main
sysop name = $handle
system name = $bbsname
qwk id = $qwkid
location = $location
hostname = $hostname
enable ipv6 = false

[paths]
gfiles path = gfiles
data path = data
menu path = menus
message path = msgs
log path = logs
temp path = temp
script path = scripts
gopher root = gopher
netmail semaphore = data/mail.sem
echomail semaphore = data/mail.sem
EOF

echo -e "${GREEN}Please modify talisman.ini to your liking. See https://talismandocs.com/ for details.${NC}"
echo -e "${GREEN}then run ./servo to start the BBS..${NC}"
