echo "Grabbing old config variables..."
cheevos_username=$(grep -i "cheevos_username*" $rootfs/etc/libretro/retroarch.cfg)
cheevos_password=$(grep -i "cheevos_password*" $rootfs/etc/libretro/retroarch.cfg)
cheevos_token=$(grep -i "cheevos_token*" $rootfs/etc/libretro/retroarch.cfg)
netplay_nickname=$(grep -i "netplay_nickname*" $rootfs/etc/libretro/retroarch.cfg)
netplay_mitm_server=$(grep -i "netplay_mitm_server*" $rootfs/etc/libretro/retroarch.cfg)
netplay_ip_port=$(grep -i "netplay_ip_port*" $rootfs/etc/libretro/retroarch.cfg)
netplay_ip_address=$(grep -i "netplay_ip_address*" $rootfs/etc/libretro/retroarch.cfg)
netplay_password=$(grep -i "netplay_password*" $rootfs/etc/libretro/retroarch.cfg)
netplay_spectate_password=$(grep -i "netplay_spectate_password*" $rootfs/etc/libretro/retroarch.cfg)

echo "Uninstalling old version of Hakchi RetroArch neo..."
var=$(hakchi pack_list | grep "Hakchi_Retroarch_Neo_") && hakchi pack_uninstall $var
echo "Uninstalled $var, Installing new version now..."

transfer_default
chmod +x $rootfs/bin/*
chmod +x $rootfs/usr/bin/*
mkdir -p $rootfs/etc/libretro/system
[ -f "/newroot/usr/share/kachikachi/fds_bios.bin" ] && cp -f /newroot/usr/share/kachikachi/fds_bios.bin $rootfs/etc/libretro/system/disksys.rom
mkdir -p $rootfs/etc/options_menu/retroarch/default_files
copy $rootfs/etc/libretro/retroarch.cfg $rootfs/etc/options_menu/retroarch/default_files/
copy $rootfs/etc/libretro/retroarch-core-options.cfg $rootfs/etc/options_menu/retroarch/default_files/
loadscr_path=$rootfs/share/retroarch/assets/core_loading_screens
for arcade_loadscr in "$loadscr_path/fbalpha2012.png" "$loadscr_path/fbalpha2012_cps1.png" "$loadscr_path/fbalpha2012_cps2.png" "$loadscr_path/fbalpha2012_cps3.png" "$loadscr_path/fbalpha2012_neogeo.png" "$loadscr_path/fb_alpha.png" "$loadscr_path/mame2003.png" "$loadscr_path/mame2003_xtreme.png" "$loadscr_path/mame2010.png" "$loadscr_path/mame2014.png"; do
  if [ ! -e "$arcade_loadscr" ]; then
    ln -s "${loadscr_path#/newroot}/mame2000.png" "$arcade_loadscr"
  fi
done
for snes_loadscr in "$loadscr_path/snes9x2002.png" "$loadscr_path/snes9x2005.png" "$loadscr_path/snes9x2010.png"; do
  if [ ! -e "$snes_loadscr" ]; then
    ln -s "${loadscr_path#/newroot}/snes9x.png" "$snes_loadscr"
  fi
done
[ ! -e "$loadscr_path/fceumm.png" ] && ln -s ${loadscr_path#/newroot}/nestopia.png $loadscr_path/fceumm.png
[ ! -e "$loadscr_path/mupen64plus.png" ] && ln -s ${loadscr_path#/newroot}/glupen64.png $loadscr_path/mupen64plus.png
[ ! -e "$loadscr_path/genesis_plus_gx.png" ] && ln -s ${loadscr_path#/newroot}/picodrive.png $loadscr_path/genesis_plus_gx.png


echo "Reloading built in config..."
sed -i -e 's/cheevos_username = "[^"]*"/cheevos_username = "'$cheevos_username'"/g' $rootfs/etc/libretro/retroarch.cfg
sed -i -e 's/cheevos_password = "[^"]*"/cheevos_password = "'$cheevos_password'"/g' $rootfs/etc/libretro/retroarch.cfg
sed -i -e 's/cheevos_token = "[^"]*"/cheevos_token = "'$cheevos_token'"/g' $rootfs/etc/libretro/retroarch.cfg
sed -i -e 's/netplay_mitm_server = "[^"]*"/netplay_mitm_server = "'$netplay_mitm_server'"/g' $rootfs/etc/libretro/retroarch.cfg
sed -i -e 's/netplay_ip_port = "[^"]*"/netplay_ip_port = "'$netplay_ip_port'"/g' $rootfs/etc/libretro/retroarch.cfg
sed -i -e 's/netplay_ip_address = "[^"]*"/netplay_ip_address = "'$netplay_ip_address'"/g' $rootfs/etc/libretro/retroarch.cfg
sed -i -e 's/netplay_spectate_password = "[^"]*"/netplay_spectate_password = "'$netplay_spectate_password'"/g' $rootfs/etc/libretro/retroarch.cfg

if [ "$cheevos_username" == "hakchiresources" ]; then
  sed -i -e 's/cheevos_enable = "[^"]*"/cheevos_enable = "false"/g' $rootfs/etc/libretro/retroarch.cfg
fi


echo "Install complete!"
return 1
