#!/bin/ash
#UserSettings
ThisScriptConfigured=no
## Gitea Admin user
GiteaUsername=<Username>
GiteaUserEmail=<Email>
GiteaPassword=<Password>
##Gitea
GiteaName=<Name>
GiteaDomain=<Domain>
GiteaWebProto=http
##DB
MysqlIP=127.0.0.1
MysqlPort=3306
MysqlUsername=gitea
MysqlDatabase=gitea
MysqlPassword=<Password>

#Check if script is configured
if [ $ThisScriptConfigured = no ]; then
    echo  "Script is not configured"
    exit
fi

#Updateing gitea config
sed -i -e 's/DBServer/'$MysqlIP':'$MysqlPort'/' -e 's/DBName/'$MysqlDatabase'/' -e 's/DBUser/'$MysqlUsername'/' -e 's/DBPass/'$MysqlPassword'/' -e 's/DOMAINname/'$GiteaDomain'/' -e 's#DomainProto#'$GiteaWebProto'://'$GiteaDomain'#'  -e 's/InstName/'$GiteaName'/' -e 's/IntToken/'$(/usr/local/bin/gitea generate secret INTERNAL_TOKEN)'/' -e 's/SecKey/'$(/usr/local/bin/gitea generate secret SECRET_KEY)'/' -e 's/JWTSectet/'$(/usr/local/bin/gitea generate secret JWT_SECRET)'/' -e 's/LFSSecret/'$(/usr/local/bin/gitea generate secret LFS_JWT_SECRET)'/' /etc/gitea/app.ini

#generate all db tables
su git -c 'gitea --config /etc/gitea/app.ini migrate'

#Create admin user
su git -c "gitea --config /etc/gitea/app.ini admin user create --admin --email $GiteaUserEmail --username $GiteaUsername --password $GiteaPassword"

#Enable on boot
rc-update add gitea
#Start gitea
service gitea start