#!/bin/bash
#This script sets up a Wine prefix, renaming the user account from the Unix
#username to `osmuseum` if that is different, as well as removing any symlinks
#to the Unix home directory
set -e

export WINEPREFIX="`realpath $1`"

USERS_DIR="$WINEPREFIX/drive_c/users"

if [ ! -d "$WINEPREFIX" ]
then
	mkdir "$WINEPREFIX"
fi

if [ "$USER" != osmuseum -a -d "$USERS_DIR/$USER" -a ! -d "$USERS_DIR/osmuseum" ]
then
	mv "$USERS_DIR/$USER" "$USERS_DIR/osmuseum"
	sed -i 's/\\\\users\\\\'$USER'/\\\\users\\\\osmuseum/g' $WINEPREFIX/*.reg
	sed -i 's/\\\\home\\\\'$USER'/\\\\home\\\\osmuseum/g' $WINEPREFIX/*.reg
fi

USER=osmuseum wineboot
find "$USERS_DIR/osmuseum" -type l -not -lname "$USERS_DIR/osmuseum/*" -not -lname '[^/]*' -exec rm '{}' \; -exec mkdir '{}' \;
