#!/bin/bash

# This file is part of PulseAudio.
#
# PulseAudio is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# PulseAudio is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
set -e

link_notebook_profile() {
    ln -s /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-notebook-internal-mic.conf /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-internal-mic.conf
    ln -s /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-notebook-mic.conf /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-mic.conf
}

link_desktop_profile() {
    ln -s /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-desktop-internal-mic.conf /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-internal-mic.conf
    ln -s /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-desktop-mic.conf /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-mic.conf
}

#check w410-bug:169010
if [[ `dmidecode |grep Centerm -c` != "0" ]] ;then
       if [[ `dmidecode |grep W410 -c` != "0" ]] ;then
        echo "#bug169010 W410"
          if [ -f "/usr/share/pulseaudio/alsa-mixer/paths/analog-input-rear-mic.conf" ] ;then
		rm -f /usr/share/pulseaudio/alsa-mixer/paths/analog-input-rear-mic.conf
	  fi
	  ln -s /usr/share/pulseaudio/alsa-mixer/paths/analog-input-rear-mic-0boost.conf /usr/share/pulseaudio/alsa-mixer/paths/analog-input-rear-mic.conf
       fi
fi

#check 飞腾D3000 DF40 超翔F870-T3MA1
if [[ `dmidecode -s system-product-name |grep -c "ChaoXiang Series"` == "1" ]] ;then
  if [[ `dmidecode -s baseboard-product-name |grep -c THTF-FTD3000-ZX200-DF40` == "1" ]] ;then
    echo "#bug291927"
    if [ -f "/usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-rear-mic.conf" ] ;then
                  rm -f /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-rear-mic.conf
          fi
          ln -s /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-rear-mic-3boost.conf /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-rear-mic.conf
  fi
fi

#check UNIS D3812 G2
if [[ `dmidecode -s system-product-name |grep -c "UNIS D3812 G2"` == "1" ]] ;then
  if [[ `dmidecode -s baseboard-product-name |grep -c MF266B` == "1" ]] ;then
    echo "#bug230422"
    if [ -f "/usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-rear-mic.conf" ] ;then
		  rm -f /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-rear-mic.conf
	  fi
	  ln -s /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-rear-mic-3boost.conf /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-rear-mic.conf
  fi
fi

#check PMDK
if [[ `aplay -l |grep -c "PMDK"` != "0" ]] ;then
	echo "PMDK"
	if [ -f "/etc/pulse/daemon.conf" ] ;then
		echo "mv /etc/pulse/daemon-PMDKHDMI.conf /etc/pulse/daemon.conf"
		mv /etc/pulse/daemon-PMDKHDMI.conf /etc/pulse/daemon.conf
	fi
fi

#check_NF220B_NF251B
if [[ `arch |grep -c aarch64` == "1" && `aplay -l |grep -c "ALC269"` == "1" ]] ;then
        chmod +755 /usr/lib/pulse-13.99.1/scripts/check_NF220B_NF251B
        if [[ `/usr/lib/pulse-13.99.1/scripts/check_NF220B_NF251B |grep -c "Cmic_flag:1"` == "1" ]] ;then
             echo "NF251B"
             ln -s /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-internal-mic-0boost.conf /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-internal-mic.conf
             ln -s /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-notebook-mic.conf /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-mic.conf
        elif [[ `/usr/lib/pulse-13.99.1/scripts/check_NF220B_NF251B |grep -c "Cmic_flag:0"` == "1" ]] ;then
             echo "NF220B"
             ln -s /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-internal-mic-1boost.conf /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-internal-mic.conf
             ln -s /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-notebook-mic.conf /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-mic.conf
        else
             link_desktop_profile
        fi
fi

#ft-hda sound card
if [[ `dmidecode --type 11 |grep -c T321` == "1" ]] ;then
    link_notebook_profile
    echo "T321" 
elif [[ `dmidecode --type 11 |grep -c T322` == "1" ]] ;then
    echo "T322"
    link_notebook_profile
elif [[ `dmidecode -s system-product-name |grep -c N69f` == "1" ]] ;then
    echo "N69f"
    ln -s /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-internal-mic-2boost.conf /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-internal-mic.conf
    ln -s /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-notebook-mic.conf /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-mic.conf
elif [[ `dmidecode -s system-product-name |grep -c N70Z` == "1" ]] ;then
    echo "N70Z"
    if [ -f "/usr/share/pulseaudio/alsa-mixer/paths/analog-input-internal-mic.conf" ] ;then
      rm -f /usr/share/pulseaudio/alsa-mixer/paths/analog-input-internal-mic.conf
    fi
    if [ -f "/usr/share/pulseaudio/alsa-mixer/paths/analog-input-mic.conf" ] ;then
      rm -f /usr/share/pulseaudio/alsa-mixer/paths/analog-input-mic.conf
    fi
    if [[ `dmidecode |grep -c "KaiTian N70Z G1d"` != "0" ]] ;then
       echo "KaiTian N70Z G1d"
       if [ -f "/usr/share/pulseaudio/alsa-mixer/paths/analog-input-alc269vc-mic.conf" ] ;then
	  rm -f /usr/share/pulseaudio/alsa-mixer/paths/analog-input-alc269vc-mic.conf
	  echo "KaiTian N70Z G1d ->analog-input-alc269vc-mic-3boost.conf"
	  ln -s /usr/share/pulseaudio/alsa-mixer/paths/analog-input-alc269vc-mic-3boost.conf /usr/share/pulseaudio/alsa-mixer/paths/analog-input-alc269vc-mic.conf
      fi
    fi
    ln -s /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-internal-mic-3boost.conf /usr/share/pulseaudio/alsa-mixer/paths/analog-input-internal-mic.conf
    ln -s /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-mic-2boost.conf /usr/share/pulseaudio/alsa-mixer/paths/analog-input-mic.conf
elif [[ `dmidecode -s system-product-name |grep -c ............221EA` == "1" ]] ;then
    echo "baixinchenlong221EA"
    if [ -f "/usr/share/pulseaudio/alsa-mixer/paths/analog-input-front-mic.conf" ] ;then
      rm -f /usr/share/pulseaudio/alsa-mixer/paths/analog-input-front-mic.conf
    fi
    if [ -f "/usr/share/pulseaudio/alsa-mixer/paths/analog-input-rear-mic.conf" ] ;then
      rm -f /usr/share/pulseaudio/alsa-mixer/paths/analog-input-rear-mic.conf
    fi
    ln -s /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-front-mic-2boost.conf /usr/share/pulseaudio/alsa-mixer/paths/analog-input-front-mic.conf
    ln -s /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-rear-mic-2boost.conf /usr/share/pulseaudio/alsa-mixer/paths/analog-input-rear-mic.conf

elif [[ `dmidecode --type 11 |grep -c 323` == "1" ]] ;then
    echo "F323"
    link_notebook_profile
elif [[ `grep -nr "14f10312" /proc/asound/ |grep -c "14f10312"` == "1" ]] ;then
    echo "X100-lenovo"
    ln -s /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-internal-mic-2boost.conf /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-internal-mic.conf
else
    echo "else link_desktop_profile"
    link_desktop_profile
fi

#ALC269VC sound card
if [[ `aplay -l |grep -c "ALC269VC"` != "0" ]] ;then
        echo "ALC269VC"
        if [[ `aplay -l |grep -c "HDMI"` != "0" ]] ;then
             echo "exist HDMI"
             ln -s /usr/share/pulseaudio/alsa-mixer/profile-sets/alc269vc-internal-mic-exist-hdmi.conf /usr/share/pulseaudio/alsa-mixer/profile-sets/alc269vc-internal-mic.conf
        else
             echo "NO HDMI"
             ln -s /usr/share/pulseaudio/alsa-mixer/profile-sets/alc269vc-internal-mic-dev3.conf /usr/share/pulseaudio/alsa-mixer/profile-sets/alc269vc-internal-mic.conf
                if [[ `dmidecode |grep -c "CP300L"` != "0" ]] ;then
                        echo "CP300L"
                        ln -s /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-internal-mic-2boost.conf /usr/share/pulseaudio/alsa-mixer/paths/analog-input-alc269vc-internal-mic.conf
                fi
        fi
fi

#避免其他声卡与ALC269VC 重复的情况：PID=0x7a07 VID=0x0014
if [ ! -f "/usr/share/pulseaudio/alsa-mixer/profile-sets/alc269vc-internal-mic.conf" ] ;then
        echo "no alc269vc-internal-mic.conf file"
        if [[ `aplay -l |grep -c "HDMI"` != "0" ]] ;then
             echo "exist HDMI -> profile-sets/alc269vc-internal-mic-exist-hdmi.conf"
             ln -s /usr/share/pulseaudio/alsa-mixer/profile-sets/alc269vc-internal-mic-exist-hdmi.conf /usr/share/pulseaudio/alsa-mixer/profile-sets/alc269vc-internal-mic.conf
        else
             echo "NO HDMI -> profile-sets/alc269vc-internal-mic-dev3.conf"
             ln -s /usr/share/pulseaudio/alsa-mixer/profile-sets/alc269vc-internal-mic-dev3.conf /usr/share/pulseaudio/alsa-mixer/profile-sets/alc269vc-internal-mic.conf
        fi
fi

if [ ! -f "/usr/share/pulseaudio/alsa-mixer/paths/analog-input-alc269vc-internal-mic.conf" ] ;then
	echo "no analog-input-alc269vc-internal-mic.conf file"
	if [[ `dmidecode |grep -c "CP302L2"` != "0" ]] ;then
		echo "NL203B CP302L2 -> paths/analog-input-hda-internal-mic-0boost.conf"
		ln -s /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-internal-mic-0boost.conf /usr/share/pulseaudio/alsa-mixer/paths/analog-input-alc269vc-internal-mic.conf
	elif [[ `dmidecode |grep -c "CP300L2"` != "0" ]] ;then
                echo "NL203B CP300L2 -> paths/analog-input-hda-internal-mic-0boost.conf"
                ln -s /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-internal-mic-0boost.conf /usr/share/pulseaudio/alsa-mixer/paths/analog-input-alc269vc-internal-mic.conf
	else
		echo "else -> paths/analog-input-hda-internal-mic-3boost.conf"
		ln -s /usr/share/pulseaudio/alsa-mixer/paths/analog-input-hda-internal-mic-3boost.conf /usr/share/pulseaudio/alsa-mixer/paths/analog-input-alc269vc-internal-mic.conf
	fi
fi

#check 41A1
if [[ `dmidecode |grep -c "706-LS3A5000-4-V1.0-B40L-41A1"` != "0" ]] ;then
        echo "TR41A1"
        if [ -f "/usr/share/pulseaudio/alsa-mixer/paths/analog-output-headphones.conf" ] ;then
                 echo "rm -f /usr/share/pulseaudio/alsa-mixer/paths/analog-output-headphones.conf"
                 rm -f /usr/share/pulseaudio/alsa-mixer/paths/analog-output-headphones.conf
                 echo "TR41A1 -> paths/analog-output-headphones-enabled-AutoMute.conf"
                 ln -s /usr/share/pulseaudio/alsa-mixer/paths/analog-output-headphones-enabled-AutoMute.conf /usr/share/pulseaudio/alsa-mixer/paths/analog-output-headphones.conf
        fi
fi

