##新建一个文本文档，将以下代码复制进去，重命名为deepines-preinstall-fix.sh


##在终端中打开 运行 
##bash deepines-preinstall-fix.sh

##sudo apt-get install timeshift

##按y等待安装完成


#! /bin/sh

STABLEVERSION="20"
STABLEDIST="apricot"

compatcheck() {
  DISTRIB=$(lsb_release -is)
  RELEASE=$(lsb_release -rs)
  CODENAME=$(lsb_release -cs)
  KERNEL=$(uname -r)
  INCOMP=0
  SRCLIST="/etc/apt/sources.list"
  if [ "${DISTRIB}" != "Deepin" ]; then
    INCOMP=1
  elif [ "${CODENAME}" != "n/a" ]; then
    INCOMP=1
  else
    if dpkg --compare-versions "${RELEASE}" lt "${STABLEVERSION}"; then
      INCOMP=1
    else
      if [ -f "${SRCLIST}" ]; then
        DEBDIST=$(grep -E '^deb .*(/deepin/|/deepin-20-beta/)? ' "${SRCLIST}" | sed -r 's/^.* (\S+) main .*$/\1/g' | grep $STABLEDIST)
        if [ -n "${DEBDIST}" ]; then
          if [ "${DEBDIST}" != "${STABLEDIST}" ]; then
            INCOMP=2
          fi
        fi
      fi
    fi
  fi
  if [ ${INCOMP} -ne 0 ]; then

    case $INCOMP in

      1)

      echo "================================================
Este sistema operativo no es Deepin $STABLEVERSION

 Este script de reparación es unicamente para Deepin $STABLEVERSION.
 No debe usarse en otras distribuciones GNU/Linux.

 ---

This operating system is not Deepin $STABLEVERSION

  This repair script is only for Deepin $STABLEVERSION. It should not be 
  used in other GNU/Linux distributions.
================================================"

      ;;

      2)

      echo "================================================
Este sistema no tiene repositorios de Deepin $STABLEVERSION

 Este sistema no tiene configurado los repositorio de
 Deepin $STABLEVERSION en $SRCLIST. 
 No se puede determinar con certeza que este sistema es 
 Deepin $STABLEVERSION.

 ---

This system does not have Deepin $STABLEVERSION repositories

  This system does not have the Deepin $STABLEVERSION repository 
  configured in $SRCLIST. 
  It cannot be determined with certainty that this system is 
  Deepin $STABLEVERSION.
================================================"

      ;;


    esac
    exit 1
  fi
}

compatcheck


echo "================================================
Comprobando configuración de los repositorios principales
Checking configuration of main repositories
"

CHECKREPOS=$(cat "${SRCLIST}" | grep -E '^deb' | grep -Ev '^deb .*(/deepin/|/deepin-20-beta/) ')

if [ "${CHECKREPOS}" ]; then
  #statements
  echo "================================================
Repositorios configurados de manera inadecuada
Improperly configured repositories
================================================"
  echo $CHECKREPOS | sed -e "s/deb h/\n  deb h/g; s/deb \[/\n  deb \[/g"
  echo "
================================================
Estos repositorios se han configurado de manera inadecuada 
en "${SRCLIST}". Elija una de las opciones a continuación.
/ These repositories have been improperly configured 
in "${SRCLIST}". Choose one of the options below.
------------------------------------------------
"

  echo "  R) Restaurar configuración original y quitar 
     repositorios configurados inadecuadamente. / Restore original
     settings and remove improperly configured repositories.

  X) No reparar y salir. / Do not repair and exit.
"
  
  FINISH=0
  RESPUESTA=""

  while [ $FINISH -eq 0 ]; do
    #statements
    read RESPUESTA

    case $RESPUESTA in

      R|r)

        echo "Restaurando configuración original de los repositorios
principales de Deepin $STABLEVERSION / Restoring original configuration of repositories
main of Deepin $STABLEVERSION
"

        echo "## Generated by deepin-installer
deb [by-hash=force] https://community-packages.deepin.com/deepin/ apricot main contrib non-free
#deb [by-hash=force] https://mirror.deepines.com/testing/deepin-20-beta/ apricot main contrib non-free" | sudo tee "${SRCLIST}"

        echo "
================================================
Restaruación exitosa / Successful restoration
================================================
"

        
        FINISH=1
      ;;

      X|x)
        echo "Saliendo, no se aplicaron cambios / Leaving, no changes were applied"
        FINISH=1
        exit 0
      ;;

      *)
        echo "Opción incorrecta / Wrong option"
      ;;

    esac

  done

else

  echo "Rerpositorios configurados correctametne.
Repositories configured correctly."

fi

FINISH=0
RESPUESTA=""
DEEPINESPKG=$(apt list --installed 2>/dev/null | grep -E "(deepines-store|deepines-repository)" | cut -d "/" -f1)

case $DEEPINESPKG in

  deepines-store)

    echo -e "\n================================================"
    echo "¿Quiere reparar problemas para instalar Repositorio Deepines?"
    echo "Do you want to repair problems to install Deepines Repository
    "

    echo "  D) Desintalar Tienda Deepines. / Uninstall Deepines Store.

  X) No desinstalar y salir. / Do not uninstall and exit.
"

    while [ $FINISH -eq 0 ]; do
      #statements
      read RESPUESTA

      case $RESPUESTA in

        D|d)

          echo "Desintalando Tienda Deepines / Uninstalling Deepines Store"

          sudo apt purge deepines-store -y && sudo apt autoremove --purge -y

          echo "
================================================
Tienda Deepines ha sido desinstalada / Deepines Store has been uninstalled
================================================"
          FINISH=1
        ;;

        X|x)
          echo "Saliendo, no se aplicaron cambios / Leaving, no changes were applied"
          FINISH=1
        ;;

        *)
          echo "Opción incorrecta / Wrong option"
        ;;

      esac
    done

  ;;

  deepines-repository)

    echo -e "\n================================================"
    echo "¿Quiere reparar problemas para instalar Tienda Deepines?"
    echo "Do you want to repair problems to install Deepines Store
    "

    echo "  R) Reparar (Desintalar Repositorio Deepines para instalar 
     Tienda Deepines). / Repair (Uninstall Deepines Repository to install
     Deepines Store).

  X) No reparar y salir. / Do not repair and exit.
"

    while [ $FINISH -eq 0 ]; do
      #statements
      read RESPUESTA

      case $RESPUESTA in

        R|r)

          echo "Desintalando Repositorio Deepines / Uninstalling Deepines Repository"

          sudo apt purge deepines-repository -y

          echo "
================================================
Ahora puede instalar Tienda Deepiens / Now you can install Deepiens Store
================================================"
          FINISH=1
        ;;

        X|x)
          echo "Saliendo, no se aplicaron cambios / Leaving, no changes were applied"
          FINISH=1
        ;;

        *)
          echo "Opción incorrecta / Wrong option"
        ;;

      esac
    done
  ;;

esac
