#!/usr/bin/env sh
#This scripts applys the patch against segfaults, creates a launch script with the right language enviroment and a program starter with icon
#saved login credentials, will be written and evaluted by this script at runtime
#If you wish to reset the saved credentials, delete all lines between '#start save' and '#end save'
#start save
login=''; #save:login
passwd=''; #save:passwd
#end save
GamePath="$(readlink -m "${1:-${pwd}}")"
BASEURL="https://www.kerbalspaceprogram.com"
VERSION_REGEX="linux-zip-[0-9]-[0-9]{1,2}(-[0-9])?"
VERSION_REGEX_DLC1="linux-dlc-mhe-en-us-[0-9]-[0-9]{1,2}(-[0-9])?"
archiveFile=""
basegameInstallFile=""
dlc1InstallFile=""
echored() {
printf "\033[33;31m${1}\033[33;0m\n"
}
echogreen() {
printf "\033[33;32m${1}\033[33;0m\n"
}
findInstallArchives() {
basegameInstallFile="$(find . -maxdepth 1 -regextype posix-extended -regex '.*ksp-linux-zip-[[:digit:]]-[[:digit:]]{1,2}-?[[:digit:]]{0,2}.zip' -print -quit)"
dlc1InstallFile="$(find . -maxdepth 1 -regextype posix-extended -regex '.*ksp-linux-dlc.*[[:digit:]]-[[:digit:]]{1,2}-?[[:digit:]]{0,2}.zip' -print -quit)"
}
download() {
archiveFile="ksp-${1}.zip" #"ksp-linux-$(echo "${1}" | sed -rn 's/lin(ux)?-()/\2/p').zip"
echogreen "Download Version ${1}"
trap 'sighandler_EXIT' EXIT INT TERM
wget --load-cookies=cookies.txt "${BASEURL}/lib/highwindsDwn.php?version=${1}" -O "${archiveFile}"
trap EXIT INT TERM
}
extractBaseGameArchiv() {
extractArchiv "${1}"
# the archive contains the 'KSP_linux' directory, we move it's content to desired ${GamePath}
cp --archive --link --no-dereference --recursive -f "${GamePath}"/KSP_linux/* "${GamePath}/"
rm -rf "${GamePath}/"KSP_linux
}
extractArchiv() {
unzip -o "${1}" -d "${GamePath}"
if [ $? != 0 ]; then
echored "Could not extract Archive"
exit 1
fi
}
sighandler_EXIT() {
rm -f cookies.txt "$(readlink -f "${0}")".tmp
echored 'Aborting Install Script'
exit 1
}
findInstallArchives
if [ ! -f "${GamePath}/buildID.txt" ]; then
echogreen "'${GamePath}' seems not contain an existing KSP installation. Continuing with a new installtion"
if [ -n "${basegameInstallFile}" -a -n "${dlc1InstallFile}" ];
then
echo ""
else
trap 'break' EXIT INT TERM
#try until succesfull login, proven by version number from download page
while [ -z "${latestVersionString}" ];
do
if [ -z "${passwd+x}" ] || [ -z ${passwd} ];
then
echogreen "Please your enter credentials for your Account at ${BASEURL}."
echo "Login:"
read login
echo "Password:"
read passwd
else
echogreen "Using saved credentials."
fi
echogreen "Login at ${BASEURL}"
#Login and save session id in cookies.txt
wget -q --keep-session-cookies --save-cookies cookies.txt --post-data "email=${login}&password=${passwd}&login=Login" "${BASEURL}/kspstore/app/web/user/login" -O /dev/null
#Get your account page for download site parameeter
downloadParameter=$(wget -q "${BASEURL}/profile.php?userProf=info" --load-cookies=cookies.txt -O - | sed -rn 's/.*.*/\1/p')
#Which is the latest avaible ksp version
latestVersionString=$(wget -q "${BASEURL}/download.php?od=$downloadParameter" --load-cookies=cookies.txt -O - | grep -Eo ".*lib/highwindsDwn.php\?version=$VERSION_REGEX" | grep -Eo "$VERSION_REGEX$" | sort -u -t- -V | tail -n 1)
if [ -z "${latestVersionString}" ];
then
echored "Wrong login credentials!"
#if given credentials are wrong, reset it to force manual input
login=''; passwd=''
fi
done
if [ "${latestVersionString}" ];
then
#saving login credentials in this script
awk -v Var_login=$login -v Var_passwd=$passwd '
/^#start save$/{print $0; a=1}
a == 0 {print $0}
a == 1 { print "login='"'"'" Var_login "'"'"'; #save:login"; print "passwd='"'"'" Var_passwd "'"'"'; #save:passwd"; a=2 }
/^#end save$/{print $0; a=0}
' "$(readlink -f "${0}")" > "$(readlink -f "${0}")".tmp
mv "$(readlink -f "${0}")".tmp "$(readlink -f "${0}")"
chmod a+x "$(readlink -f "${0}")"
echogreen "Succesfully loged in and saved credentials."
fi
trap EXIT INT TERM
fi
mkdir -p "${GamePath}"
# Base Game
if [ -n "${basegameInstallFile}" ]
then
archiveFile="$(find . -maxdepth 1 -name "ksp-linux-*.zip" 2> /dev/null | sort | tail -n1 )"
echogreen "Use existing archive ${basegameInstallFile}"
else
if [ ! -f "cookies.txt" ]
then
echored "You don't have succesfully loged into your Account at ${BASEURL}. Can't download Game. Aborting"
exit 1
fi
echogreen "Download latest linux version from https://kerbalspaceprogram.com"
download "${latestVersionString}"
fi
findInstallArchives
extractBaseGameArchiv "${basegameInstallFile}"
# DLC: Making History
if [ -n "${dlc1InstallFile}" ]
then
archiveFile="$(find . -name "ksp-linux-dlc-mhe-*.zip" 2> /dev/null | sort | tail -n1 )"
echogreen "Use existing archive ${dlc1InstallFile}"
unzip -oq "${dlc1InstallFile}"
extractArchiv "KSP-Making_History_Expansion-en-us.zip"
rm Instructions-lin-*.txt dlc-mhe-*.sh KSP-Making_History_Expansion-*.zip
else
downloadParameter=$(wget -q "${BASEURL}/profile.php?userProf=info" --load-cookies=cookies.txt -O - | sed -rn 's/.*.*/\1/p')
if [ "${downloadParameter}" ]; then
latestVersionString=$(wget -q "${BASEURL}/downloadMHE.php?od=$downloadParameter" --load-cookies=cookies.txt -O - | grep -Eo ".*lib/highwindsDwn.php\?version=$VERSION_REGEX_DLC1" | grep -Eo "$VERSION_REGEX_DLC1$" | sort -u -t- -V | tail -n 1)
download "${latestVersionString}"
unzip -oq "${archiveFile}"
extractArchiv "KSP-Making_History_Expansion-en-us.zip"
rm Instructions-lin-*.txt dlc-mhe-*.sh KSP-Making_History_Expansion-*.zip
fi
fi
fi
rm -f cookies.txt
echogreen "Installation succesfull. Continuing with patching and System integration"
#Segfault patch
position1=$(echo "obase=16; $(perl -n0777e 'print pos()-1,"\n" while /\x01\x00\x00\x00\xB8\x01\x00\x00\x00\xC3\x41\x56\x41\x55\x49/g' "${GamePath}/KSP.x86_64") - E" | bc)
if [ "$position1" != "-E" ]
then
echogreen "Patching Segfault out of the Rockets ..."
cp "${GamePath}/KSP.x86_64" "${GamePath}/KSP.x86_64.unpatched"
position2=$(echo "obase=16;ibase=16; $position1 + 5" | bc)
echo "$position1: 00" | xxd -r - "${GamePath}/KSP.x86_64"
echo "$position2: 00" | xxd -r - "${GamePath}/KSP.x86_64"
else
echogreen "Patching seems not necessary(Game version without that bug) or possible(already modified executable?)"
fi
#create the launch script with correct language enviroment variables
cat > "${GamePath}/ksp_launch" <<'EOF'
#!/bin/sh
cd "`dirname "$(readlink -f "${0}")"`"
if [ `arch` = "x86" ]
then
LC_ALL=C LD_PRELOAD="libpthread.so.0 libGL.so.1" __GL_THREADED_OPTIMIZATIONS=1 ./KSP.x86
elif [ `arch` = "x86_64" ]
then
LC_ALL=C LD_PRELOAD="libpthread.so.0 libGL.so.1" __GL_THREADED_OPTIMIZATIONS=1 ./KSP.x86_64
else
echo "Nope, your processor architecture is not supported."
echo "Maybe it would help to add more boosters?"
fi
cd -
EOF
chmod u+rx "${GamePath}/ksp_launch"
#create a .desktop file Program starter
cat > "${GamePath}/squad-ksp.desktop" < "${GamePath}/ksp_icon.png" <